/** * Sync all tables that have the alias field in them * */ public function sync() { $aliases = array(); $db = $this->getDataSource(); foreach ($db->listSources() as $table) { foreach ($db->describe($table) as $column => $description) { if ($column == 'alias') { $aliases = array_merge($aliases, $this->find('all', array('conditions' => array('Alias.plugin' => ZuhaInflector::pluginize($table), 'Alias.controller' => $table, 'Alias.action' => 'view')))); } } } if (!empty($aliases)) { foreach ($aliases as $alias) { $query = __("UPDATE `%s` SET `alias` = '%s' WHERE `%s`.`id` = '%s';", $alias['Alias']['controller'], $alias['Alias']['name'], $alias['Alias']['controller'], $alias['Alias']['value']); $this->query($query); $return[] = $query; } } return $return; }
/** * afterFind callback * * if beforeFind doesn't get the alias, then we need to get it here and add it in * * @param object $Model * @param mixed $results */ public function afterFind(Model $Model, $results, $primary = false) { // look up the alias if it isn't set already if (!empty($results[0][$Model->alias]) && !isset($results[0]['Alias'])) { for ($i = 0, $count = count($results); $i < $count; ++$i) { $alias = $Model->Alias->find('first', array('conditions' => array('Alias.value' => $results[$i][$Model->name]['id'], 'Alias.controller' => Inflector::tableize($Model->name)))); if (!empty($alias)) { $results[$i]['Alias'] = $alias['Alias']; } } } // map the alias to a virtual field for ($i = 0, $count = count($results); $i < $count; ++$i) { if (!empty($results[$i]['Alias'])) { $results[$i][$Model->alias]['_alias'] = '/' . $results[$i]['Alias']['name']; // not dead set on adding the slash } else { $results[$i][$Model->alias]['_alias'] = '/' . strtolower(ZuhaInflector::pluginize($Model->name)) . '/' . Inflector::tableize($Model->name) . '/view/' . $results[$i][$Model->alias]['id']; } } return $results; }
/** * This Builds the tokens that can be replaced in a string. * @param unknown $models * @return multitype: */ public function buildTokens($models = array(), $assoc = false) { foreach ($models as $model) { App::uses($model, ZuhaInflector::pluginize($model) . '.Model'); $Model = new $model(); $this->tokens[$Model->name] = array_keys($Model->schema()); if ($assoc) { $associated = $Model->listAssociatedModels(); foreach ($associated as $assocModel) { $this->tokens[$assocModel] = array_keys($Model->{$assocModel}->schema()); } } } return $this->tokens; }
/** * Tables * * A list of all tables, as keys with their corresponding plugin as values * * @access public * @return array */ public function _tables() { $db = ConnectionManager::getDataSource('default'); foreach ($db->listSources() as $table) { if (strpos($table, 'zbk_') === false) { $plugin = ZuhaInflector::pluginize($table); if (ctype_lower($plugin)) { throw new Exception(__('I bet someone added a db table (%s), without noting it in bootstrap::ZuhaInflector::pluginize. Please check.', $table)); } $tables[$table] = $plugin; } } return $tables; }
/** * Sitemap method * Write the sitemap to the webroot folder */ public function writeSitemap() { App::uses('Folder', 'Utility'); App::uses('File', 'Utility'); $sitemap = array(); $models = App::objects($plugin . '.Model'); $plugins = CakePlugin::loaded(); foreach ($plugins as $plugin) { // the else here was App::objects($pluginPath . '.Model') // not totally sure the changing to just plugin, won't break something $models = !empty($models) ? array_merge($models, App::objects($plugin . '.Model')) : App::objects($plugin . '.Model'); } sort($models); foreach ($models as $model) { strpos($model, 'AppModel') || strpos($model, 'AppModel') === 0 ? null : ($return[$model] = $model); } foreach (array_reverse($return) as $key => $model) { $model = ZuhaInflector::pluginize($model) ? ZuhaInflector::pluginize($model) . '.' . $model : $model; try { $Model = ClassRegistry::init($model); } catch (Exception $e) { $Model = false; // ignore, we don't care about missing plugin exceptions here // debug($e->getMessage()); } if (method_exists($Model, 'sitemap') && is_callable(array($Model, 'sitemap'))) { $map = $Model->sitemap(); $sitemap = is_array($map) ? array_merge($sitemap, $map) : $sitemap; } } // let's output the actual file here $path = ROOT . DS . SITE_DIR . DS . 'Locale' . DS . 'View' . DS . 'webroot'; $dir = new Folder($path); $file = new File($dir->pwd() . DS . 'sitemap.xml'); $content = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL; foreach ($sitemap as $page) { $content .= "\t<url>" . PHP_EOL . "\t\t"; $content .= '<loc>' . $page['url']['loc'] . '</loc>' . PHP_EOL . "\t\t"; $content .= '<lastmod>' . $page['url']['lastmod'] . '</lastmod>' . PHP_EOL . "\t\t"; $content .= '<changefreq>' . $page['url']['changefreq'] . '</changefreq>' . PHP_EOL . "\t\t"; $content .= '<priority>' . $page['url']['priority'] . '</priority>' . PHP_EOL . "\t"; $content .= '</url>' . PHP_EOL; } $content .= '</urlset>'; if ($file->write($content)) { $file->close(); return true; } else { return false; } }
public function addRecursiveData($model, $data) { $Model = ClassRegistry::init(ZuhaInflector::pluginize($model) . '.' . $model); $Model->recursive = 1; $data = $Model->find('first', array('conditions' => array("{$Model->name}.id" => $data[$Model->name]['id']))); return $data; }
/** * Clean data method * * @param array $data * @return array $data */ protected function _cleanData($data) { // handle if we are saving a page automatically and only have the alias if (!empty($data['Alias']['name']) && empty($data['WebpageMenuItem']['item_url'])) { $data['WebpageMenuItem']['item_url'] = '/' . $data['Alias']['name']; } // handle beforeSave() type data if (empty($data['WebpageMenuItem']['parent_id']) && !empty($data['WebpageMenuItem']['menu_id'])) { $data['WebpageMenuItem']['parent_id'] = $data['WebpageMenuItem']['menu_id']; } // handle save() type data if (empty($data['parent_id']) && !empty($data['menu_id'])) { $data['parent_id'] = $data['menu_id']; } // handle beforeSave() type data if (empty($data['WebpageMenuItem']['name']) && !empty($data['WebpageMenuItem']['item_text'])) { $data['WebpageMenuItem']['name'] = $data['WebpageMenuItem']['item_text']; } // handle save() type data if (empty($data['name']) && !empty($data['item_text'])) { $data['name'] = $data['item_text']; } // put data in, to create a check data for whether to create page or not() // it is in the cleanData function because we add some data to the save depending on creation of a page // make sure this gets fired last after all other $data updates if (!empty($data['WebpageMenuItem']['item_url']) && strpos($data['WebpageMenuItem']['item_url'], 'http') !== 0) { // if link_url starts with http do nothing } elseif ($data['WebpageMenuItem']['page_type'] == 'content' || $data['WebpageMenuItem']['page_type'] == 'section' || $data['WebpageMenuItem']['page_type'] == 'plugin') { // NOTE : don't change this if above, if you do installing a new site fails App::uses('Alias', 'Model'); $Alias = new Alias(); // else see if the page already exists $url = strpos($data['WebpageMenuItem']['item_url'], '/') === 0 ? substr($data['WebpageMenuItem']['item_url'], 1) : $data['WebpageMenuItem']['item_url']; $urlAlias = $Alias->getAlias($url); $textAlias = $Alias->getAlias($data['WebpageMenuItem']['item_text']); if (!empty($urlAlias['old']) || !empty($textAlias['old'])) { // if it does we don't need create a page, just move on ignoring the rest $data['WebpageMenuItem']['item_url'] = !empty($data['WebpageMenuItem']['item_url']) ? $data['WebpageMenuItem']['item_url'] : '/' . $textAlias['old']; } elseif ($data['WebpageMenuItem']['page_type'] == 'content' || $data['WebpageMenuItem']['page_type'] == 'section') { // if not then create page (depending on page type) $this->set($data); if ($this->validates()) { // map menu data to webpage data $webpage['Alias']['name'] = empty($data['WebpageMenuItem']['item_url']) ? $Alias->getNewAlias($data['WebpageMenuItem']['item_text']) : null; // if link_url is blank, set the link_url from the name (asciifyy) $webpage['Webpage']['name'] = $data['WebpageMenuItem']['item_text']; $webpage['Webpage']['title'] = $data['WebpageMenuItem']['item_text']; App::uses('Webpage', 'Webpages.Model'); $Webpage = new Webpage(); $webpage = $Webpage->placeholder($webpage, array('create' => true, 'type' => $data['WebpageMenuItem']['page_type'])); unset($webpage['Webpage']); // don't want returned data to save again unset($webpage['Child']); // don't want returned data to save again unset($webpage['Alias']); // don't want returned data to save again $data = Set::merge($data, $webpage); } else { // it isn't going to save anyway, it didn't validate so do nothing, data should be resubmitted } } elseif ($data['WebpageMenuItem']['page_type'] == 'plugin') { $plugin = ZuhaInflector::pluginize($data['WebpageMenuItem']['item_text']); App::uses($plugin . 'AppModel', $plugin . '.Model'); $className = $plugin . 'AppModel'; $Model = new $className(); if (method_exists($Model, 'menuInit')) { // see if the plugin model has a function to generate starting links (note : handle test data in the schema) $data = $Model->menuInit($data); } else { throw new Exception('Create the menuInit() method in the ' . $plugin . 'AppModel file.'); } } } if (!empty($data['WebpageMenuItem']['parent_id']) && empty($data['WebpageMenuItem']['user_role_id'])) { $data['WebpageMenuItem']['user_role_id'] = $this->field('user_role_id', array($this->alias . '.id' => $data['WebpageMenuItem']['parent_id'])); } if (!empty($data['ChildMenuItem'][0])) { for ($i = 0; $i < count($data['ChildMenuItem']); ++$i) { if (empty($data['ChildMenuItem'][$i]['user_role_id']) && !empty($data['WebpageMenuItem']['user_role_id'])) { $data['ChildMenuItem'][$i]['user_role_id'] = $data['WebpageMenuItem']['user_role_id']; } } } return $data; }
/** * Model User Fields * * checks a model for which relationships are with the user model * @param array $sections */ protected function _modelUserFields($sections) { foreach ($sections as $k => $parent) { $modelName = Inflector::classify($parent['Section']['alias']); $plugin = ZuhaInflector::pluginize($modelName); if (in_array($plugin, CakePlugin::loaded())) { $register = !empty($plugin) ? $plugin . '.' . $modelName : $modelName; if ($Model = ClassRegistry::init($register, true)) { $belongs = $Model->belongsTo; foreach ($belongs as $b) { if ($b['className'] == 'Users.User') { $sections[$k]['userFields'][] = $b['foreignKey']; } } } } } return $sections; }