/** * get valuelist from filemaker layout * * @param AppModel $model * @param string $fieldName * @return mixed array or false */ function valueList(&$model, $fieldName = null) { if (empty($model->returnValueLists) || !$model->returnValueLists) { return false; } if ($fields = $model->schema()) { $valueList = array(); foreach ($fields as $_fieldName => $_schema) { if (!empty($_schema['valuelist'])) { $valueList[$_fieldName] = array(); foreach ($_schema['valuelist'] as $value) { $value = html_entity_decode($value, ENT_COMPAT, 'UTF-8'); $valueList[$_fieldName][$value] = $value; } } } if (empty($fieldName)) { return $valueList; } else { if (!empty($valueList[$fieldName])) { return $valueList[$fieldName]; } } } return false; }
/** * Check DB table for localized fields and define them if needed * * @param AppModel $model * @return bool (true if no changes were made) */ protected function _checkSchema($model) { if (empty($this->_ready[$model->alias])) { $this->_ready[$model->alias] = true; } else { return; } $fields = $this->settings[$model->alias]; if (empty($fields)) { return true; } $locales = SlConfigure::read('I18n.locales'); if (empty($locales)) { trigger_error('I18n didn\'t initilialize properly.', E_USER_ERROR); return false; } $db =& ConnectionManager::getDataSource($model->useDbConfig); $schema = $model->schema(); $alterTable = array(); // add all localized missing fields in this model foreach ($fields as $field) { foreach ($locales as $locale) { $field_lang = $field . '_' . $locale; if (empty($schema[$field_lang])) { if (empty($schema[$field])) { trigger_error("Table for {$model->alias} Model doesn't have a field named '{$field}'!", E_USER_ERROR); } $alterTable[$model->table]['add'][$field_lang] = $schema[$field]; } } } foreach ($locales as $locale) { $field_lang = '_' . $locale; if (empty($schema[$field_lang])) { $alterTable[$model->table]['add'][$field_lang] = array('type' => 'boolean', 'null' => false); } } if ($alterTable) { if (!method_exists($db, 'alterSchema')) { trigger_error("Table configuration for {$model->alias} Model could not be changed to reflect your latest language settings because its DataSource does not support altering schemas.", E_USER_ERROR); } $model->cacheSources = false; $model->cacheQueries = false; // delete cached model file clearCache("{$model->useDbConfig}_{$model->table}", 'models', ''); // execute alter table and update schema $model->query($db->alterSchema($alterTable)); $model->schema(true); // output a notice about updated DB table trigger_error("Table configuration for {$model->alias} Model has changed to reflect your latest language settings.", E_USER_NOTICE); return false; } return true; }
/** * Initiate SearchableBehavior * * @param AppModel $model * @param array $config Array of options for configuring the Searchable * Behavior settings for the given model. Keys include: * - fields - array of fields from the model to include in the search index. * If omitted, all char, varchar, string, text fields will be included. * To include data from fields in the current model, just specify the field * name. E.g. array('title') * The Searchable Behavior can include data from associated models in the * Search Index too. This is useful for say Post belongsTo Category, and you * want the Category name included in the Search Index. * To achieve this, specify the field name in the current model as the key and * the model and field in the associated model as the value. E.g. * array('category_id' => 'Category.name') * - scope - array of conditions in the form array('field' => 'value') to * apply to determine whether the record in the Search Index is active or not, * and therefore whether it should be included in the search results. If * omitted, the record in the Search Index is always active. * - name - the field to be used from the Searchable model when populating the * name field in the Search Index. This is used as the title of a search * result entry on the results page, and has the link to view the result on * it. If omitted, the displayField of the model is used. Set to false if you * don't want the title field to be populated. * - summary - the field to be used from the Searchable model when populating * the summary field in the Search Index. This is used as the summary of a * search result entry on the results page. If omitted, no field is used, and * the summary will be a series of excerpts from the Search Index data with * the search terms highlighted. * - published - the field to be used from the Searchable model when * populating the published field in the Search Index. This can be used in the * conditions array when performing the search. If omitted, no field is used, * and the published field contain NULL. * - url - array of url elements e.g. controller, action etc. If controller is * omitted, the controller version of the model is used. If action is omitted, * view is used, if there are no other non-url paramters (e.g. slug), the * Searchable Model primary key value is added to the url. */ function setup(&$model, $config = array()) { if (!is_array($config)) { $config = array($config); } // Add config to settings for given model $this->settings[$model->alias] = array_merge($this->_defaults, $config); // Normalize the fields property using default string types from the model // schema if not specified, or processing the fields config param passed in. if (empty($this->settings[$model->alias]['fields'])) { foreach ($model->schema() as $field => $info) { if (in_array($info['type'], array('text', 'varchar', 'char', 'string', 'date'))) { $this->settings[$model->alias]['fields'][$model->alias . '.' . $field] = $model->alias . '.' . $field; } } } else { // Ensure fields is in the format array(Model.field => Model.field, ...) foreach ($this->settings[$model->alias]['fields'] as $field => $modelField) { unset($this->settings[$model->alias]['fields'][$field]); if (strpos($modelField, '.') === false) { $modelField = $model->alias . '.' . $modelField; } if (is_numeric($field)) { $field = $modelField; } elseif (strpos($field, '.') === false) { $field = $model->alias . '.' . $field; } $this->settings[$model->alias]['fields'][$field] = $modelField; } } // Set 'name' to false if you don't want to populate the 'name' field if (!isset($this->settings[$model->alias]['name']) || $this->settings[$model->alias]['name'] !== false) { $this->settings[$model->alias]['name'] = $model->displayField; } // If url is not an array, make it one if (!isset($this->settings[$model->alias]['url'])) { $this->settings[$model->alias]['url'] = array(); } // Add default plugin url component of null if (!isset($this->settings[$model->alias]['url']['plugin'])) { $this->settings[$model->alias]['url']['plugin'] = null; } // Add default controller url component of controller version of model if (!isset($this->settings[$model->alias]['url']['controller'])) { $this->settings[$model->alias]['url']['controller'] = Inflector::pluralize(Inflector::underscore($model->alias)); } // Add default action of view if (!isset($this->settings[$model->alias]['url']['action'])) { $this->settings[$model->alias]['url']['action'] = 'view'; } }
/** * Dynamically set the schema to the properties the corresponding data source is expecting * when using the elastic search data source it will return the elastic mapping * * @param mixed $field (optional) * @return array */ public function schema($field = false) { if ($this->isElastic()) { if (!empty($this->_schema) && !isset($this->_oldSchema)) { $this->_oldSchema = $this->_schema; } $schema = $this->_schema = $this->elasticMapping(); if ($field && is_string($field)) { return Hash::get($schema, $field); } return $schema; } if (!empty($this->_oldSchema)) { $this->_schema = $this->_oldSchema; unset($this->_oldSchema); } // Failsafe to prevent schema mix-ups if (method_exists($this, 'elasticMapping') && $this->_schema === $this->elasticMapping()) { $this->_schema = null; } return parent::schema($field); }
private function copyLanguage($object, $id, $newId) { if (isset($object->multiLanguage['columns'])) { $tableLanguage = 'multilanguage_' . $object->useTable; $model = new AppModel(); $model->useTable = $tableLanguage; $fields = array_keys($model->schema()); $fieldSelect = $fieldInsert = array(); foreach ($fields as $field) { if (in_array($field, array('id'))) { continue; } $fieldInsert[] = '`' . $field . '`'; if ($field == 'object_id') { $fieldSelect[] = '"' . $newId . '"'; } else { $fieldSelect[] = '`' . $field . '`'; } } $query = 'INSERT INTO ' . $model->useTable . ' (' . implode(',', $fieldInsert) . ') SELECT ' . implode(',', $fieldSelect) . ' FROM ' . $model->useTable . ' WHERE object_id = ' . $id; $this->query($query); } }
function bakeModel($model) { if (App::import('Model', $model)) { $object = new $model(); } else { App::import('Model', 'App'); $object = new AppModel(array('name' => $model)); } $fields = $object->schema(); $out = "/* SVN FILE: \$Id\$ */\n"; $out .= "package " . $this->paths['model'] . "\n{\n"; $out .= "\timport com.fake.model.Model\n\n"; $out .= "\tpublic class " . $model . " extends Model\n"; $out .= "\t{\n"; if (!empty($fields)) { foreach ($fields as $field => $options) { $out .= "\t\tpublic var " . $field . ":" . $this->__type($options['type']) . ";\n"; } } $out .= "\n\t\tpublic function " . $model . "()\n"; $out .= "\t\t{\n\n\t\t}\n"; $out .= "\t}\n"; $out .= "}\n"; $path = rtrim($this->params['src'], DS) . DS . join(DS, explode('.', $this->paths['model'])) . DS . $model . '.as'; return $this->createFile($path, $out); }