Пример #1
0
 /**
  * @access protected
  * @return array
  */
 protected function defineAttributes()
 {
     $requiredTitle = isset($this->_requiredFields) && in_array('title', $this->_requiredFields);
     $attributes = array('id' => AttributeType::Number, 'elementId' => AttributeType::Number, 'locale' => AttributeType::Locale, 'title' => array(AttributeType::String, 'required' => $requiredTitle));
     if (Craft::isInstalled() && !craft()->isConsole()) {
         $allFields = craft()->fields->getAllFields();
         foreach ($allFields as $field) {
             $fieldType = craft()->fields->populateFieldType($field);
             if ($fieldType) {
                 $attributeConfig = $fieldType->defineContentAttribute();
             }
             // Default to Mixed
             if (!$fieldType || !$attributeConfig) {
                 $attributeConfig = AttributeType::Mixed;
             }
             $attributeConfig = ModelHelper::normalizeAttributeConfig($attributeConfig);
             $attributeConfig['label'] = $field->name;
             if (isset($this->_requiredFields) && in_array($field->id, $this->_requiredFields)) {
                 $attributeConfig['required'] = true;
             }
             $attributes[$field->handle] = $attributeConfig;
         }
     }
     return $attributes;
 }
Пример #2
0
 public static function updateItem($item, $data, $columns = array())
 {
     if (empty($item)) {
         return -1;
     }
     $result = ModelHelper::fillItem($item, $data, $columns);
     if ($result == 1) {
         $result = $item->update();
         return $result ? 1 : 0;
     }
     return $result;
 }
 protected function _populateModelsFromArray(array $array)
 {
     $models = array();
     foreach ($array as $row) {
         $model = json_decode($row['model'], true);
         $model = ModelHelper::expandModelsInArray($model);
         $class = $model['service']['__class__'];
         $model->service = new $class($model->service);
         $model->queueId = $row['id'];
         $models[] = new Postmaster_TransportModel($model);
     }
     return $models;
 }
 /**
  * Define database column
  *
  * @return mixed
  */
 public function defineContentAttribute()
 {
     $settings = $this->getSettings();
     // It hasn't always been a settings, so default to Text if it's not set.
     if (!$settings->getAttribute('columnType')) {
         return array(AttributeType::String, 'column' => ColumnType::Text);
     }
     if ($settings->columnType === 'number') {
         $attribute = ModelHelper::getNumberAttributeConfig(null, null, $settings->decimals);
         $attribute['default'] = 0;
         return $attribute;
     }
     return array(AttributeType::String, 'column' => $settings->columnType);
 }
Пример #5
0
 /**
  * @inheritDoc IFieldType::defineContentAttribute()
  *
  * @return mixed
  */
 public function defineContentAttribute()
 {
     $attribute = ModelHelper::getNumberAttributeConfig($this->settings->min, $this->settings->max, $this->settings->decimals);
     $attribute['default'] = 0;
     return $attribute;
 }
 /**
  * Checks whether the given field is saving data into a textual column, and saves it accordingly.
  *
  * @param FieldModel $field
  * @param string     $fieldColumnPrefix
  *
  * @return bool
  */
 private function _checkField(FieldModel $field, $fieldColumnPrefix)
 {
     if ($field->type == 'Matrix') {
         $this->_matrixFieldIds[] = $field->id;
     } else {
         $fieldType = $field->getFieldType();
         if ($fieldType) {
             $attributeConfig = $fieldType->defineContentAttribute();
             if ($attributeConfig && $attributeConfig != AttributeType::Number) {
                 $attributeConfig = ModelHelper::normalizeAttributeConfig($attributeConfig);
                 if ($attributeConfig['type'] == AttributeType::String) {
                     $this->_textColumns[] = $fieldColumnPrefix . $field->handle;
                 }
             }
         }
     }
 }
