Пример #1
0
 /**
  * @param string $fieldName
  * @param FieldDescription $fieldInfo
  * @param bool $fieldValue
  * @param bool $fieldProperties
  * @return \DOMElement
  */
 protected function createField($fieldName, FieldDescription $fieldInfo, $fieldValue = false, $fieldProperties = false)
 {
     foreach (['nullable', 'pattern', 'message', 'tabName', 'tableName', 'sort', 'customField', 'url', 'separator', 'default'] as $propertyName) {
         $fieldInfo->removeProperty($propertyName);
     }
     return parent::createField($fieldName, $fieldInfo, $fieldValue, $fieldProperties);
 }
Пример #2
0
 /**
  * Create field.
  *
  * @param mixed $initialValue Initial value.
  */
 public function createField($initialValue = NULL)
 {
     if ($this->isActive) {
         $field = new Field('tags');
         if (is_null($initialValue)) {
             if (!$this->data->isEmpty() && ($currentData = $this->data->getFieldByName($this->pk->getName()))) {
                 $field->setData($this->pull($currentData->getData(), $this->tableName));
                 $this->data->addField($field);
             }
         } else {
             for ($i = 0; $i < count(E()->getLanguage()->getLanguages()); $i++) {
                 $field->setRowData($i, is_array($initialValue) ? $initialValue : [$initialValue]);
             }
             $this->data->addField($field);
         }
     }
 }
Пример #3
0
 /**
  * считаем комментарии для загруженных постов
  * 
  * @param array $data
  */
 protected function loadCommentCount(&$data)
 {
     $comments = Comments::createInsatceFor($this->getTableName());
     if ($commentCount = $comments->getCountByIds(simplifyDBResult($data, 'post_id'))) {
         foreach ($data as &$item) {
             if (key_exists($item['post_id'], $commentCount)) {
                 $item['comments_num'] = $commentCount[$item['post_id']];
             } else {
                 $item['comments_num'] = 0;
             }
         }
         $fd = new FieldDescription('comments_num');
         $fd->setType(FieldDescription::FIELD_TYPE_INT);
         $this->getDataDescription()->addFieldDescription($fd);
     }
 }
Пример #4
0
 /**
  * Load element from XML description.
  *
  * @param \SimpleXMLElement $description Element description.
  *
  * @throws SystemException 'ERR_DEV_NO_CONTROL_TYPE'
  */
 public function loadFromXml(\SimpleXMLElement $description)
 {
     if (!isset($description['type'])) {
         throw new SystemException('ERR_DEV_NO_CONTROL_TYPE', SystemException::ERR_DEVELOPER);
     }
     $attr = $description->attributes();
     $this->setAttribute('mode', FieldDescription::computeRights($this->getToolbar()->getComponent()->document->getRights(), !is_null($attr['ro_rights']) ? (int) $attr['ro_rights'] : NULL, !is_null($attr['fc_rights']) ? (int) $attr['fc_rights'] : NULL));
     unset($attr['ro_rights']);
     unset($attr['fc_rights']);
     foreach ($attr as $key => $value) {
         if (isset($this->{$key})) {
             $this->{$key} = (string) $value;
         } else {
             $this->setAttribute($key, (string) $value);
         }
     }
 }
Пример #5
0
 /**
  * Intersect the configuration meta-data with meta-data from data base.
  * @param FieldDescription $configFieldDescription Configuration meta-data.
  * @param FieldDescription $dbFieldDescription Meta-data from data base.
  * @return FieldDescription
  */
 public static function intersect(FieldDescription $configFieldDescription, FieldDescription $dbFieldDescription)
 {
     $type = $configFieldDescription->getType();
     $mode = $configFieldDescription->getMode();
     if (!is_null($av = $configFieldDescription->getAvailableValues())) {
         $dbFieldDescription->setAvailableValues($av);
     }
     if ($dbFieldDescription->getPropertyValue('index') == 'PRI') {
         $dbFieldDescription->setType(FieldDescription::FIELD_TYPE_HIDDEN);
     }
     if (!is_null($type)) {
         $dbFieldDescription->setProperty('origType', $dbFieldDescription->getType());
         //меняем тип
         $dbFieldDescription->setType($type);
     }
     if (!is_null($mode)) {
         $dbFieldDescription->setMode($mode);
     }
     $dbFieldDescription->isMultilanguage = $configFieldDescription->isMultilanguage || $dbFieldDescription->isMultilanguage();
     $properties = array_unique(array_merge($configFieldDescription->getPropertyNames(), $dbFieldDescription->getPropertyNames()));
     foreach ($properties as $propertyName) {
         $propertyValue = $configFieldDescription->getPropertyValue($propertyName);
         if (!is_null($propertyValue) && !($propertyName == 'title' && $propertyValue == 'FIELD_' . self::EMPTY_FIELD_NAME)) {
             $dbFieldDescription->setProperty($propertyName, $propertyValue);
         }
     }
     return $dbFieldDescription;
 }
