private function loadRoleOptions() { if (!ClassRegistry::isKeySet('SelectOptions')) { $this->loadModel('SelectOptions'); } $this->set('roleOptions', $this->Roles->find('list', array('fields' => array('Roles.role_name', 'Roles.role_name')))); }
/** * Add for problem. * * @param string $foreignKey, Book id */ public function add($objectType, $foreignKey) { try { $model = Inflector::classify($objectType); if (!ClassRegistry::isKeySet($model)) { $this->{$model} = ClassRegistry::init(Configure::read('Problems.Models.' . $model)); } else { $this->{$model} = ClassRegistry::getObject($model); } if (get_class($this->{$model}) === 'AppModel') { throw new Exception(__d('problems', 'Could not save the Problem of unallowed type.')); } $result = $this->{$model}->report($foreignKey, $this->Auth->user('id'), $this->request->data); if ($result === true) { $this->_setFlash(__d('problems', 'The problem has been saved'), 'success'); $this->Referer->redirect('/'); } } catch (OutOfBoundsException $e) { $this->_setFlash($e->getMessage(), 'error'); } catch (LogicException $e) { $this->_setFlash($e->getMessage(), 'error'); $this->Referer->redirect('/'); } catch (Exception $e) { $this->_setFlash($e->getMessage(), 'error'); $this->redirect('/'); } $types = $this->{$model}->Problem->types; $this->set(compact('foreignKey', 'types', 'objectType')); }
/** * コンストラクタ * * @return void * @access public */ function __construct() { $this->_view =& ClassRegistry::getObject('view'); if (isInstalled()) { if (ClassRegistry::isKeySet('Permission')) { $this->Permission = ClassRegistry::getObject('Permission'); } else { $this->Permission = ClassRegistry::init('Permission'); } if (ClassRegistry::isKeySet('Page')) { $this->Page = ClassRegistry::getObject('Page'); } else { $this->Page = ClassRegistry::init('Page'); } if (ClassRegistry::isKeySet('PageCategory')) { $this->PageCategory = ClassRegistry::getObject('PageCategory'); } else { $this->PageCategory = ClassRegistry::init('PageCategory'); } if (isset($this->_view->viewVars['siteConfig'])) { $this->siteConfig = $this->_view->viewVars['siteConfig']; } // プラグインのBaserヘルパを初期化 $this->_initPluginBasers(); } }
function input($fieldName, $options = array()) { if (ClassRegistry::isKeySet($this->model())) { $model =& ClassRegistry::getObject($this->model()); if ($model->isOrigamiColumn($fieldName)) { $type = $model->getOrigamiColumnType($fieldName); if (method_exists($this, $type)) { unset($options['type']); // We are specifying the type, so we need to remove any user specified types $out = $this->{$type}($fieldName, $options); // The code is this section is mostly copied from the cake php // FormHelper input method $div = true; $divOptions = array(); if (array_key_exists('div', $options)) { $div = $options['div']; unset($options['div']); } if (!empty($div)) { $divOptions['class'] = 'input'; $divOptions = $this->addClass($divOptions, $type); if (is_string($div)) { $divOptions['class'] = $div; } elseif (is_array($div)) { $divOptions = array_merge($divOptions, $div); } if (in_array($this->field(), $this->fieldset['validates'])) { $divOptions = $this->addClass($divOptions, 'required'); } if (!isset($divOptions['tag'])) { $divOptions['tag'] = 'div'; } } if ($type != 'hidden') { $error = null; if (isset($options['error'])) { $error = $options['error']; unset($options['error']); } if ($error !== false) { $errMsg = $this->error($fieldName, $error); if ($errMsg) { $out .= $errMsg; $divOptions = $this->addClass($divOptions, 'error'); } } } if (isset($divOptions) && isset($divOptions['tag'])) { $tag = $divOptions['tag']; unset($divOptions['tag']); $out = $this->Html->tag($tag, $out, $divOptions); } // end cake php inputs copy return $out; } } } return parent::input($fieldName, $options); }
/** * KeywordConfig モデルを準備する * */ private function setUpModel() { if (ClassRegistry::isKeySet($this->plugin . '.KeywordConfig')) { $this->KeywordConfigModel = ClassRegistry::getObject($this->plugin . '.KeywordConfig'); } else { $this->KeywordConfigModel = ClassRegistry::init($this->plugin . '.KeywordConfig'); } }
/** * construct * * @param View $View */ public function __construct(View $View) { parent::__construct($View); if (ClassRegistry::isKeySet('Page')) { $this->Page = ClassRegistry::getObject('Page'); } else { $this->Page = ClassRegistry::init('Page', 'Model'); } }
/** * Setup the behaviour and merge in the settings. Check that the model has the * required fields * * @param Model $model * @param array $config */ public function setup(Model $model, $config = array()) { parent::setup($model, $config); $this->settings[$model->alias] = array_merge($this->defaults, $config); $this->modelAlias = $model->alias; $this->hasField($model); $this->checkPrefix($model); if (!ClassRegistry::isKeySet($this->settings[$model->alias]['statusModel'])) { $this->Status = ClassRegistry::init($this->settings[$model->alias]['statusModel']); } }
function beforeRender() { $models = ClassRegistry::keys(); foreach ($models as $currentModel) { if (ClassRegistry::isKeySet($currentModel)) { $currentObject =& ClassRegistry::getObject($currentModel); if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) { $this->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors; } } } }
/** * Get log instance. * * @param [type] $className [description] * @return CakeLog instance. */ public static function getLog($className = null) { if (empty($className)) { $className = Common::read('Log.classname', 'CakeLog'); } list($plugin, $className) = pluginSplit($className, true); $key = 'CommonLog'; if (!ClassRegistry::isKeySet('CommonLog')) { App::uses($className, $plugin . 'Log'); ClassRegistry::addObject('CommonLog', new $className()); } $Log = ClassRegistry::getObject('CommonLog'); return $Log; }
function getEnumValues($ModelField) { // split input into Model and Fieldname $m = explode('.', $ModelField); if ($m[0] == $ModelField) { return false; } else { !ClassRegistry::isKeySet($m[0]) ? $this->loadModel($m[0]) : false; $type = $this->{$m}[0]->getColumnType($m[1]); preg_match_all("/'(.*?)'/", $type, $enums); foreach ($enums[1] as $value) { $enumList[$value] = $value; } return $enumList; } }
/** * Sets up the configuation for the model, and loads ACL models if they haven't been already * * @param mixed $config */ function setup(&$model, $config = array()) { if (is_string($config)) { $config = array('type' => $config); } $this->settings[$model->alias] = array_merge(array('type' => 'requester'), (array) $config); $type = $this->__typeMaps[$this->settings[$model->alias]['type']]; if (!ClassRegistry::isKeySet($type)) { uses('model' . DS . 'db_acl'); $object =& new $type(); } else { $object =& ClassRegistry::getObject($type); } $model->{$type} =& $object; if (!method_exists($model, 'parentNode')) { trigger_error("Callback parentNode() not defined in {$model->alias}", E_USER_WARNING); } }
function __debug() { $argc = func_num_args(); $args = func_get_args(); if ($argc == 0) { return false; } global $__session; if (Configure::read() == 0) { return false; } $class = 'CakeSession'; if ($__session === null) { if (ClassRegistry::isKeySet($class)) { $__session =& ClassRegistry::getObject($class); } else { if (!class_exists($class)) { App::import('Core', $class); } $__session =& new $class(); ClassRegistry::addObject($class, $__session); } } $session =& $__session; if (!$session->started()) { $session->start(); } $debugs = array(); if ($session->check('debug.printable')) { $debugs = $session->read('debug.printable'); } $debugger =& Debugger::getInstance(); $trace = $debugger->trace(aa('start', 2, 'format', 'array')); while ($trace[0]['function'] === __FUNCTION__ || false !== strpos($trace[0]['function'], 'call_user_func')) { array_shift($trace); } $debug = compact('args', 'trace'); $debugs = array_merge($debugs, array($debug)); // debug(compact('prints','args')); $session->write('debug.printable', $debugs); return true; }
/** * undocumented * * @access public */ function &getComponent($component) { $componentKey = 'Component.' . $component; if (ClassRegistry::isKeySet($componentKey)) { $Component =& ClassRegistry::getObject($componentKey); } else { Assert::true(App::import('Component', $component)); $class = $component . 'Component'; $Component = new $class(); $Controller = new Controller(); if (method_exists($Component, 'initialize')) { $Component->initialize($Controller, array()); } if (method_exists($Component, 'startup')) { $Component->startup($Controller); } } Assert::isObject($Component); return $Component; }
/** * Sets up the configuation for the model, and loads ACL models if they haven't been already * * @param mixed $config */ function setup(&$model, $config = array()) { if (empty($config)) { $config = array('Aro'); } elseif (is_string($config)) { $config = array($config); } $this->settings[$model->name]['types'] = $config; foreach ($this->settings[$model->name]['types'] as $type) { if (!ClassRegistry::isKeySet($type)) { uses('model' . DS . 'db_acl'); $object =& new $type(); } else { $object =& ClassRegistry::getObject($type); } $model->{$type} =& $object; } if (!method_exists($model, 'parentNode')) { trigger_error("Callback parentNode() not defined in {$model->name}", E_USER_WARNING); } }
function __construct(&$controller) { if (is_object($controller)) { $this->viewVars = $controller->viewVars; if (isset($controller->compress)) { $this->compress = $controller->compress; } if ($this->compress === true) { $this->compress = 30100; } $dispatcher = AmfDispatcher::getInstance(); $vars = array('binaryType', 'binaryLength', 'bigEndian', 'references', 'stored', 'types', 'requests', 'request', 'header', 'body', 'encoding'); foreach ($vars as $var) { $this->{$var} = $dispatcher->{$var}; } } if (ClassRegistry::isKeySet('amfview')) { $amf = ClassRegistry::getObject('amfview'); $this->stream = $amf->stream; $this->__hasRendered = $amf->__hasRendered; } ClassRegistry::addObject('amfview', $this); }
function establishBindings(&$Model, $modelName = false) { if (!$modelName) { $modelName = $this->settings[$Model->name]['with']; } if (!isset($Model->hasMany[$modelName])) { // if the model is not already in the class registry create it if (!ClassRegistry::isKeySet(Inflector::tableize($modelName))) { // if the calling model is 'User' creates a 'UserData' model using the eav_data table $EavModel =& ClassRegistry::init(array('class' => 'AppModel', 'name' => $modelName, 'alias' => $modelName, 'table' => 'eav_data')); } // bind the data to the model $Model->bindModel(array('hasMany' => array($modelName => array('foreignKey' => 'entity_id'))), false); } if (!isset($Model->{$modelName}->belongsTo['Attribute'])) { // Attribute and Datatype helper tables store the meaningful data $Model->{$modelName}->bindModel(array('belongsTo' => array('Attribute')), false); } if (!isset($Model->{$modelName}->Attribute->belongsTo['Datatype'])) { $Model->{$modelName}->Attribute->bindModel(array('belongsTo' => array('Datatype')), false); } if (isset($Model->{$modelName}->Attribute->belongsTo['Datatype'])) { $eav_tables = $Model->{$modelName}->Attribute->Datatype->getEnumValues('table'); /** * leetness .. add new data storage tables by creating * eav_{$datatypes} and extend the 'table' enum in the * 'datatypes' table. Models will be created on the fly. */ foreach ($eav_tables as $eav_table) { $eavModelName = 'Eav' . Inflector::camelize(strtolower($eav_table)); if (!isset($Model->{$modelName}->belongsTo[$eavModelName])) { $eavTableArray = array($eavModelName => array('foreignKey' => 'value_id')); $Model->{$modelName}->bindModel(array('belongsTo' => $eavTableArray), false); } } } }
/** * Handles the lazy loading of model associations by looking in the association arrays for the requested variable * * @param string $name variable tested for existence in class * @return boolean true if the variable exists (if is a not loaded model association it will be created), false otherwise */ public function __isset($name) { $className = false; foreach ($this->_associations as $type) { if (isset($name, $this->{$type}[$name])) { $className = empty($this->{$type}[$name]['className']) ? $name : $this->{$type}[$name]['className']; break; } elseif (isset($name, $this->__backAssociation[$type][$name])) { $className = empty($this->__backAssociation[$type][$name]['className']) ? $name : $this->__backAssociation[$type][$name]['className']; break; } else { if ($type == 'hasAndBelongsToMany') { foreach ($this->{$type} as $k => $relation) { if (empty($relation['with'])) { continue; } if (is_array($relation['with'])) { if (key($relation['with']) === $name) { $className = $name; } } else { list($plugin, $class) = pluginSplit($relation['with']); if ($class === $name) { $className = $relation['with']; } } if ($className) { $assocKey = $k; $dynamic = !empty($relation['dynamicWith']); break 2; } } } } } if (!$className) { return false; } list($plugin, $className) = pluginSplit($className); if (!ClassRegistry::isKeySet($className) && !empty($dynamic)) { $this->{$className} = new AppModel(array('name' => $className, 'table' => $this->hasAndBelongsToMany[$assocKey]['joinTable'], 'ds' => $this->useDbConfig)); } else { $this->_constructLinkedModel($name, $className, $plugin); } if (!empty($assocKey)) { $this->hasAndBelongsToMany[$assocKey]['joinTable'] = $this->{$name}->table; if (count($this->{$name}->schema()) <= 2 && $this->{$name}->primaryKey !== false) { $this->{$name}->primaryKey = $this->hasAndBelongsToMany[$assocKey]['foreignKey']; } } return true; }
/** * Build an array-based association from string. * * @param string $type 'belongsTo', 'hasOne', 'hasMany', 'hasAndBelongsToMany' * @return void * @access private */ function __generateAssociation($type) { foreach ($this->{$type} as $assocKey => $assocData) { $class = $assocKey; $dynamicWith = false; foreach ($this->__associationKeys[$type] as $key) { if (!isset($this->{$type}[$assocKey][$key]) || $this->{$type}[$assocKey][$key] === null) { $data = ''; switch ($key) { case 'fields': $data = ''; break; case 'foreignKey': $data = ($type == 'belongsTo' ? Inflector::underscore($assocKey) : Inflector::singularize($this->table)) . '_id'; break; case 'associationForeignKey': $data = Inflector::singularize($this->{$class}->table) . '_id'; break; case 'with': $data = Inflector::camelize(Inflector::singularize($this->{$type}[$assocKey]['joinTable'])); $dynamicWith = true; break; case 'joinTable': $tables = array($this->table, $this->{$class}->table); sort($tables); $data = $tables[0] . '_' . $tables[1]; break; case 'className': $data = $class; break; case 'unique': $data = true; break; } $this->{$type}[$assocKey][$key] = $data; } } if (!empty($this->{$type}[$assocKey]['with'])) { $joinClass = $this->{$type}[$assocKey]['with']; if (is_array($joinClass)) { $joinClass = key($joinClass); } $plugin = null; if (strpos($joinClass, '.') !== false) { list($plugin, $joinClass) = explode('.', $joinClass); $plugin = $plugin . '.'; $this->{$type}[$assocKey]['with'] = $joinClass; } if (!ClassRegistry::isKeySet($joinClass) && $dynamicWith === true) { $this->{$joinClass} = new AppModel(array('name' => $joinClass, 'table' => $this->{$type}[$assocKey]['joinTable'], 'ds' => $this->useDbConfig)); } else { $this->__constructLinkedModel($joinClass, $plugin . $joinClass); $this->{$type}[$assocKey]['joinTable'] = $this->{$joinClass}->table; } if (count($this->{$joinClass}->schema()) <= 2 && $this->{$joinClass}->primaryKey !== false) { $this->{$joinClass}->primaryKey = $this->{$type}[$assocKey]['foreignKey']; } } } }
/** * Returns an HTML FORM element. * * ### Options: * * - `type` Form method defaults to POST * - `action` The controller action the form submits to, (optional). * - `url` The url the form submits to. Can be a string or a url array. If you use 'url' * you should leave 'action' undefined. * - `default` Allows for the creation of Ajax forms. * - `onsubmit` Used in conjunction with 'default' to create ajax forms. * - `inputDefaults` set the default $options for FormHelper::input(). Any options that would * be set when using FormHelper::input() can be set here. Options set with `inputDefaults` * can be overridden when calling input() * - `encoding` Set the accept-charset encoding for the form. Defaults to `Configure::read('App.encoding')` * * @access public * @param string $model The model object which the form is being defined for * @param array $options An array of html attributes and options. * @return string An formatted opening FORM tag. * @link http://book.cakephp.org/view/1384/Creating-Forms */ function create($model = null, $options = array()) { $created = $id = false; $append = ''; if (is_array($model) && empty($options)) { $options = $model; $model = null; } if (empty($model) && $model !== false && !empty($this->request['models'])) { $model = $this->request['models'][0]; $this->defaultModel = $this->request['models'][0]; } elseif (empty($model) && empty($this->request['models'])) { $model = false; } $models = ClassRegistry::keys(); foreach ($models as $currentModel) { if (ClassRegistry::isKeySet($currentModel)) { $currentObject =& ClassRegistry::getObject($currentModel); if (is_a($currentObject, 'Model') && !empty($currentObject->validationErrors)) { $this->validationErrors[Inflector::camelize($currentModel)] =& $currentObject->validationErrors; } } } if ($model !== false) { $object = $this->_introspectModel($model); $this->setEntity($model . '.', true); } $modelEntity = $this->model(); if ($model !== false && isset($this->fieldset[$modelEntity]['key'])) { $data = $this->fieldset[$modelEntity]; $recordExists = isset($this->request->data[$model]) && !empty($this->request->data[$model][$data['key']]) && !is_array($this->request->data[$model][$data['key']]); if ($recordExists) { $created = true; $id = $this->request->data[$model][$data['key']]; } } $options = array_merge(array('type' => $created && empty($options['action']) ? 'put' : 'post', 'action' => null, 'url' => null, 'default' => true, 'encoding' => strtolower(Configure::read('App.encoding')), 'inputDefaults' => array()), $options); $this->_inputDefaults = $options['inputDefaults']; unset($options['inputDefaults']); if (!isset($options['id'])) { $domId = isset($options['action']) ? $options['action'] : $this->request['action']; $options['id'] = $this->domId($domId . 'Form'); } if ($options['action'] === null && $options['url'] === null) { $options['action'] = $this->request->here(false); } elseif (empty($options['url']) || is_array($options['url'])) { if (empty($options['url']['controller'])) { if (!empty($model) && $model != $this->defaultModel) { $options['url']['controller'] = Inflector::underscore(Inflector::pluralize($model)); } elseif (!empty($this->request->params['controller'])) { $options['url']['controller'] = Inflector::underscore($this->request->params['controller']); } } if (empty($options['action'])) { $options['action'] = $this->request->params['action']; } $actionDefaults = array('plugin' => $this->plugin, 'controller' => $this->_View->viewPath, 'action' => $options['action']); $options['action'] = array_merge($actionDefaults, (array) $options['url']); if (empty($options['action'][0]) && !empty($id)) { $options['action'][0] = $id; } } elseif (is_string($options['url'])) { $options['action'] = $options['url']; } unset($options['url']); switch (strtolower($options['type'])) { case 'get': $htmlAttributes['method'] = 'get'; break; case 'file': $htmlAttributes['enctype'] = 'multipart/form-data'; $options['type'] = $created ? 'put' : 'post'; case 'post': case 'put': case 'delete': $append .= $this->hidden('_method', array('name' => '_method', 'value' => strtoupper($options['type']), 'id' => null)); default: $htmlAttributes['method'] = 'post'; break; } $this->requestType = strtolower($options['type']); $htmlAttributes['action'] = $this->url($options['action']); unset($options['type'], $options['action']); if ($options['default'] == false) { if (isset($htmlAttributes['onSubmit']) || isset($htmlAttributes['onsubmit'])) { $htmlAttributes['onsubmit'] .= ' event.returnValue = false; return false;'; } else { $htmlAttributes['onsubmit'] = 'event.returnValue = false; return false;'; } } if (!empty($options['encoding'])) { $htmlAttributes['accept-charset'] = $options['encoding']; unset($options['encoding']); } unset($options['default']); $htmlAttributes = array_merge($options, $htmlAttributes); $this->fields = array(); if (isset($this->request->params['_Token']) && !empty($this->request->params['_Token'])) { $append .= $this->hidden('_Token.key', array('value' => $this->request->params['_Token']['key'], 'id' => 'Token' . mt_rand())); } if (!empty($append)) { $append = $this->Html->useTag('block', ' style="display:none;"', $append); } $this->setEntity($model . '.', true); return $this->Html->useTag('form', $htmlAttributes) . $append; }
/** * Private helper method to create associated models of given class. * @param string $assoc * @param string $className Class name * @param string $type Type of assocation * @access private */ function __constructLinkedModel($assoc, $className) { $colKey = Inflector::underscore($className); if (!class_exists($className)) { loadModel($className); } if (ClassRegistry::isKeySet($colKey)) { $this->{$assoc} =& ClassRegistry::getObject($colKey); $this->{$className} =& $this->{$assoc}; } else { $this->{$assoc} =& new $className(); $this->{$className} =& $this->{$assoc}; } $this->alias[$assoc] = $this->{$assoc}->table; $this->tableToModel[$this->{$assoc}->table] = $className; $this->modelToTable[$assoc] = $this->{$assoc}->table; }
/** * Tests that a model can be loaded from the model names passed in the request object * * @return void */ public function testIntrospectModelFromRequest() { $this->loadFixtures('Post'); App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS))); CakePlugin::load('TestPlugin'); $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')); $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost')); $this->Form->create('TestPluginPost'); $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost')); $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost')); CakePlugin::unload(); App::build(); }
/** * 公開状態を取得する * * @param array $data ブログ記事 * @return boolean 公開状態 */ public function allowPublish($data) { if (ClassRegistry::isKeySet('BlogPost')) { $BlogPost = ClassRegistry::getObject('BlogPost'); } else { $BlogPost = ClassRegistry::init('BlogPost'); } return $BlogPost->allowPublish($data); }
/** * Gets the data for the current tag * * @param mixed $options If an array, should be an array of attributes that $key needs to be added to. * If a string or null, will be used as the View entity. * @param string $field * @param string $key The name of the attribute to be set, defaults to 'value' * @return mixed If an array was given for $options, an array with $key set will be returned. * If a string was supplied a string will be returned. * @todo Refactor this method to not have as many input/output options. */ public function value($options = array(), $field = null, $key = 'value') { if ($options === null) { $options = array(); } elseif (is_string($options)) { $field = $options; $options = 0; } if (is_array($options) && isset($options[$key])) { return $options; } if (!empty($field)) { $this->setEntity($field); } $result = null; $data = $this->request->data; $entity = $this->entity(); if (!empty($data) && !empty($entity)) { $result = Set::extract(implode('.', $entity), $data); } $habtmKey = $this->field(); if (empty($result) && isset($data[$habtmKey][$habtmKey]) && is_array($data[$habtmKey])) { $result = $data[$habtmKey][$habtmKey]; } elseif (empty($result) && isset($data[$habtmKey]) && is_array($data[$habtmKey])) { if (ClassRegistry::isKeySet($habtmKey)) { $model = ClassRegistry::getObject($habtmKey); $result = $this->_selectedArray($data[$habtmKey], $model->primaryKey); } } if (is_array($options)) { if ($result === null && isset($options['default'])) { $result = $options['default']; } unset($options['default']); } if (is_array($options)) { $options[$key] = $result; return $options; } else { return $result; } }
/** * Guess the location for a model based on its name and tries to create a new instance * or get an already created instance of the model * * @param string $model * @return Model model instance */ protected function _getModel($model) { $object = null; if (!$model || $model === 'Model') { return $object; } if (array_key_exists($model, $this->_models)) { return $this->_models[$model]; } if (ClassRegistry::isKeySet($model)) { $object = ClassRegistry::getObject($model); } elseif (isset($this->request->params['models'][$model])) { $plugin = $this->request->params['models'][$model]['plugin']; $plugin .= ($plugin) ? '.' : null; $object = ClassRegistry::init(array( 'class' => $plugin . $this->request->params['models'][$model]['className'], 'alias' => $model )); } elseif (ClassRegistry::isKeySet($this->defaultModel)) { $defaultObject = ClassRegistry::getObject($this->defaultModel); if ($defaultObject && in_array($model, array_keys($defaultObject->getAssociated()), true) && isset($defaultObject->{$model})) { $object = $defaultObject->{$model}; } } else { $object = ClassRegistry::init($model, true); } $this->_models[$model] = $object; if (!$object) { return null; } $this->fieldset[$model] = array('fields' => null, 'key' => $object->primaryKey, 'validates' => null); return $object; }
/** * Modifies $result array to place virtual fields in model entry where they belongs to * * @param array $resut REference to the fetched row * @return void */ function fetchVirtualField(&$result) { if (isset($result[0]) && is_array($result[0])) { foreach ($result[0] as $field => $value) { if (strpos($field, $this->virtualFieldSeparator) === false) { continue; } list($alias, $virtual) = explode($this->virtualFieldSeparator, $field); if (!ClassRegistry::isKeySet($alias)) { return; } $model = ClassRegistry::getObject($alias); if ($model->isVirtualField($virtual)) { $result[$alias][$virtual] = $value; unset($result[0][$field]); } } if (empty($result[0])) { unset($result[0]); } } }
/** * Gets the data for the current tag * * @param array $options * @param string $key * @return array * @access public */ function value($options = array(), $field = null, $key = 'value') { if ($options === null) { $options = array(); } elseif (is_string($options)) { $field = $options; $options = 0; } if (!empty($field)) { $this->setFormTag($field); } if (is_array($options) && isset($options[$key])) { return $options; } $result = null; if (isset($this->data[$this->model()][$this->field()])) { $result = $this->data[$this->model()][$this->field()]; } elseif (isset($this->data[$this->field()]) && is_array($this->data[$this->field()])) { if (ClassRegistry::isKeySet($this->field())) { $model =& ClassRegistry::getObject($this->field()); $result = $this->__selectedArray($this->data[$this->field()], $model->primaryKey); } } if (is_array($options)) { if (empty($result) && isset($options['default'])) { $result = $options['default']; } unset($options['default']); } if (is_array($options)) { $options[$key] = $result; return $options; } else { return $result; } }
/** * testPluginAppModel method * * @return void */ public function testPluginAppModel() { $TestRegistryPluginModel = ClassRegistry::isKeySet('TestRegistryPluginModel'); $this->assertFalse($TestRegistryPluginModel); //Faking a plugin CakePlugin::load('RegistryPlugin', array('path' => '/fake/path')); $TestRegistryPluginModel = ClassRegistry::init('RegistryPlugin.TestRegistryPluginModel'); $this->assertTrue(is_a($TestRegistryPluginModel, 'TestRegistryPluginModel')); $this->assertEquals($TestRegistryPluginModel->tablePrefix, 'something_'); $PluginUser = ClassRegistry::init(array('class' => 'RegistryPlugin.RegisterUser', 'alias' => 'RegistryPluginUser', 'table' => false)); $this->assertTrue(is_a($PluginUser, 'RegistryPluginAppModel')); $PluginUserCopy = ClassRegistry::getObject('RegistryPluginUser'); $this->assertTrue(is_a($PluginUserCopy, 'RegistryPluginAppModel')); $this->assertSame($PluginUser, $PluginUserCopy); CakePlugin::unload(); }
/** * プラグイン管理のリンクを指定したユーザーのお気に入りに追加 * * @param string $pluginName プラグイン名 * @param array $user ユーザーデータの配列 * @return void */ public function addFavoriteAdminLink($pluginName, $user) { $plugin = $this->findByName($pluginName); $dirPath = $this->getDirectoryPath($pluginName); $pluginInfo = $this->getPluginInfo(array($plugin), $dirPath); //リンクが設定されていない if (empty($pluginInfo['Plugin']['admin_link'])) { return; } if (ClassRegistry::isKeySet('Favorite')) { $this->Favorite = ClassRegistry::getObject('Favorite'); } else { $this->Favorite = ClassRegistry::init('Favorite'); } $adminLinkUrl = preg_replace('/^' . preg_quote(Configure::read('App.baseUrl'), '/') . '/', '', Router::url($pluginInfo['Plugin']['admin_link'])); //すでにお気に入りにリンクが含まれている場合 if ($this->Favorite->find('count', array('conditions' => array('Favorite.url' => $adminLinkUrl, 'Favorite.user_id' => $user['id']))) > 0) { return; } $favorite = array('name' => $pluginInfo['Plugin']['title'] . '管理', 'url' => $adminLinkUrl, 'sort' => $this->Favorite->getMax('sort') + 1, 'user_id' => $user['id']); $this->Favorite->create($favorite); $this->Favorite->save(); }
/** * Loads a behavior into the collection. You can use use `$config['enabled'] = false` * to load a behavior with callbacks disabled. By default callbacks are enabled. Disable behaviors * can still be used as normal. * * You can alias your behavior as an existing behavior by setting the 'className' key, i.e., * {{{ * public $actsAs = array( * 'Tree' => array( * 'className' => 'AliasedTree' * ); * ); * }}} * All calls to the `Tree` behavior would use `AliasedTree` instead. * * @param string $behavior CamelCased name of the behavior to load * @param array $config Behavior configuration parameters * @return boolean True on success, false on failure * @throws MissingBehaviorException when a behavior could not be found. */ public function load($behavior, $config = array()) { if (is_array($config) && isset($config['className'])) { $alias = $behavior; $behavior = $config['className']; } $configDisabled = isset($config['enabled']) && $config['enabled'] === false; unset($config['enabled'], $config['className']); list($plugin, $name) = pluginSplit($behavior, true); if (!isset($alias)) { $alias = $name; } $class = $name . 'Behavior'; App::uses($class, $plugin . 'Model/Behavior'); if (!class_exists($class)) { throw new MissingBehaviorException(array('class' => $class, 'plugin' => substr($plugin, 0, -1))); } if (!isset($this->{$alias})) { if (ClassRegistry::isKeySet($class)) { $this->_loaded[$alias] = ClassRegistry::getObject($class); } else { $this->_loaded[$alias] = new $class(); ClassRegistry::addObject($class, $this->_loaded[$alias]); if (!empty($plugin)) { ClassRegistry::addObject($plugin . '.' . $class, $this->_loaded[$alias]); } } } elseif (isset($this->_loaded[$alias]->settings) && isset($this->_loaded[$alias]->settings[$this->modelName])) { if ($config !== null && $config !== false) { $config = array_merge($this->_loaded[$alias]->settings[$this->modelName], $config); } else { $config = array(); } } if (empty($config)) { $config = array(); } $this->_loaded[$alias]->setup(ClassRegistry::getObject($this->modelName), $config); foreach ($this->_loaded[$alias]->mapMethods as $method => $methodAlias) { $this->_mappedMethods[$method] = array($alias, $methodAlias); } $methods = get_class_methods($this->_loaded[$alias]); $parentMethods = array_flip(get_class_methods('ModelBehavior')); $callbacks = array('setup', 'cleanup', 'beforeFind', 'afterFind', 'beforeSave', 'afterSave', 'beforeDelete', 'afterDelete', 'onError'); foreach ($methods as $m) { if (!isset($parentMethods[$m])) { $methodAllowed = $m[0] != '_' && !array_key_exists($m, $this->_methods) && !in_array($m, $callbacks); if ($methodAllowed) { $this->_methods[$m] = array($alias, $m); } } } if (!in_array($alias, $this->_enabled) && !$configDisabled) { $this->enable($alias); } elseif ($configDisabled) { $this->disable($alias); } return true; }
/** * Loads tasks defined in var $tasks * * @return bool * @access public */ function loadTasks() { if ($this->tasks === null || $this->tasks === false || $this->tasks === true || empty($this->tasks)) { return true; } $tasks = $this->tasks; if (!is_array($tasks)) { $tasks = array($tasks); } foreach ($tasks as $taskName) { $task = Inflector::underscore($taskName); $taskClass = Inflector::camelize($taskName . 'Task'); if (!class_exists($taskClass)) { foreach ($this->Dispatch->shellPaths as $path) { $taskPath = $path . 'tasks' . DS . $task . '.php'; if (file_exists($taskPath)) { require_once $taskPath; break; } } } $taskClassCheck = $taskClass; if (!PHP5) { $taskClassCheck = strtolower($taskClass); } if (ClassRegistry::isKeySet($taskClassCheck)) { $this->taskNames[] = $taskName; if (!PHP5) { $this->{$taskName} =& ClassRegistry::getObject($taskClassCheck); } else { $this->{$taskName} = ClassRegistry::getObject($taskClassCheck); } } else { $this->taskNames[] = $taskName; if (!PHP5) { $this->{$taskName} =& new $taskClass($this->Dispatch); } else { $this->{$taskName} = new $taskClass($this->Dispatch); } } if (!isset($this->{$taskName})) { $this->err("Task `{$taskName}` could not be loaded"); $this->_stop(); } } return true; }