/**
  * Gets the classes whose objects the user can execute certain action
  *
  * @param $action object - The action that the user should be allowed to do
  * @param $user User - The user that holds de permissions
  * @return Array - an array of class objects
  */
 function &findByPermission($action, $user)
 {
     $classQuery = new Query("Class");
     // Navigate relationships
     $folderClassQuery =& $classQuery->queryRelationedClass("FolderClass");
     $permissionQuery =& $folderClassQuery->queryRelationedClass("Permission");
     $actionQuery =& $permissionQuery->queryRelationedClass("Action", Relationship::ManyToOneType());
     $roleQuery =& $permissionQuery->queryRelationedClass("Role", Relationship::ManyToOneType());
     $roleUserQuery =& $roleQuery->queryRelationedClass("RoleUser");
     $userQuery =& $roleUserQuery->queryRelationedClass("User", Relationship::ManyToOneType());
     // Criterias
     $criteriaGroup = new CriteriaGroup();
     $actionCriteria = new Criteria($actionQuery, "action", $action->getAction());
     $userCriteria = new Criteria($userQuery, "ID", $user->getId());
     $criteriaGroup->addCriterion($actionCriteria);
     $criteriaGroup->addCriterion($userCriteria);
     $classQuery->setCriterion($criteriaGroup);
     // sorting
     $order = new Order($classQuery, "title", "ASC");
     $classQuery->addOrder($order);
     // Execute the query
     $recordset =& $classQuery->execute();
     $array = $this->mapAll($recordset);
     return $array;
 }
 /**
  * Gets the folders for a user to do certain action.
  *
  * @param $action Action - that the user intends to do
  * @param $user User - the user that tries to get his folders.
  * @return Array - vector of folder objects
  */
 function &findByPermission($action, $user)
 {
     $folderQuery = $this->newQueryObject();
     $folderClassQuery =& $folderQuery->queryRelationedClass("FolderClass");
     $permissionQuery =& $folderClassQuery->queryRelationedClass("Permission");
     $actionQuery =& $permissionQuery->queryRelationedClass("Action", Relationship::ManyToOneType());
     $roleQuery =& $permissionQuery->queryRelationedClass("Role", Relationship::ManyToOneType());
     $roleUserQuery =& $roleQuery->queryRelationedClass("RoleUser");
     $criteriaGroup = new CriteriaGroup();
     $actionCriteria = new Criteria($actionQuery, "action", $action->getAction());
     $criteriaGroup->addCriterion($actionCriteria);
     $userCriteria = new Criteria($roleUserQuery, "userID", $user->getId());
     $criteriaGroup->addCriterion($userCriteria);
     // Set the criterion
     $folderQuery->setCriterion($criteriaGroup);
     // Execute
     return $this->mapAll($folderQuery->execute());
 }
 /**
  * Searches objects by text parameters
  * @param $classId Id of the class
  * @param $text Text to lookup in the objects
  * @param $fullTextSearch if true, the text is searched in all text fields and every coincidence is returned. 
  *				If false, it is only looked up in the title.
  */
 function findByText($classId, $text, $fullTextSearch, $folderId = -1)
 {
     $objectQuery = new Query("Object");
     $criteriaGroup = new CriteriaGroup(CriteriaGroup::getAndSeparator());
     // Lookup by class...
     $criteria = new Criteria($objectQuery, "classID", $classId);
     $criteriaGroup->addCriterion($criteria);
     if ($folderId != -1 && $folderId != null) {
         // Lookup by folder...
         $objectFolderQuery =& $objectQuery->queryRelationedClass("ObjectFolder");
         $folderCriteria = new Criteria($objectFolderQuery, "folderID", $folderId);
         $criteriaGroup->addCriterion($folderCriteria);
     }
     // ... and criterias sent
     if ($fullTextSearch) {
         // by fullTextIndex
         $criteria = new Criteria($objectQuery, "fullTextIndex", $text, Criteria::likeType());
         $criteriaGroup->addCriterion($criteria);
     } else {
         // Issue a query to ObjectAttribute entity
         $objectAttributeQuery =& $objectQuery->queryRelationedClass("ObjectAttribute");
         // Get the class
         $baseClassMapper = new BaseClassMapper();
         $class = $baseClassMapper->get($classId);
         /* @var $class BaseClass */
         // Get the fields that compose the title
         $titleClassAttributesArray = $class->getTitleClassAttributes();
         // We need to query all title fields using the given value with OR (anyone can match)
         $criteriaGroupTitle = new CriteriaGroup(CriteriaGroup::getOrSeparator());
         foreach ($titleClassAttributesArray as $titleClassAttribute) {
             // Must belong to the given class and LIKE the given title
             $criteriaGroupItem = new CriteriaGroup();
             /* @var $titleClassAttribute ClassAttribute */
             $criteriaGroupItem->addCriterion($criteria);
             $criteria = new Criteria($objectAttributeQuery, "classAttributeId", $titleClassAttribute->getId());
             $criteriaGroupItem->addCriterion($criteria);
             $criteria = new Criteria($objectAttributeQuery, "value", $text, Criteria::likeType());
             $criteriaGroupItem->addCriterion($criteria);
             $criteriaGroupTitle->addCriterion($criteriaGroupItem);
         }
         // Set the title criteria
         $criteriaGroup->addCriterion($criteriaGroupTitle);
     }
     $objectQuery->setCriterion($criteriaGroup);
     // Execute the query and map the results
     $objectMapper = new ObjectMapper();
     $rv = $objectMapper->mapAll($objectQuery->execute());
     return $rv;
 }
echo "Resulting query: " . $query->getQueryString() . "<br/>";
assert("SELECT DISTINCT ##Object.* FROM ##Object LEFT OUTER JOIN ##Class ON ##Object.ID = ##Class.ObjectID WHERE ##Class.className = 'news'" == $query->getQueryString());
echo "Query with CriteriaGroup instead of only one Criteria<br/>";
$query = new Query("object");
$criteriaGroup = new CriteriaGroup(CriteriaGroup::getAndSeparator());
$criteria = new Criteria($query, "hits", "10", Criteria::lessEqualThanType());
$criteriaGroup->addCriterion($criteria);
$criteria = new Criteria($query, "hits", "1", Criteria::moreEqualThanType());
$criteriaGroup->addCriterion($criteria);
$query->setCriterion($criteriaGroup);
echo "Resulting query: " . $query->getQueryString() . "<br/>";
echo "Query with multiples Criteria Groups and an order<br/>";
$query = new Query("object");
$criteriaGroupRoot = new CriteriaGroup(CriteriaGroup::getAndSeparator());
$criteriaGroupOne = new CriteriaGroup(CriteriaGroup::getAndSeparator());
$criteria = new Criteria($query, "created", "2005-04-15", Criteria::lessThanType());
$criteriaGroupOne->addCriterion($criteria);
$criteriaGroupTwo = new CriteriaGroup(CriteriaGroup::getOrSeparator());
$criteria = new Criteria($query, "updatedBy", "1");
$criteriaGroupTwo->addCriterion($criteria);
$criteria = new Criteria($query, "updatedBy", "2");
$criteriaGroupTwo->addCriterion($criteria);
$criteriaGroupRoot->addCriterion($criteriaGroupOne);
$criteriaGroupRoot->addCriterion($criteriaGroupTwo);
$query->setCriterion($criteriaGroupRoot);
$order = new Order($query, "hits", Order::OrderTypeAsc());
$query->addOrder($order);
echo "Resulting query: " . $query->getQueryString() . "<br/>";
$rs = $query->execute();
echo "Executed query: ";
print_r($rs);