Пример #6
0
 /**
  * Create field.
  * It is used in feeds and their derivatives.
  *
  * @param bool|string $mapFieldName Map field name.
  * @param bool $returnOnlyFirstAttachment Defines whether only the first attachment should be returned.
  * @param bool|array $mapValue Map values.
  */
 public function createField($mapFieldName = false, $returnOnlyFirstAttachment = false, $mapValue = false)
 {
     if ($this->isActive && !$this->data->isEmpty()) {
         if (!$mapFieldName) {
             $mapFieldName = $this->pk->getName();
         }
         if (!$mapValue) {
             if (!($f = $this->data->getFieldByName($mapFieldName))) {
                 return;
             }
             $mapValue = $f->getData();
         }
         $mapTableName = $this->tableName;
         //@todo в принципе имя филда можеть быть вычислено через ColumnInfo
         $f = new Field('attachments');
         $this->data->addField($f);
         if (!is_array($mapValue)) {
             $mapValue = [$mapValue];
         }
         if ($filteredMapValue = array_filter(array_values($mapValue))) {
             $langMapTableName = $this->dbh->getTranslationTablename($mapTableName);
             $columns = $this->dbh->getColumnsInfo($mapTableName);
             $prefix = '';
             foreach ($columns as $cname => $col) {
                 if (isset($col['index']) && $col['index'] == 'PRI') {
                     $prefix = str_replace('_id', '', $cname);
                 }
             }
             if ($langMapTableName) {
                 $lang_columns = $this->dbh->getColumnsInfo($langMapTableName);
                 $lang_pk = false;
                 foreach ($lang_columns as $cname => $col) {
                     if (isset($col['index']) && $col['index'] == 'PRI' && $cname != 'lang_id') {
                         $lang_pk = $cname;
                     }
                 }
             }
             $additional_fields = [];
             foreach ($columns as $cname => $col) {
                 if ($cname != 'session_id' && (empty($col['index']) or $col['index'] != 'PRI' and empty($col['key']['tableName']))) {
                     $new_cname = str_replace($prefix . '_', '', $cname);
                     if ($new_cname != 'order_num') {
                         $additional_fields[$cname] = $new_cname;
                     }
                 }
             }
             if ($langMapTableName) {
                 foreach ($lang_columns as $cname => $col) {
                     if (empty($col['index']) or $col['index'] != 'PRI') {
                         $new_cname = str_replace($prefix . '_', '', $cname);
                         if ($new_cname != 'name') {
                             $additional_fields[$cname] = $new_cname;
                         }
                     }
                 }
             }
             $request = 'SELECT spu.' . $mapFieldName . ',spu.upl_id as id, spu.*, ' . 'upl_path as file, upl_name as name, upl_title as title, upl_width as width, upl_height as height, TIME_FORMAT(upl_duration, "%i:%s") as duration,
                         upl_internal_type as type,upl_mime_type as mime, upl_data as data, ' . 'upl_is_mp4 as is_mp4, upl_is_webm as is_webm, upl_is_flv as is_flv ' . ($langMapTableName && $lang_pk ? ', spt.*' : '') . 'FROM ' . self::ATTACH_TABLENAME . ' su ' . 'LEFT JOIN `' . $mapTableName . '` spu ON spu.upl_id = su.upl_id ' . ($langMapTableName && $lang_pk ? 'LEFT JOIN `' . $langMapTableName . '` spt ON spu.' . $lang_pk . ' = spt.' . $lang_pk . ' AND spt.lang_id = ' . E()->getDocument()->getLang() : '') . ' WHERE ' . $mapFieldName . ' IN (' . implode(',', $filteredMapValue) . ') AND (su.upl_is_ready=1) AND (su.upl_is_active = 1)';
             // получаем имя колонки _order_num и сортируем по этому полю, если оно есть
             if ($columns) {
                 foreach ($columns as $col => $colInfo) {
                     if (strpos($col, '_order_num') !== false) {
                         $request .= ' ORDER BY ' . $col;
                     }
                 }
             }
             $images = $this->dbh->select($request);
             if ($images) {
                 foreach ($images as $row) {
                     $repoPath = E()->FileRepoInfo->getRepositoryRoot($row['file']);
                     $row['secure'] = E()->getConfigValue('repositories.ftp.' . $repoPath . '.secure', 0) ? true : false;
                     // делаем преобразование имен из $additional_fiels (отрезаем prefix)
                     if ($additional_fields) {
                         foreach ($additional_fields as $old_field => $new_field) {
                             if (array_key_exists($old_field, $row)) {
                                 $val = $row[$old_field];
                                 unset($row[$old_field]);
                                 $row[$new_field] = $val;
                             }
                         }
                     }
                     $mapID = $row[$mapFieldName];
                     if ($returnOnlyFirstAttachment && isset($imageData[$mapID])) {
                         continue;
                     }
                     if (!isset($imageData[$mapID])) {
                         $imageData[$mapID] = [];
                     }
                     array_push($imageData[$mapID], $row);
                 }
                 list($uploadsPK) = array_keys($columns);
                 for ($i = 0; $i < sizeof($mapValue); $i++) {
                     if (isset($imageData[$mapValue[$i]])) {
                         if ($this->addOG) {
                             foreach ($imageData[$mapValue[$i]] as $row) {
                                 E()->getOGObject()->addImage($row['file']);
                             }
                             $attachment = $imageData[$mapValue[$i]];
                             if (isset($attachment[0]) && $attachment[0]['type'] === 'video') {
                                 //inspect($attachment);
                                 E()->getOGObject()->setVideo($attachment[0]['file'], $attachment[0]['duration'], $attachment[0]['mime'], $attachment[0]['width'], $attachment[0]['height']);
                             }
                         }
                         $builder = new SimplestBuilder();
                         $localData = new Data();
                         $localData->load($imageData[$mapValue[$i]]);
                         $dataDescription = new DataDescription();
                         $fd = new FieldDescription('id');
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription($uploadsPK);
                         $fd->setType(FieldDescription::FIELD_TYPE_INT);
                         $fd->setProperty('key', true);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('file');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $base = pathinfo($imageData[$mapValue[$i]][0]['file'], PATHINFO_DIRNAME) . '/' . pathinfo($imageData[$mapValue[$i]][0]['file'], PATHINFO_FILENAME);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('type');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('duration');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('width');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('height');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('mime');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('data');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd = new FieldDescription('title');
                         $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                         $dataDescription->addFieldDescription($fd);
                         $fd_name = new FieldDescription('name');
                         $dataDescription->addFieldDescription($fd_name);
                         $fd = new FieldDescription('secure');
                         $fd->setType(FieldDescription::FIELD_TYPE_HIDDEN);
                         $dataDescription->addFieldDescription($fd);
                         $playlist = [];
                         foreach (['mp4', 'webm', 'flv'] as $fileType) {
                             if ($imageData[$mapValue[$i]][0]['is_' . $fileType] == '1') {
                                 $playlist[] = ['id' => $base . '.' . $fileType, 'type' => $fileType];
                             }
                         }
                         if ($playlist && count($playlist) > 1) {
                             $fd = new FieldDescription('playlist');
                             $fd->setType(FieldDescription::FIELD_TYPE_SELECT);
                             $fd->loadAvailableValues($playlist, 'id', 'id');
                             $dataDescription->addFieldDescription($fd);
                         }
                         // дополнительные поля из основной и языковой таблицы _uploads
                         foreach ($additional_fields as $new_name) {
                             if ($new_name != 'name') {
                                 $fd = new FieldDescription($new_name);
                                 $fd->setType(FieldDescription::FIELD_TYPE_STRING);
                                 $dataDescription->addFieldDescription($fd);
                             }
                         }
                         $builder->setData($localData);
                         $builder->setDataDescription($dataDescription);
                         if ($this->dbh->getTagsTablename($mapTableName)) {
                             $tm = new TagManager($dataDescription, $localData, $mapTableName);
                             $tm->createFieldDescription();
                             $tm->createField();
                         }
                         $builder->build();
                         $f->setRowData($i, $builder->getResult());
                     }
                 }
             }
         }
     }
 }