Пример #7
0
 /**
  * Saves a field.
  *
  * @param FieldModel $field
  * @throws \Exception
  * @return bool
  */
 public function saveField(FieldModel $field)
 {
     $fieldRecord = $this->_getFieldRecordById($field->id);
     $isNewField = $fieldRecord->isNewRecord();
     if (!$isNewField) {
         $fieldRecord->oldHandle = $fieldRecord->handle;
     }
     $fieldRecord->groupId = $field->groupId;
     $fieldRecord->name = $field->name;
     $fieldRecord->handle = $field->handle;
     $fieldRecord->instructions = $field->instructions;
     $fieldRecord->translatable = $field->translatable;
     $fieldRecord->type = $field->type;
     $fieldType = $this->populateFieldType($field);
     $preppedSettings = $fieldType->prepSettings($field->settings);
     $fieldRecord->settings = $field->settings = $preppedSettings;
     $fieldType->setSettings($preppedSettings);
     $fieldType->model = $field;
     $recordValidates = $fieldRecord->validate();
     $settingsValidate = $fieldType->getSettings()->validate();
     if ($recordValidates && $settingsValidate) {
         $transaction = craft()->db->beginTransaction();
         try {
             $fieldType->onBeforeSave();
             $fieldRecord->save(false);
             // Now that we have a field ID, save it on the model
             if (!$field->id) {
                 $field->id = $fieldRecord->id;
             }
             // Create/alter the content table column
             $column = $fieldType->defineContentAttribute();
             if ($column) {
                 $column = ModelHelper::normalizeAttributeConfig($column);
                 if ($isNewField) {
                     craft()->db->createCommand()->addColumn('content', $field->handle, $column);
                 } else {
                     // Existing field going from a field that did not define any content attributes to one that does.
                     if (!craft()->db->schema->columnExists('content', $fieldRecord->oldHandle)) {
                         craft()->db->createCommand()->addColumn('content', $field->handle, $column);
                     } else {
                         // Existing field that already had a column defined, just altering it.
                         craft()->db->createCommand()->alterColumn('content', $fieldRecord->oldHandle, $column, $field->handle);
                     }
                 }
             } else {
                 // Did the old field have a column we need to remove?
                 if (!$isNewField) {
                     if ($fieldRecord->oldHandle && craft()->db->schema->columnExists('content', $fieldRecord->oldHandle)) {
                         craft()->db->createCommand()->dropColumn('content', $fieldRecord->oldHandle);
                     }
                 }
             }
             $fieldType->onAfterSave();
             $transaction->commit();
         } catch (\Exception $e) {
             $transaction->rollBack();
             throw $e;
         }
         return true;
     } else {
         $field->addErrors($fieldRecord->getErrors());
         $field->addSettingErrors($fieldType->getSettings()->getErrors());
         return false;
     }
 }
Пример #8
0
 /**
  * Returns the attribute labels.
  *
  * @return array
  */
 public function attributeLabels()
 {
     return ModelHelper::getAttributeLabels($this);
 }
 /**
  * Saves a field.
  *
  * @param FieldModel $field
  * @param bool       $validate
  *
  * @throws \Exception
  * @return bool
  */
 public function saveField(FieldModel $field, $validate = true)
 {
     if (!$validate || $this->validateField($field)) {
         $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
         try {
             $field->context = craft()->content->fieldContext;
             $fieldRecord = $this->_getFieldRecord($field);
             $isNewField = $fieldRecord->isNewRecord();
             $fieldRecord->groupId = $field->groupId;
             $fieldRecord->name = $field->name;
             $fieldRecord->handle = $field->handle;
             $fieldRecord->context = $field->context;
             $fieldRecord->instructions = $field->instructions;
             $fieldRecord->translatable = $field->translatable;
             $fieldRecord->type = $field->type;
             // Get the field type
             $fieldType = $field->getFieldType();
             // Give the field type a chance to prep the settings from post
             $preppedSettings = $fieldType->prepSettings($field->settings);
             // Set the prepped settings on the FieldRecord, FieldModel, and the field type
             $fieldRecord->settings = $field->settings = $preppedSettings;
             $fieldType->setSettings($preppedSettings);
             if ($fieldRecord->settings instanceof BaseModel) {
                 // Call getAttributes() without passing 'true' so the __model__ isn't saved
                 $fieldRecord->settings = $fieldRecord->settings->getAttributes();
             }
             $fieldType->onBeforeSave();
             $fieldRecord->save(false);
             // Now that we have a field ID, save it on the model
             if ($isNewField) {
                 $field->id = $fieldRecord->id;
             }
             // Create/alter the content table column
             $columnType = $fieldType->defineContentAttribute();
             $contentTable = craft()->content->contentTable;
             $oldColumnName = $this->oldFieldColumnPrefix . $fieldRecord->getOldHandle();
             $newColumnName = craft()->content->fieldColumnPrefix . $field->handle;
             if ($columnType) {
                 $columnType = ModelHelper::normalizeAttributeConfig($columnType);
                 // Make sure we're working with the latest data in the case of a renamed field.
                 craft()->db->schema->refresh();
                 if (craft()->db->columnExists($contentTable, $oldColumnName)) {
                     craft()->db->createCommand()->alterColumn($contentTable, $oldColumnName, $columnType, $newColumnName);
                 } else {
                     if (craft()->db->columnExists($contentTable, $newColumnName)) {
                         craft()->db->createCommand()->alterColumn($contentTable, $newColumnName, $columnType);
                     } else {
                         craft()->db->createCommand()->addColumn($contentTable, $newColumnName, $columnType);
                     }
                 }
             } else {
                 // Did the old field have a column we need to remove?
                 if (!$isNewField) {
                     if ($fieldRecord->getOldHandle() && craft()->db->columnExists($contentTable, $oldColumnName)) {
                         craft()->db->createCommand()->dropColumn($contentTable, $oldColumnName);
                     }
                 }
             }
             if (!$isNewField) {
                 // Save the old field handle on the model in case the field type needs to do something with it.
                 $field->oldHandle = $fieldRecord->getOldHandle();
                 unset($this->_fieldsByContextAndHandle[$field->context][$field->oldHandle]);
             }
             // Cache it
             $this->_fieldsById[$field->id] = $field;
             $this->_fieldsByContextAndHandle[$field->context][$field->handle] = $field;
             unset($this->_allFieldsInContext[$field->context]);
             unset($this->_fieldsWithContent[$field->context]);
             $fieldType->onAfterSave();
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } catch (\Exception $e) {
             if ($transaction !== null) {
                 $transaction->rollback();
             }
             throw $e;
         }
         return true;
     } else {
         return false;
     }
 }
