/** * initialize method * * Merge settings and set Config.language to a valid locale * * @return void * @access public */ function initialize(&$Controller, $config = array()) { App::import('Vendor', 'Mi.MiCache'); $lang = MiCache::setting('Site.lang'); if (!$lang) { if (!defined('DEFAULT_LANGUAGE')) { return; } $lang = DEFAULT_LANGUAGE; } elseif (!defined('DEFAULT_LANGUAGE')) { define('DEFAULT_LANGUAGE', $lang); } Configure::write('Config.language', $lang); App::import('Core', 'I18n'); $I18n =& I18n::getInstance(); $I18n->domain = 'default_' . $lang; $I18n->__lang = $lang; $I18n->l10n->get($lang); if (!empty($Controller->plugin)) { $config['plugins'][] = Inflector::underscore($Controller->plugin); } if (!empty($config['plugins'])) { $plugins = array_intersect(MiCache::mi('plugins'), $config['plugins']); $Inst = App::getInstance(); foreach ($plugins as $path => $name) { $Inst->locales[] = $path . DS . 'locale' . DS; } } }
/** * afterFind method * * @param mixed $Model * @param mixed $results * @param bool $primary * @access public * @return void */ function afterFind(&$Model, $results, $primary = false) { extract($this->settings[$Model->alias]); if (App::import('Vendor', 'Mi.MiCache')) { $models = MiCache::mi('models'); } else { $models = Configure::listObjects('model'); } if ($primary && isset($results[0][$Model->alias][$modelField]) && isset($results[0][$Model->alias][$foreignKey]) && $Model->recursive > 0) { foreach ($results as $key => $result) { $associated = array(); $model = Inflector::classify($result[$Model->alias][$modelField]); $foreignId = $result[$Model->alias][$foreignKey]; if ($model && $foreignId && in_array($model, $models)) { $result = $result[$Model->alias]; if (!isset($Model->{$model})) { $Model->bindModel(array('belongsTo' => array($model => array('conditions' => array($Model->alias . '.' . $modelField => $model), 'foreignKey' => $foreignKey)))); } $conditions = array($model . '.' . $Model->{$model}->primaryKey => $result[$foreignKey]); $recursive = -1; $associated = $Model->{$model}->find('first', compact('conditions', 'recursive')); $name = $Model->{$model}->display($result[$foreignKey]); $associated[$model]['display_field'] = $name ? $name : '*missing*'; $results[$key][$model] = $associated[$model]; } } } elseif (isset($results[$Model->alias][$modelField])) { $associated = array(); $model = Inflector::classify($result[$Model->alias][$modelField]); $foreignId = $results[$Model->alias][$foreignKey]; if ($model && $foreignId) { $result = $results[$Model->alias]; if (!isset($Model->{$model})) { $Model->bindModel(array('belongsTo' => array($model => array('conditions' => array($Model->alias . '.' . $modelField => $model), 'foreignKey' => $foreignKey)))); } $conditions = array($model . '.' . $Model->{$model}->primaryKey => $result[$foreignKey]); $recursive = -1; $associated = $Model->{$model}->find('first', compact('conditions', 'recursive')); $name = $Model->{$model}->display($result[$foreignKey]); $associated[$model]['display_field'] = $name ? $name : '*missing*'; $results[$model] = $associated[$model]; } } return $results; }
/** * testMi method * * It doesn't really matter which method is called * * @return void * @access public */ function testMi() { $path = TMP . 'mi_cache_test' . DS; $Folder = new Folder($path, true); new File($path . 'one' . DS . 'empty.php', true); new File($path . 'two' . DS . 'empty.php', true); new File($path . 'three' . DS . 'empty.php', true); $expected = array($path . 'one' . DS . 'empty.php', $path . 'three' . DS . 'empty.php', $path . 'two' . DS . 'empty.php'); $return = MiCache::mi('files', $path); sort($return); $this->assertIdentical($return, $expected); $Folder->delete(); $return = MiCache::mi('files', $path); sort($return); $this->assertIdentical($return, $expected); }
/** * Returns a formatted LABEL element for HTML FORMs. * * Overriden to refer to field_names.po unless the label is explicitly passed * * @param string $fieldName This should be "Modelname.fieldname", "Modelname/fieldname" is deprecated * @param string $text Text that will appear in the label field. * @return string The formatted LABEL element */ public function label($fieldName = null, $text = null, $attributes = array()) { if (empty($fieldName)) { $view = ClassRegistry::getObject('view'); $fieldName = implode('.', $view->entity()); } if ($text === null) { if (strpos($fieldName, '.')) { $alias = explode('.', $fieldName); if (count($alias) == 3) { $text = str_replace($alias[1] . '.', '', $fieldName); } else { $text = $fieldName; } $alias = $alias[0]; $text = str_replace('.', ' ', $fieldName); } else { $view = ClassRegistry::getObject('view'); $alias = $view->association ? $view->association : $view->model; $text = $alias . ' ' . $fieldName; } if (substr($text, -3) == '_id') { $text = substr($text, 0, strlen($text) - 3); } $_text = Inflector::humanize(Inflector::underscore($text)); $pluginDomain = ''; if (!empty($this->params['plugin'])) { $models = MiCache::mi('models', $this->params['plugin']); if (in_array($alias, $models)) { $pluginDomain = Inflector::underscore($this->params['plugin']) . '_'; } } $text = __d($pluginDomain . 'field_names', $_text, true); if ($_text === $text) { $text = str_replace(Inflector::humanize(Inflector::underscore($alias)) . ' ', '', $_text); } } return parent::label($fieldName, $text, $attributes); }
/** * Return all possible paths to find view files in order * * Check for plugin/locale/<locale>/views/.../foo.ctp if Config.language has been set * * @TODO visibility * @param string $plugin '' * @param bool $cached true * @return array paths * @access protected */ public function _paths($plugin = '', $cached = true) { if (!class_exists('MiCache')) { App::import('Vendor', 'Mi.MiCache'); } $plugin = (string) $plugin; $theme = (string) $this->theme; return MiCache::mi('paths', 'view', compact('plugin', 'theme')); }
/** * controllers method * * @param mixed $what * @return void * @access protected */ protected function _controllers($what) { $this->out($what . ' for which controller?'); $controllers = array_values(MiCache::mi('controllers')); foreach ($controllers as $i => $controller) { $this->out($i + 1 . '. ' . $controller); } $controller = ''; while ($controller == '') { $controller = $this->in(__d('mi', "Enter a number from the list above, or 'q' to exit", true), null, 'q'); if ($controller === 'q') { $this->out(__d('mi', "Exit", true)); $this->_stop(); } if ($controller == '' || !is_numeric($controller) || $controller > count($controllers)) { $this->out(__d('mi', 'Error:', true)); $this->out(__d('mi', "The number you selected was not an option. Please try again.", true)); $controller = ''; } } $this->args[] = $controllers[$controller - 1]; }
/** * populateVendorMap method * * @param bool $reset false * @static * @return void * @access protected */ protected static function _populateVendorMap($reset = false) { if ($reset || !self::$vendorMap) { App::import('Vendor', 'Mi.MiCache'); $plugins = MiCache::mi('plugins'); foreach ($plugins as $path => $plugin) { if (is_dir($path . DS . 'webroot')) { if (!class_exists('Mi')) { APP::import('Vendor', 'Mi.Mi'); } $files = Mi::files($path . DS . 'webroot', null, '.*\\.(css|js)$'); foreach ($files as $fPath) { $fPath = realpath($fPath); self::$vendorMap[str_replace(realpath($path . DS . 'webroot') . DS, $plugin . DS, $fPath)] = $fPath; } } } self::log("Populating Vendor Map"); if (!class_exists('MiCache')) { self::log("\tMiCache doesn't exist. Skipping."); return; } self::$vendorMap = am(self::$vendorMap, MiCache::mi('vendors', null, array('shells'), array('excludeFolders' => array('shells', 'simpletest'), 'extension' => array('css', 'js'), 'excludePattern' => false))); self::log("\tDone."); } }
/** * modes method * * List all modes * * @param bool $reset * @return void * @access public */ function modes($reset = false) { if (!$reset) { $data = cache('enum_modes'); if ($data) { return unserialize($data); } } $data = $this->find('all', array('fields' => array('DISTINCT Enum.type'), 'order' => 'type')); if (!$data) { $components = MiCache::mi('components'); $tables = MiCache::mi('tables'); foreach ($tables as $table) { $class = Inflector::classify($table); if (in_array($class, $components)) { continue; } if ($Inst = ClassRegistry::init($class)) { if ($Inst->Behaviors->attached('Enum')) { foreach ($Inst->actsAs['Enum'] as $field) { $data[] = $Inst->name . '.' . $field; } } } } } else { $data = Set::extract($data, '/Enum/type'); } if ($data) { cache('enum_modes', serialize($data), '+1 day'); } return $data; }