/** * 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; } } }
/** * display method * * @param mixed $identifier null * @param mixed $value null * @param mixed $default null * @return void * @access public */ function display($identifier = null, $value = null, $default = null) { if (!isset($this->_cache[$identifier])) { $this->_cache[$identifier] = MiCache::data('MiEnums.Enum', 'values', $identifier); } if (isset($this->_cache[$identifier][$value])) { return $this->_cache[$identifier][$value]; } if (!$default) { return MiCache::data('MiEnums.Enum', 'defaultValue', $identifier); } return $default; }
/** * 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; }
/** * Custom find types, as per Matt Curry's method * * @param string $type * @param array $options * @return mixed array|integer|boolean * @access public * @author Matt Curry * @link http://github.com/mcurry/find */ function find($type, $options = array()) { $method = null; if (is_string($type)) { $method = sprintf('__find%s', Inflector::camelize($type)); } if ($method && method_exists($this, $method)) { $return = $this->{$method}($options); if ($this->query != null) { unset($this->query['paginate']); $query = $this->query; $this->query = null; return $query; } return $return; } if (!empty($options['blacklist'])) { $options['blacklist'] = (array) $options['blacklist']; $options['fields'] = isset($options['fields']) ? $options['fields'] : array_keys($this->schema()); $options['fields'] = array_diff($options['fields'], $options['blacklist']); unset($options['blacklist']); } if (!empty($options['cache'])) { if (!class_exists('MiCache')) { App::import('Vendor', 'mi_cache'); } if (is_int($options['cache'])) { MiCache::config(array('duration' => $options['cache'])); } unset($options['cache']); return MiCache::data($this->alias, 'find', $type, $options); } if (!in_array($type, array_keys($this->_findMethods))) { diebug(array($type, $options)); $calledFrom = debug_backtrace(); CakeLog::write('error', "Unknown method {$this->alias}->{$method} in " . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . ' on line ' . $calledFrom[0]['line']); return false; } $args = func_get_args(); return call_user_func_array(array('parent', 'find'), $args); }
/** * Clean up after yourself * * @return void * @access public */ public function endTest() { $Configure = Configure::getInstance(); $Configure = $this->_Configure; MiCache::clear(); }
/** * 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); }
/** * Do we have a settings table? * * If not put Configure::write('MiSettings.noDb', true) in your bootstrap * * @return void * @access protected */ protected static function _hasSettingsTable() { if (MiCache::$_hasSettingsTable === null) { MiCache::$_hasSettingsTable = !Configure::read('MiSettings.noDb'); } return MiCache::$_hasSettingsTable; }
/** * 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')); }
/** * 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; }
/** * 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]; }
/** * removeStopWords from a string. if $splitOnStopWord is true, the following occurs: * input "apples bananas pears and red cars" * output array('apples bananas pears', 'red cars') * * If the passed string doesn't contain the seperator, or after stripping out stop words there's * nothing left - the original input is returned (in the desired format) * * Therefore passing "contain" will return immediately array('contain') * Passing "contain this text" will return array('text') * both contain and this are stop words * Passing "contain this" will return array('contain this') * * @param mixed $Model * @param mixed $string string or array of words * @param array $params * @return mixed * @access public */ public function removeStopWords(&$Model, $string = '', $params = array()) { if (!$string) { return $string; } $seperator = ' '; $splitOnStopWord = true; $return = 'array'; $originalIfEmpty = true; extract($params); if (!class_exists('MiCache')) { App::import('Vendor', 'Mi.MiCache'); } if (!empty($this->settings[$Model->alias]['language'])) { $lang = $this->settings[$Model->alias]['language']; } else { $lang = MiCache::setting('Site.lang'); if (!$lang) { $lang = 'eng'; } $this->settings[$Model->alias]['language'] = $lang; } if (!array_key_exists($lang, $this->stopWords)) { ob_start(); if (!App::import('Vendor', 'stop_words/' . $lang, array('file' => "stop_words/{$lang}.txt"))) { App::import('Vendor', 'stop_words/' . $lang, array('plugin' => 'Mi', 'file' => "stop_words/{$lang}.txt")); } $stopWords = preg_replace('@/\\*.*\\*/@', '', ob_get_clean()); $this->stopWords[$lang] = array_filter(array_map('trim', explode("\n", $stopWords))); } if (is_array($string)) { $originalTerms = $terms = $string; foreach ($terms as $i => &$term) { $term = trim(preg_replace('@[^\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}]@u', $seperator, $term), $seperator); } $lTerms = array_map('mb_strtolower', $terms); $lTerms = array_diff($lTerms, $this->stopWords[$lang]); $terms = array_intersect_key($terms, $lTerms); } else { if (!strpos($string, $seperator)) { if ($return === 'array') { return array($string); } return $string; } $string = preg_replace('@[^\\p{Ll}\\p{Lm}\\p{Lo}\\p{Lt}\\p{Lu}]@u', $seperator, $string); $originalTerms = $terms = array_filter(array_map('trim', explode($seperator, $string))); if ($splitOnStopWord) { $terms = $chunk = array(); $snippet = ''; foreach ($originalTerms as $term) { $lterm = strtolower($term); if (in_array($lterm, $this->stopWords[$lang])) { if ($chunk) { $terms[] = $chunk; $chunk = array(); } continue; } $chunk[] = $term; } if ($chunk) { $terms[] = $chunk; } foreach ($terms as &$phrase) { $phrase = implode(' ', $phrase); } } else { $lTerms = array_map('mb_strtolower', $terms); $lTerms = array_diff($lTerms, $this->stopWords[$lang]); $terms = array_intersect_key($terms, $lTerms); } } if (!$terms && $originalIfEmpty) { $terms = array(implode(' ', $originalTerms)); } if ($return === 'array') { return array_values(array_unique($terms)); } return implode($seperator, $terms); }
/** * Write the cache only if it changed * * @return void * @access private */ function __destruct() { if ($this->_change) { MiCache::write($this->_key, $this->_cache, 'Url'); } if ($this->_changeGlobal) { MiCache::write('_global', $this->_cacheGlobal, 'Url'); } }
/** * url method * * @param mixed $url * @param bool $full false * @return void * @access public */ function url($url, $full = false) { if (is_a($this, 'SeoComponent')) { $_this =& $this; } else { $_this =& SeoComponent::getInstance(); } $domain = null; if (is_array($url)) { $url = $_this->sortUrl($url); if (array_key_exists('domain', $url)) { if (!strpos($url['domain'], '.')) { $site = Configure::read('Site.id'); if ($url['domain'] == $site) { unset($url['domain']); } else { $url['domain'] = MiCache::data('Site', 'field', 'domain', array('id' => $url['domain'])); } } elseif ($url['domain'] == Configure::read('Seo.maindomain')) { unset($url['domain']); } if (!empty($url['domain'])) { $domain = $url['domain']; } unset($url['domain']); } } $_url = Router::url($url); if (!$domain) { return $_url; } static $s = false; if ($s === false) { if (env('HTTPS')) { $s = 's'; } else { $s = null; } } return 'http' . $s . '://' . $domain . $_url; }
/** * 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."); } }
/** * viewPaths method * * @param mixed $locale null * @param mixed $plugin null * @return void * @access protected */ protected function _viewPaths($plugin = null, $locale = null) { if (!$locale) { App::import('Vendor', 'Mi.MiCache'); $locale = MiCache::setting('Site.lang'); if (!$locale) { $locale = Configure::read('Config.language'); if (!$locale && defined('DEFAULT_LANGUAGE')) { $locale = 'DEFAULT_LANGUAGE'; } } if ($locale) { Configure::write('Config.language', $locale); App::import('Core', 'I18n'); $I18n =& I18n::getInstance(); $I18n->domain = 'default_' . $locale; $I18n->__lang = $locale; $I18n->l10n->get($locale); } } if (Mi::cakeVersion() === '1.2') { $viewPaths = Configure::read('viewPaths'); $localePaths = Configure::read('localePaths'); } else { $viewPaths = App::path('views'); $localePaths = App::path('locales'); } if (!class_exists('I18n')) { App::import('Core', array('I18n')); } $languagePaths = array_filter(array_unique(I18n::getInstance()->l10n->languagePath)); $paths = array(); if ($plugin) { $count = count($viewPaths); for ($i = 0; $i < $count; $i++) { $base = $viewPaths[$i] . 'plugins' . DS . $plugin . DS; if ($locale) { foreach ($localePaths as $path) { foreach ($localePaths as $_locale) { $paths[] = $_locale . $locale . DS . 'views' . DS . 'plugins' . DS . $plugin . DS; } } } $paths[] = $base; } if (Mi::cakeVersion() === '1.2') { $pluginPaths = Configure::read('pluginPaths'); } else { $pluginPaths = App::path('plugins'); } $count = count($pluginPaths); for ($i = 0; $i < $count; $i++) { $base = $pluginPaths[$i] . $plugin . DS; if ($locale) { foreach ($localePaths as $path) { foreach ($languagePaths as $_locale) { $paths[] = $pluginPaths[$i] . $plugin . DS . 'locale' . DS . $_locale . DS . 'views' . DS; } } } $paths[] = $base . 'views' . DS; } } if ($locale) { foreach ($localePaths as $path) { foreach ($languagePaths as $_locale) { $paths[] = $path . $_locale . DS . 'views' . DS; } } } if ($paths) { $viewPaths = array_merge($paths, $viewPaths); } if (!empty($theme)) { $themePaths = array(); $count = count($paths); for ($i = 0; $i < $count; $i++) { if (strpos($paths[$i], DS . 'plugins' . DS) === false && strpos($paths[$i], DS . 'libs' . DS . 'view') === false) { if ($plugin) { $themePaths[] = $paths[$i] . 'themed' . DS . $theme . DS . 'plugins' . DS . $plugin . DS; } $themePaths[] = $paths[$i] . 'themed' . DS . $theme . DS; } } $viewPaths = array_merge($themePaths, $viewPaths); } return array_unique($viewPaths); }
/** * Return the enumerated values for the specified field. * * @param mixed $Model * @param string $field '' * @param bool $autoPopulate true * @return void * @access public */ function enumValues(&$Model, $field = '', $autoPopulate = true) { if (!$field) { $fields = $this->enumFields($Model); $return = array(); foreach ($fields as $field) { $return[$field] = $this->enumFields($Model, $field, $autoPopulate); } return $return; } App::import('Vendor', 'Mi.MiCache'); return MiCache::data('MiEnums.Enum', 'values', $Model->name . '.' . $field, $Model->useDbConfig, $autoPopulate); }
/** * If code and domain are not passed explicitly - use the MiCache setting class * to determine which code and domain to use * * @param mixed $code null * @param mixed $domain null * @return string the code to inject * @access protected */ protected function _codeBlock($code = null, $domain = null) { if ($this->settings['element']) { $element = $this->settings['element'] === true ? 'analytics' : $this->settings['element']; return $this->View->element($element, compact('code', 'domain')); } if (is_null($code)) { $code = MiCache::setting('Site.analyticsCode'); if (!$code) { return; } } if (is_null($domain)) { $domain = MiCache::setting('Site.analyticsDomain'); } $this->_template(); $return = String::insert($this->_template, compact('code', 'domain')); if ($domain) { $return = preg_replace('#(<<<domainStart|domainEnd>>>)\\s*#s', '', $return); } else { $return = preg_replace('#<<<domainStart.*domainEnd>>>\\s*#s', '', $return); } return trim($return); }