Пример #10
0
 /**
  * Saves a content model to the database.
  *
  * @param ContentModel $content
  *
  * @return bool
  */
 private function _saveContentRow(ContentModel $content)
 {
     $values = array('id' => $content->id, 'elementId' => $content->elementId, 'locale' => $content->locale);
     $excludeColumns = array_keys($values);
     $excludeColumns = array_merge($excludeColumns, array_keys(DbHelper::getAuditColumnConfig()));
     $fullContentTableName = craft()->db->addTablePrefix($this->contentTable);
     $contentTableSchema = craft()->db->schema->getTable($fullContentTableName);
     foreach ($contentTableSchema->columns as $columnSchema) {
         if ($columnSchema->allowNull && !in_array($columnSchema->name, $excludeColumns)) {
             $values[$columnSchema->name] = null;
         }
     }
     // If the element type has titles, than it's required and will be set. Otherwise, no need to include it (it
     // might not even be a real column if this isn't the 'content' table).
     if ($content->title) {
         $values['title'] = $content->title;
     }
     foreach (craft()->fields->getFieldsWithContent() as $field) {
         $handle = $field->handle;
         $value = $content->{$handle};
         $values[$this->fieldColumnPrefix . $field->handle] = ModelHelper::packageAttributeValue($value, true);
     }
     $isNewContent = !$content->id;
     if (!$isNewContent) {
         $affectedRows = craft()->db->createCommand()->update($this->contentTable, $values, array('id' => $content->id));
     } else {
         $affectedRows = craft()->db->createCommand()->insert($this->contentTable, $values);
     }
     if ($affectedRows) {
         if ($isNewContent) {
             // Set the new ID
             $content->id = craft()->db->getLastInsertID();
         }
         // Fire an 'onSaveContent' event
         $this->onSaveContent(new Event($this, array('content' => $content, 'isNewContent' => $isNewContent)));
         return true;
     } else {
         return false;
     }
 }
