Overview

Packages

  • Horde
    • Form
    • MIME
      • Viewer
    • Scheduler
  • None
  • Whups
    • UnitTests

Classes

  • Horde_Core_Ui_VarRenderer_whups
  • Whups
  • Whups_Ajax_Imple_ContactAutoCompleter
  • Whups_Api
  • Whups_Driver
  • Whups_Driver_Sql
  • Whups_Form_AddComment
  • Whups_Form_Admin_AddAttribute
  • Whups_Form_Admin_AddPriority
  • Whups_Form_Admin_AddQueue
  • Whups_Form_Admin_AddReply
  • Whups_Form_Admin_AddState
  • Whups_Form_Admin_AddType
  • Whups_Form_Admin_AddUser
  • Whups_Form_Admin_AddVersion
  • Whups_Form_Admin_CloneType
  • Whups_Form_Admin_DefaultPriority
  • Whups_Form_Admin_DefaultState
  • Whups_Form_Admin_DeleteAttribute
  • Whups_Form_Admin_DeletePriority
  • Whups_Form_Admin_DeleteQueue
  • Whups_Form_Admin_DeleteReply
  • Whups_Form_Admin_DeleteState
  • Whups_Form_Admin_DeleteType
  • Whups_Form_Admin_DeleteVersion
  • Whups_Form_Admin_EditAttributeStepOne
  • Whups_Form_Admin_EditAttributeStepTwo
  • Whups_Form_Admin_EditPriorityStepOne
  • Whups_Form_Admin_EditPriorityStepTwo
  • Whups_Form_Admin_EditQueueStepOne
  • Whups_Form_Admin_EditQueueStepTwo
  • Whups_Form_Admin_EditReplyStepOne
  • Whups_Form_Admin_EditReplyStepTwo
  • Whups_Form_Admin_EditStateStepOne
  • Whups_Form_Admin_EditStateStepTwo
  • Whups_Form_Admin_EditTypeStepOne
  • Whups_Form_Admin_EditTypeStepTwo
  • Whups_Form_Admin_EditUser
  • Whups_Form_Admin_EditVersionStepOne
  • Whups_Form_Admin_EditVersionStepTwo
  • Whups_Form_InsertBranch
  • Whups_Form_Query_AttributeCriterion
  • Whups_Form_Query_ChooseNameForLoad
  • Whups_Form_Query_ChooseNameForSave
  • Whups_Form_Query_DateCriterion
  • Whups_Form_Query_Delete
  • Whups_Form_Query_GroupCriterion
  • Whups_Form_Query_Parameter
  • Whups_Form_Query_PropertyCriterion
  • Whups_Form_Query_TextCriterion
  • Whups_Form_Query_UserCriterion
  • Whups_Form_Renderer_Comment
  • Whups_Form_Search
  • Whups_Form_SendReminder
  • Whups_Form_Ticket_CreateStepFour
  • Whups_Form_Ticket_CreateStepOne
  • Whups_Form_Ticket_CreateStepThree
  • Whups_Form_Ticket_CreateStepTwo
  • Whups_Form_Ticket_Edit
  • Whups_Form_TicketDetails
  • Whups_LoginTasks_SystemTask_Upgrade
  • Whups_Mail
  • Whups_Query
  • Whups_Query_Manager
  • Whups_Reports
  • Whups_Ticket
  • Whups_View_Base
  • Whups_View_Results
  • Whups_View_SavedQueries
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * The whups query manager.
  4:  *
  5:  * Copyright 2001-2002 Robert E. Coyle <robertecoyle@hotmail.com>
  6:  * Copyright 2001-2012 Horde LLC (http://www.horde.org/)
  7:  *
  8:  * See the enclosed file LICENSE for license information (BSD). If you
  9:  * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
 10:  *
 11:  * @author  Robert E. Coyle <robertecoyle@hotmail.com>
 12:  * @author  Jan Schneider <jan@horde.org>
 13:  * @package Whups
 14:  */
 15: 
 16: class Whups_Query_Manager
 17: {
 18:     /**
 19:      * Horde_Share instance for managing shares.
 20:      *
 21:      * @var Horde_Share
 22:      */
 23:     protected $_shareManager;
 24: 
 25:     /**
 26:      * Constructor.
 27:      *
 28:      * @TODO: inject the share driver
 29:      */
 30:     public function __construct()
 31:     {
 32:         $this->_shareManager =
 33:             $GLOBALS['injector']->getInstance('Horde_Core_Factory_Share')->create();
 34:     }
 35: 
 36:     /**
 37:      * Returns a specific query identified by its id.
 38:      *
 39:      * @param integer $queryId  A query id.
 40:      *
 41:      * @return Whups_Query  The matching query or null if not found.
 42:      * @throws Whups_Exception
 43:      */
 44:     public function getQuery($queryId)
 45:     {
 46:         try {
 47:             $share = $this->_shareManager->getShareById($queryId);
 48:         } catch (Horde_Exception_NotFound $e) {
 49:             throw new Whups_Exception($e);
 50:         }
 51:         return $this->_getQuery($share);
 52:     }
 53: 
 54:     /**
 55:      * Returns a specific query identified by its slug name.
 56:      *
 57:      * @param string $slug  A query slug.
 58:      *
 59:      * @return Whups_Query  The matching query or null if not found.
 60:      * @throws Whups_Exception
 61:      */
 62:     public function getQueryBySlug($slug)
 63:     {
 64:         try {
 65:             $shares = $this->_shareManager->listShares(
 66:                 $GLOBALS['registry']->getAuth(),
 67:                 array('perm' => Horde_Perms::READ,
 68:                       'attributes' => array('slug' => $slug)));
 69:         } catch (Horde_Share_Exception $e) {
 70:             throw new Whups_Exception($e);
 71:         }
 72:         if (!count($shares)) {
 73:             return;
 74:         }
 75: 
 76:         return $this->_getQuery(reset($shares));
 77:     }
 78: 
 79:     /**
 80:      * Builds a query object from a share object.
 81:      *
 82:      * @param Horde_Share_Object $share  A share object representing a query.
 83:      *
 84:      * @return Whups_Query  The query object built from the share.
 85:      */
 86:     protected function _getQuery(Horde_Share_Object $share)
 87:     {
 88:         $queryDetails = $GLOBALS['whups_driver']->getQuery($share->getId());
 89:         $queryDetails['query_id'] = $share->getId();
 90:         $queryDetails['query_name'] = $share->get('name');
 91:         $queryDetails['query_slug'] = $share->get('slug');
 92: 
 93:         return new Whups_Query($this, $queryDetails);
 94:     }
 95: 
 96:     /**
 97:      * Checks to see if a user has a given permission to $queryId.
 98:      *
 99:      * @param integer $queryId     The query to check.
100:      * @param string $userid       The userid of the user.
101:      * @param integer $permission  A Horde_Perms::* constant to test for.
102:      * @param string $creator      The creator of the event.
103:      *
104:      * @return boolean  Whether or not $userid has $permission.
105:      */
106:     public function hasPermission($queryId, $userid, $permission, $creator = null)
107:     {
108:         try {
109:             $share = $this->_shareManager->getShareById($queryId);
110:         } catch (Horde_Exception_NotFound $e) {
111:             // If the share doesn't exist yet, then it has open perms.
112:             return true;
113:         }
114:         return $share->hasPermission($userid, $permission, $creator);
115:     }
116: 
117:     /**
118:      * List queries.
119:      */
120:     public function listQueries($user, $return_slugs = false)
121:     {
122:         try {
123:             $shares = $this->_shareManager->listShares($user);
124:         } catch (Horde_Share_Exception $e) {
125:             throw new Whups_Exception($e);
126:         }
127: 
128:         $queries = array();
129:         foreach ($shares as $share) {
130:             $queries[$share->getId()] = $return_slugs
131:                 ? array('name' => $share->get('name'),
132:                         'slug' => $share->get('slug'))
133:                 : $share->get('name');
134:         }
135: 
136:         return $queries;
137:     }
138: 
139:     /**
140:      */
141:     public function newQuery()
142:     {
143:         return new Whups_Query($this);
144:     }
145: 
146:     /**
147:      * @param Whups_Query $query The query to save.
148:      * @throws Whups_Exception
149:      */
150:     public function save(Whups_Query $query)
151:     {
152:         if ($query->id) {
153:             // Query already exists; get its share and update the name
154:             // if necessary.
155:             try {
156:                 $share = $this->_shareManager->getShareById($query->id);
157:             } catch (Horde_Exception_NotFound $e) {
158:                 // Share has an id but doesn't exist; just throw an
159:                 // error.
160:                 throw new Whups_Exception($e);
161:             }
162:             if ($share->get('name') != $query->name ||
163:                 $share->get('slug') != $query->slug) {
164:                 $share->set('name', $query->name);
165:                 $share->set('slug', $query->slug);
166:                 $share->save();
167:             }
168:         } else {
169:             // Create a new share for the query.
170:             $share = $this->_shareManager->newShare($GLOBALS['registry']->getAuth(), (string)new Horde_Support_Uuid(), $query->name);
171:             $share->set('slug', $query->slug);
172:             try {
173:                 $this->_shareManager->addShare($share);
174:             } catch (Horde_Share_Exception $e) {
175:                 throw new Whups_Exception($e);
176:             }
177:             $query->id = $share->getId();
178:         }
179: 
180:         // Update the queries table.
181:         $GLOBALS['whups_driver']->saveQuery($query);
182:     }
183: 
184:     /**
185:      * @param Whups_Query $query The query to delete.
186:      */
187:     public function delete(Whups_Query $query)
188:     {
189:         if (!$query->id) {
190:             // Queries that aren't saved yet shouldn't be able to be deleted.
191:             return;
192:         }
193: 
194:         try {
195:             $share = $this->_shareManager->getShareById($query->id);
196:             $this->_shareManager->removeShare($share);
197:         } catch (Exception $e) {
198:             throw new Whups_Exception($e);
199:         }
200:         $GLOBALS['whups_driver']->deleteQuery($query->id);
201:     }
202: 
203: }
API documentation generated by ApiGen