Пример #7
0
 function __toString()
 {
     $dbh = E()->getDB();
     $tableName = $this->getAttribute('tableName');
     $fieldName = $this->name;
     $values = $this->value;
     if (!is_array($values)) {
         $values = [$values];
     }
     if (!$dbh->tableExists($tableName) || !($tableInfo = $dbh->getColumnsInfo($tableName)) || !isset($tableInfo[$fieldName])) {
         throw new SystemException('ERR_BAD_FILTER_DATA', SystemException::ERR_CRITICAL, $tableName);
     }
     if (is_array($tableInfo[$fieldName]['key'])) {
         $fkTranslationTableName = $dbh->getTranslationTablename($tableInfo[$fieldName]['key']['tableName']);
         $fkTableName = $fkTranslationTableName ? $fkTranslationTableName : $tableInfo[$fieldName]['key']['tableName'];
         $fkValueField = substr($fkKeyName = $tableInfo[$fieldName]['key']['fieldName'], 0, strrpos($fkKeyName, '_')) . '_name';
         $fkTableInfo = $dbh->getColumnsInfo($fkTableName);
         if (!isset($fkTableInfo[$fkValueField])) {
             $fkValueField = $fkKeyName;
         }
         if ($res = $dbh->getColumn($fkTableName, $fkKeyName, $fkTableName . '.' . $fkValueField . ' ' . call_user_func_array('sprintf', array_merge([FilterConditionConverter::getInstance()[$this->condition]['condition']], $values)) . ' ')) {
             return $this->operator . ' ' . $tableName . '.' . $fieldName . ' IN (' . implode(',', $res) . ')';
         } else {
             return $this->operator . ' ' . ' FALSE ';
         }
     } else {
         $fieldType = FieldDescription::convertType($tableInfo[$fieldName]['type'], $fieldName, $tableInfo[$fieldName]['length'], $tableInfo[$fieldName]);
         if ($fieldType == FieldDescription::FIELD_TYPE_BOOL) {
             $this->value = '';
         } elseif (!$this->value) {
             return '';
         }
         if (in_array($this->condition, ['like', 'notlike']) && in_array($fieldType, [FieldDescription::FIELD_TYPE_DATE, FieldDescription::FIELD_TYPE_DATETIME])) {
             if ($this->condition == 'like') {
                 $this->condition = '=';
             } else {
                 $this->condition = '!=';
             }
         }
         $fieldName = ($tableName ? $tableName . '.' : '') . $fieldName;
         if ($fieldType == FieldDescription::FIELD_TYPE_DATETIME) {
             $fieldName = 'DATE(' . $fieldName . ')';
         }
         $conditionPatterns = FilterConditionConverter::getInstance()[$this->condition]['condition'];
         if (in_array($fieldType, [FieldDescription::FIELD_TYPE_DATETIME, FieldDescription::FIELD_TYPE_DATE])) {
             $conditionPatterns = str_replace('\'%s\'', 'DATE(\'%s\')', $conditionPatterns);
         }
         $r = $this->operator . ' ' . $fieldName . ' ' . call_user_func_array('sprintf', array_merge([$conditionPatterns], $values)) . ' ';
         return $r;
     }
 }
Пример #8
0
 /**
  * Intersect this data description with other object data description.
  *
  * @param DataDescription $otherDataDescr Other data description.
  * @return DataDescription
  */
 public function intersect(DataDescription $otherDataDescr)
 {
     $result = false;
     if ($this->isEmpty()) {
         $result = $otherDataDescr;
     } else {
         // проходимся по описаниям полей текущего объекта
         foreach ($this->fieldDescriptions as $fieldName => $fieldDescription) {
             // если существует описание из БД - пересекаем с ним
             if ($otherDataDescr->getFieldDescriptionByName($fieldName)) {
                 $this->fieldDescriptions[$fieldName] = FieldDescription::intersect($this->fieldDescriptions[$fieldName], $otherDataDescr->getFieldDescriptionByName($fieldName));
             } else {
                 $this->getFieldDescriptionByName($fieldName)->setProperty('customField', 'customField');
             }
         }
         $result = $this;
     }
     return $result;
 }