Пример #11
0
 public function post($segments)
 {
     $action_id = $segments[0];
     switch ($action_id) {
         case 'eval-code':
             $code = $_POST['code'];
             include COMPONENT_ROOT . '/tool/eval-code.php';
             break;
         case 'create-table':
             $table_name = $_POST['table_name'];
             $column_array = array();
             foreach ($_POST as $key => $value) {
                 switch ($key) {
                     case 'table_name':
                     case 'default_value_radio':
                         break;
                     default:
                         $column_array[$key] = $value;
                         break;
                 }
                 // end switch ($key)
             }
             // end foreach ($_POST as $key => $value)
             $create_table = ModelHelper::createTable($table_name, $column_array);
             $create_class = ModelHelper::createClassFile($table_name, $column_array);
             $create_class_god = ModelHelper::createClassGodFile($table_name);
             if ($create_table && $create_class && $create_class_god) {
                 ResponseMessenger::json('success');
             } else {
                 // end if ($create_table && $create_class && $create_class_god)
                 ResponseMessenger::json('fail');
             }
             // end if ($create_table && $create_class && $create_class_god) else
             break;
         case 'export-table':
             $table_list = $_POST['table_list'];
             $table_array = explode(',', $table_list);
             $success_table_array = array();
             $fail_table_array = array();
             foreach ($table_array as $table_name) {
                 if (ModelHelper::exportTable($table_name)) {
                     array_push($success_table_array, $table_name);
                 } else {
                     // end if (ModelHelper::exportTable($table_name))
                     array_push($fail_table_array, $table_name);
                 }
                 // end if (ModelHelper::exportTable($table_name)) else
             }
             // end foreach ($table_array as $table_name)
             $message = '成功匯出 ' . count($success_table_array) . ' 個 Table';
             $parameter = array("success" => implode(', ', $success_table_array), "fail" => implode(', ', $fail_table_array));
             ResponseMessenger::json('success', $message, $parameter);
             break;
         case 'import-table':
             $table_list = $_POST['table_list'];
             $table_array = explode(',', $table_list);
             $success_table_array = array();
             $fail_table_array = array();
             foreach ($table_array as $table_name) {
                 if (ModelHelper::importTable($table_name)) {
                     array_push($success_table_array, $table_name);
                 } else {
                     // end if (ModelHelper::importTable($table_name))
                     array_push($fail_table_array, $table_name);
                 }
                 // end if (ModelHelper::importTable($table_name)) else
             }
             // end foreach ($table_array as $table_name)
             $message = '成功匯入 ' . count($success_table_array) . ' 個 Table';
             $parameter = array("success" => implode(', ', $success_table_array), "fail" => implode(', ', $fail_table_array));
             ResponseMessenger::json('success', $message, $parameter);
             break;
         case 'export-data':
             $table_list = $_POST['table_list'];
             $table_array = explode(',', $table_list);
             $success_table_array = array();
             $fail_table_array = array();
             foreach ($table_array as $table_name) {
                 if (ModelHelper::exportData($table_name)) {
                     array_push($success_table_array, $table_name);
                 } else {
                     // end if (ModelHelper::exportData($table_name))
                     array_push($fail_table_array, $table_name);
                 }
                 // end if (ModelHelper::exportData($table_name)) else
             }
             // end foreach ($table_array as $table_name)
             $message = '成功匯出 ' . count($success_table_array) . ' 個 Table';
             $parameter = array("success" => implode(', ', $success_table_array), "fail" => implode(', ', $fail_table_array));
             ResponseMessenger::json('success', $message, $parameter);
             break;
         case 'sync-data':
             $table_list = $_POST['table_list'];
             $table_array = explode(',', $table_list);
             $success_table_array = array();
             $fail_table_array = array();
             foreach ($table_array as $table_name) {
                 if (ModelHelper::syncData($table_name)) {
                     array_push($success_table_array, $table_name);
                 } else {
                     // end if (ModelHelper::syncData($table_name))
                     array_push($fail_table_array, $table_name);
                 }
                 // end if (ModelHelper::syncData($table_name)) else
             }
             // end foreach ($table_array as $table_name)
             $message = '成功同步 ' . count($success_table_array) . ' 個 Table';
             $parameter = array("success" => implode(', ', $success_table_array), "fail" => implode(', ', $fail_table_array));
             ResponseMessenger::json('success', $message, $parameter);
             break;
         case 'arrange-database':
             include COMPONENT_ROOT . '/tool/arrange-database.php';
             break;
         default:
             echo 'Undefined post action';
             break;
     }
     // end switch ($action_id)
 }
