Exemplo n.º 1
0
 /**
  * Sets element id if table has no "id" attribute
  */
 public function setId()
 {
     if (isset($this->id)) {
         return;
     }
     $tableName = DatabaseFactory::getElementTableName($this->getElementClass());
     // Creates an attribute "id" for "_has_" tables
     if (StringTool::contains($tableName, ElementFactory::TABLE_JOIN_SEPARATOR)) {
         // Creates an attribute 'id' from both primary keys
         $tableList = StringTool::split(ElementFactory::TABLE_JOIN_SEPARATOR, $tableName);
         // Sets condition from both tables
         $table1FieldName = $tableList[0] . '_id';
         $table2FieldName = $tableList[1] . '_id';
         $this->id = $this->{$table1FieldName} . '-' . $this->{$table2FieldName};
     }
 }
 /**
  * Gets current request complete url
  * @param boolean $includeGetParamList if GET params have to be added to URI
  * @return string The url
  */
 public static function getCurrentURL($includeGetParamList = true)
 {
     $url = 'http' . (!ArrayTool::array_key_exists('HTTPS', $_SERVER) || $_SERVER['HTTPS'] == 'off' ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
     // Gets only string before the "?" if get params are ignored
     if (!$includeGetParamList) {
         list($url) = StringTool::split('\\?', $url);
     }
     return $url;
 }
 /**
  * Updates an element in database
  * @param Element $element The element to update
  * @return int The affected rows number
  */
 public static function updateElement($element)
 {
     $elementClass = $element->getElementClass();
     $tableName = DatabaseFactory::getElementTableName($elementClass);
     // '_has_' tables have 2 primary keys
     if (!StringTool::contains($tableName, ElementFactory::TABLE_JOIN_SEPARATOR)) {
         $conditions = $tableName . '_id = \'' . $element->id . '\'';
     } else {
         $tableList = StringTool::split(ElementFactory::TABLE_JOIN_SEPARATOR, $tableName);
         // Sets condition from both tables
         $tableFieldName = $tableList[0] . '_id';
         $conditions = $tableFieldName . ' = \'' . $element->{$tableFieldName} . '\'';
         $tableFieldName = $tableList[1] . '_id';
         $conditions .= ' AND ' . $tableFieldName . ' = \'' . $element->{$tableFieldName} . '\'';
     }
     $updatedPropertyList = $element->getUpdatedPropertyList();
     // Updates the element in database
     $affectedRowNumber = DatabaseFactory::updateElementList($elementClass, $updatedPropertyList, $conditions);
     return $affectedRowNumber;
 }
 /**
  * Gets parent element from an element
  * @param Element $element The child element
  * @param string $parentClass The parent element class
  * @param string $conditions The conditions string to apply
  * @param string $orderBy The order string to apply
  * @return Element The parent element
  */
 public static function getParentElement($element, $parentClass, $conditions = NULL, $orderBy = NULL)
 {
     $logInstance = LogTool::getInstance();
     $logInstance->logDebug('Gets ' . $element->getElementClass() . ' parent ' . $parentClass . ' element...');
     // Gets parent element by type and id
     $parentIdFieldName = DatabaseFactory::getParentIdColumnName($parentClass);
     // Split parent class name for search like people_mother, to get mother_people_id field from People table
     $parentClassNameList = StringTool::split(ElementFactory::TABLE_FIELD_SEPARATOR, $parentClass);
     $parentClass = end($parentClassNameList);
     $parentId = $element->{$parentIdFieldName};
     // Parent element id is null
     if ($parentId === NULL) {
         throw new ElementException('Cannot get ' . $parentClass . ' parent for ' . $element->getElementClass() . ' with id #' . $element->id . ': ' . $parentIdFieldName . ' is NULL');
     }
     return ElementFactory::getElement($parentClass, $parentId, $conditions, $orderBy);
 }