/** * 获取集合的数据结构 * * @return array */ private function getSchema() { $schema = array('2d' => array(), 'file' => array(), 'export' => array(), 'post' => array('_id' => array('type' => '_idfield')), 'all' => array(), 'quick' => array(), 'combobox' => array('rshCollectionValueField' => '_id')); $cursor = $this->_structure->find(array('collection_id' => $this->_collection_id)); $cursor->sort(array('orderBy' => 1, '_id' => -1)); while ($cursor->hasNext()) { $row = $cursor->getNext(); $type = $row['type'] == 'filefield' ? 'file' : 'post'; $schema[$type][$row['field']] = $row; $schema['all'][$row['field']] = $row; $this->_fields[$row['field']] = true; $this->_title[$row['field']] = $row['label']; if ($row['type'] === '2dfield') { $schema['2d'][$row['field']] = $row; } if ($row['rshKey']) { $schema['combobox']['rshCollectionKeyField'] = $row['field']; } if ($row['rshValue']) { $schema['combobox']['rshCollectionValueField'] = $row['field']; } if (isset($row['isFatherField']) && $row['isFatherField']) { $this->_fatherField = $row['field']; } // 检查结构的时候,检查允许导出的字段 if (!empty($row['export'])) { $schema['export'][$row['field']] = true; } if (isset($row['isQuick']) && $row['isQuick'] && $row['type'] == 'arrayfield') { $schema['quick'][$row['field']] = $row; } if (!empty($row['rshCollection'])) { $rshCollectionStructures = $this->_structure->findAll(array('collection_id' => $this->getCollectionIdByAlias($row['rshCollection']))); if (!empty($rshCollectionStructures)) { $rshCollectionKeyField = ''; $rshCollectionValueField = '_id'; $rshCollectionValueFieldType = 'textfield'; foreach ($rshCollectionStructures as $rshCollectionStructure) { if ($rshCollectionStructure['rshKey']) { $rshCollectionKeyField = $rshCollectionStructure['field']; } if ($rshCollectionStructure['rshValue']) { $rshCollectionValueField = $rshCollectionStructure['field']; $rshCollectionValueFieldType = $rshCollectionStructure['type']; } } if (empty($rshCollectionKeyField)) { throw new \Exception('字段' . $row['field'] . '的“关联集合”的键值属性尚未设定,请检查表表结构设定'); } $this->_rshCollection[$row['rshCollection']] = array('collectionField' => $row['field'], 'rshCollectionKeyField' => $rshCollectionKeyField, 'rshCollectionValueField' => $rshCollectionValueField, 'rshCollectionValueFieldType' => $rshCollectionValueFieldType); } else { throw new \Exception('字段' . $row['field'] . '的“关联集合”的键值属性尚未设定,请检查表表结构设定'); } } } ksort($this->_title); $this->_schema = $schema; return $schema; }
/** * Run before a model is saved, used to set up slug for model. * * @param object $model Model about to be saved. * * @access public * @since 1.0 */ function beforeSave(&$model) { if (!is_array($this->settings[$model->name]['label'])) { $this->settings[$model->name]['label'] = array($this->settings[$model->name]['label']); } foreach ($this->settings[$model->name]['label'] as $field) { if (!$model->hasField($field)) { return; } } if ($model->hasField($this->settings[$model->name]['slug']) && ($this->settings[$model->name]['overwrite'] || empty($model->{$model->primaryKey}))) { $label = ''; foreach ($this->settings[$model->name]['label'] as $field) { $label .= ife(!empty($label), ' ', ''); $label .= $model->data[$model->name][$field]; } if (empty($label)) { $label = 'slug'; } // If the user submits a non-empty slug use that one, else generate a new one from the label field $slug = ''; if (isset($model->data[$model->name][$this->settings[$model->name]['slug']]) && !empty($model->data[$model->name][$this->settings[$model->name]['slug']])) { $slug = $this->_getSlug($model->data[$model->name][$this->settings[$model->name]['slug']], $this->settings[$model->name]); } else { $slug = $this->_getSlug($label, $this->settings[$model->name]); } // Find out if such a slug exists $conditions = "{$model->name}.{$this->settings[$model->name]['slug']} = '{$slug}'"; if (!empty($model->{$model->primaryKey})) { $conditions[$model->name . '.' . $model->primaryKey] = '!= ' . $model->{$model->primaryKey}; } $result = $model->findAll($conditions, array($model->primaryKey, $this->settings[$model->name]['slug']), null, null, 1, 0); $sameUrls = null; if ($result !== false && !empty($result)) { $sameUrls = Set::extract($result, '{n}.' . $model->name . '.' . $this->settings[$model->name]['slug']); } if (!empty($sameUrls)) { $begginingSlug = $slug; $index = 1; while ($index > 0) { if (!in_array($begginingSlug . $this->settings[$model->name]['separator'] . $index, $sameUrls)) { $slug = $begginingSlug . $this->settings[$model->name]['separator'] . $index; $index = -1; } $index++; } } $model->data[$model->name][$this->settings[$model->name]['slug']] = $slug; } }
/** * 删除基本数据 并返回需要删除的关联数据 * * @param object $model * @param array $ids * @return array */ private function _deleteBaseData($model = '', $ids = array()) { $criteria = new CDbCriteria(); $criteria->addInCondition('id', $ids); $criteria->addColumnCondition(array('user_id' => $this->_uid)); $res = $model->findAll($criteria); $content = array(); foreach ($res as $v) { if (in_array($v->id, $ids)) { $content[] = $v; } } $model->deleteByPk($ids); return $content; }
/** * 获取当前结合的结构 */ private function getSchema() { if ($this->_structure == null) { $this->_structure = $this->model('Project\\Model\\Structure'); } $query = array('collection_id' => $this->_collection_id); $structures = $this->_structure->findAll($query); if (!empty($structures)) { foreach ($structures as $row) { $this->_schema[] = $row['field']; } } else { throw new \Exception("你尚未定义数据结构"); } }
/** * Run before a model is saved, used to set up slug for model. * * @param object $model Model about to be saved. * * @access public * @since 1.0 */ function beforeSave(&$model) { if (!is_array($this->settings[$model->name]['label'])) { $this->settings[$model->name]['label'] = array($this->settings[$model->name]['label']); } foreach ($this->settings[$model->name]['label'] as $field) { if (!$model->hasField($field)) { return; } } if ($model->hasField($this->settings[$model->name]['slug']) && ($this->settings[$model->name]['overwrite'] || empty($model->{$model->primaryKey}))) { $label = ''; foreach ($this->settings[$model->name]['label'] as $field) { $label .= ife(!empty($label), ' ', ''); $label .= $model->data[$model->name][$field]; } if (empty($label)) { $label = 'slug'; } $slug = $this->_slug($label, $this->settings[$model->name]); $conditions = array($model->name . '.' . $this->settings[$model->name]['slug'] => 'LIKE ' . $slug . '%'); if (!empty($model->{$model->primaryKey})) { $conditions[$model->name . '.' . $model->primaryKey] = '!= ' . $model->{$model->primaryKey}; } $result = $model->findAll($conditions, array($model->primaryKey, $this->settings[$model->name]['slug']), null, null, 1, 0); $sameUrls = null; if ($result !== false && !empty($result)) { $sameUrls = Set::extract($result, '{n}.' . $model->name . '.' . $this->settings[$model->name]['slug']); } if (!empty($sameUrls)) { $begginingSlug = $slug; $index = 1; while ($index > 0) { if (!in_array($begginingSlug . $this->settings[$model->name]['separator'] . $index, $sameUrls)) { $slug = $begginingSlug . $this->settings[$model->name]['separator'] . $index; $index = -1; } $index++; } } $model->data[$model->name][$this->settings[$model->name]['slug']] = $slug; } }