Пример #12
0
 /**
  * Saves a content model to the database.
  *
  * @param ContentModel $content
  * @param bool         $validate Whether to call the model's validate() function first.
  * @return bool
  */
 public function saveContent(ContentModel $content, $validate = true)
 {
     if (!$validate || $content->validate()) {
         $values = array('id' => $content->id, 'elementId' => $content->elementId, 'locale' => $content->locale, 'title' => $content->title);
         $allFields = craft()->fields->getAllFields();
         foreach ($allFields as $field) {
             $fieldType = craft()->fields->populateFieldType($field);
             // Only include this value if the content table has a column for it
             if ($fieldType && $fieldType->defineContentAttribute()) {
                 $handle = $field->handle;
                 $value = $content->{$handle};
                 $values[$field->handle] = ModelHelper::packageAttributeValue($value, true);
             }
         }
         $isNewContent = !$content->id;
         if (!$isNewContent) {
             $affectedRows = craft()->db->createCommand()->update('content', $values, array('id' => $content->id));
         } else {
             $affectedRows = craft()->db->createCommand()->insert('content', $values);
             if ($affectedRows) {
                 // Set the new ID
                 $content->id = craft()->db->getLastInsertID();
             }
         }
         if ($affectedRows) {
             // Fire an 'onSaveContent' event
             $this->onSaveContent(new Event($this, array('content' => $content, 'isNewContent' => $isNewContent)));
             return true;
         }
     }
     return false;
 }
Пример #13
0
 /**
  * Get all result rows as an array.
  *
  * @return array
  */
 public function getAll()
 {
     $res = array();
     if (empty($this->_result)) {
         return json_encode(array());
     }
     foreach ($this->_result as $row) {
         $row = ModelHelper::removeNumericKeys($row);
         $res[] = $row;
     }
     return json_encode($res);
 }
     $column_list = $db_obj->getTableColumns($table_name);
     echo "\tCreate class file...";
     if (ModelHelper::createClassFile($table_name, $column_list)) {
         echo 'ok' . PHP_EOL;
     } else {
         echo 'fail' . PHP_EOL;
     }
 }
 // sync class god file
 $class_path = CLASS_ROOT . '/model/' . $class_name . 'God.php';
 echo "\tCreate class god file...";
 if (file_exists($class_path)) {
     echo "\tClass god file exists" . PHP_EOL;
 } else {
     $column_list = $db_obj->getTableColumns($table_name);
     if (ModelHelper::createClassGodFile($table_name)) {
         echo 'ok' . PHP_EOL;
     } else {
         echo 'fail' . PHP_EOL;
     }
 }
 // sync table data
 $sql_path = DATA_SQL_ROOT . '/' . $table_name . '.sql';
 if (file_exists($sql_path)) {
     echo "\tTruncate `{$table_name}` data...";
     $sql = "TRUNCATE {$table_name}";
     if ($db_obj->query($sql)) {
         echo 'ok' . PHP_EOL;
         echo "\t\tImport `{$table_name}` data...";
         $sql = file_get_contents($sql_path);
         if ($db_obj->query($sql)) {
Пример #15
0
 /**
  * @inheritDoc BaseModel::defineAttributes()
  *
  * @return array
  */
 protected function defineAttributes()
 {
     $requiredTitle = isset($this->_requiredFields) && in_array('title', $this->_requiredFields);
     $attributes = array('id' => AttributeType::Number, 'elementId' => AttributeType::Number, 'locale' => array(AttributeType::Locale, 'default' => craft()->i18n->getPrimarySiteLocaleId()), 'title' => array(AttributeType::String, 'required' => $requiredTitle, 'maxLength' => 255, 'label' => 'Title'));
     foreach (craft()->fields->getAllFields() as $field) {
         $fieldType = $field->getFieldType();
         if ($fieldType) {
             $attributeConfig = $fieldType->defineContentAttribute();
         }
         // Default to Mixed
         if (!$fieldType || !$attributeConfig) {
             $attributeConfig = AttributeType::Mixed;
         }
         $attributeConfig = ModelHelper::normalizeAttributeConfig($attributeConfig);
         $attributeConfig['label'] = $field->name != '__blank__' ? $field->name : StringHelper::uppercaseFirst($field->handle);
         if (isset($this->_requiredFields) && in_array($field->id, $this->_requiredFields)) {
             $attributeConfig['required'] = true;
         }
         $attributes[$field->handle] = $attributeConfig;
     }
     return $attributes;
 }
Пример #16
0
 /**
  * Returns the content attribute config.
  *
  * @return mixed
  */
 public function defineContentAttribute()
 {
     return ModelHelper::getNumberAttributeConfig($this->settings->min, $this->settings->max, $this->settings->decimals);
 }