function page_model_test($delete) { include_once "adventure_model.php"; $page = new PageModel("Sample Page Text" . rand(0, 100000), AdventureModel::first()->id, "images/test.png"); $page->save(); $page->print_fields(); $page->set("page_text", "Updated Page Text"); $page->save(); $page->print_fields(); if ($delete) { $page->delete(); } $pm = PageModel::find(PageModel::last()->id); $pm->print_fields(); PageModel::find(999); }
public function view(){ $v = $this->getView(); $pages = PageModel::Find(array('admin' => '1')); $groups = array(); $flatlist = array(); if(isset($_SESSION['user_sudo'])){ $p = new PageModel('/user/sudo'); $p->set('title', 'Exit SUDO Mode'); $groups['SUDO'] = [ 'title' => 'SUDO', 'href' => '', 'children' => [ 'Exit SUDO Mode' => $p, ], ]; $flatlist[ 'Exit SUDO Mode' ] = $p; } if(\Core\user()){ foreach($pages as $p){ /** @var PageModel $p */ if(!\Core\user()->checkAccess($p->get('access'))) continue; // Pages can define which sub-menu they get grouped under. // The 'Admin' submenu is the default. $group = $p->get('admin_group') ? $p->get('admin_group') : 't:STRING_ADMIN'; // Support i18n here! if(strpos($group, 't:') === 0){ $group = t(substr($group, 2)); } if(!isset($groups[$group])){ $groups[$group] = [ 'title' => $group, 'href' => '', 'children' => [], ]; } if($p->get('baseurl') == '/admin'){ // Admin gets special treatment. $groups[t('STRING_ADMIN')]['href'] = '/admin'; continue; } switch($p->get('title')){ case 'System Configuration': $p->set('title', "System Config"); break; case 'Navigation Listings': $p->set('title', "Navigation"); break; case 'Content Page Listings': $p->set('title', "Content Pages"); break; default: $p->set( 'title', trim( str_replace(['Administration', 'Admin'],'', $p->get('title')) ) ); } $title = $p->get('title'); // Support i18n here! if(strpos($title, 't:') === 0){ $title = t(substr($title, 2)); } if(isset($groups[$title])){ // Link the main group to this page instead of an empty link. // This removes duplicate links such as the group "User" and page "User". $groups[$title]['href'] = $p->get('rewriteurl'); } else{ // The new grouped pages $groups[$group]['children'][ $title ] = $p; // And the flattened list to support legacy templates. $flatlist[ $title ] = $p; } } // This is a hack to make sure that users can view the /admin link if they can view other admin pages. /*if(sizeof($flatlist) && !isset($groups['Admin']['Dashboard'])){ $p = new PageModel('/admin'); $p->set('title', 'Dashboard'); $groups['Admin']['Dashboard'] = $p; }*/ } ksort($flatlist); ksort($groups); foreach($groups as $gname => $dat){ ksort($groups[$gname]['children']); } // Build a list of languages that can be set by the user. $locales = \Core\i18n\I18NLoader::GetLocalesEnabled(); $selected = \Core\i18n\I18NLoader::GetUsersLanguage(); $languages = []; if(sizeof($locales) > 1){ // There is at least 1 language available on the system, YAY! foreach($locales as $localeKey => $localeDat){ if(($pos = strpos($localeKey, '_')) !== false){ // This locale contains an underscore, that means it has a corresponding country! // These are what we want to display to the end user. $country = substr($localeKey, $pos+1); // Here I am retrieving the language and dialect in the native dialect if at all possible. // This is because if you as a user only can read your native language and your browser renders something different, // then you want to be able to read what you're switching it to. $str1 = new \Core\i18n\I18NString($localeDat['lang']); $str1->setLanguage($localeKey); $localeTitle = $str1->getTranslation(); if($localeDat['dialect']){ $str2 = new \Core\i18n\I18NString($localeDat['dialect']); $str2->setLanguage($localeKey); $localeTitle .= ' (' . $str2->getTranslation() . ')'; } $languages[] = [ 'key' => $localeKey, 'title' => $localeTitle, 'country' => $country, 'image' => 'assets/images/iso-country-flags/' . strtolower($country) . '.png', 'selected' => $localeKey == $selected, ]; } } } $v->templatename = 'widgets/adminmenu/view.tpl'; $v->assign('pages', $flatlist); $v->assign('groups', $groups); $v->assign('languages', $languages); return $v; }
/** * Get the model for the page subform. * This is on the group because a page object can be set embedded in another form. * * @param null $page * * @return null|PageModel */ public function getModel($page = null) { // Allow linked models. if (!$page) $page = new PageModel($this->get('baseurl')); // Because name can be changed. $name = $this->_attributes['name']; $els = $this->getElements(true, false); foreach ($els as $e) { /** @var $e FormElement */ if (!preg_match('/^[a-z_]*\[(.*?)\].*/', $e->get('name'), $matches)) continue; $key = $matches[1]; $val = $e->get('value'); // Meta attributes if(strpos($e->get('name'), $name . '_meta') === 0){ $val = $e->get('value'); $page->setMeta($key, $val); } elseif(strpos($e->get('name'), $name) === 0){ $page->set($key, $val); } else{ continue; } } return $page; // Add in any insertables too, if they're attached. // DISABLING 2012.05 cpowell /* if(($i = $this->getElementByName('insertables'))){ $els = $i->getElements(); foreach($els as $e){ if(!preg_match('#^insertable\[(.*?)\].*#', $e->get('name'), $matches)) continue; $submodel = $page->findLink('Insertable', array('name' => $matches[1])); $submodel->set('value', $e->get('value')); } } return $page; */ }
/** * Shortcut for unpublishing a page. */ public function page_unpublish() { $view = $this->getView(); $request = $this->getPageRequest(); if(!\Core\user()->checkAccess('p:/core/pages/manage')){ return View::ERROR_ACCESSDENIED; } $baseurl = $request->getParameter('baseurl'); $page = new PageModel($baseurl); if(!$page->exists()){ return View::ERROR_NOTFOUND; } if(!$request->isPost()){ return View::ERROR_BADREQUEST; } // Is this page already un-published? if($page->get('published_status') == 'draft'){ \Core\set_message('t:MESSAGE_ERROR_PAGE_ALREADY_UNPUBLISHED'); \Core\go_back(); } $page->set('published_status', 'draft'); $page->save(); \Core\set_message('t:MESSAGE_SUCCESS_PAGE_UNPUBLISHED'); \Core\go_back(); }
/** * Get the page model for the current page. * * @return PageModel */ public function getPageModel() { if ($this->_pagemodel === null) { $uri = $this->uriresolved; $pagefac = new ModelFactory('PageModel'); $pagefac->where('rewriteurl = ' . $uri); //$pagefac->where('fuzzy = 0'); $pagefac->limit(1); if(Core::IsComponentAvailable('multisite') && MultiSiteHelper::IsEnabled()){ $pagefac->whereGroup('OR', array('site = -1', 'site = ' . MultiSiteHelper::GetCurrentSiteID())); } $p = $pagefac->get(); // Split this URL, it'll be used somewhere. $pagedat = $this->splitParts(); if ($p) { // :) Found it $this->_pagemodel = $p; } elseif ($pagedat && isset($pagedat['baseurl'])) { // Is this even a valid controller? // This will allow a page to be called with it being in the pages database. $p = new PageModel($pagedat['baseurl']); if(!$p->exists()){ $p->set('rewriteurl', $pagedat['rewriteurl']); } $this->_pagemodel = $p; } else { // No page in the database and no valid controller... sigh $this->_pagemodel = new PageModel(); } //var_dump($p); die(); // Make sure all the parameters from both standard GET and core parameters are tacked on. if ($pagedat && $pagedat['parameters']) { foreach ($pagedat['parameters'] as $k => $v) { $this->_pagemodel->setParameter($k, $v); } } if (is_array($_GET)) { foreach ($_GET as $k => $v) { if (is_numeric($k)) continue; $this->_pagemodel->setParameter($k, $v); } } } return $this->_pagemodel; }
/** * Internal function to parse and handle the configs in the component.xml file. * This is used for installations and upgrades. * * @param boolean $install Set to false to force uninstall/disable mode. * @param int $verbosity (default 0) 0: standard output, 1: real-time, 2: real-time verbose output. * * @return boolean | int * @throws InstallerException */ public function _parsePages($install = true, $verbosity = 0) { $changes = array(); $overallAction = $install ? 'Installing' : 'Uninstalling'; Core\Utilities\Logger\write_debug($overallAction . ' pages for ' . $this->getName()); // I need to get the schema definitions first. $node = $this->_xmlloader->getElement('pages'); //$prefix = $node->getAttribute('prefix'); // Now, get every table under this node. foreach ($node->getElementsByTagName('page') as $subnode) { /** @var DomElement $subnode */ $baseurl = $subnode->getAttribute('baseurl'); // Insert/Update the defaults for an entry in the database. // These are always global pages. $m = new PageModel(-1, $baseurl); if($verbosity == 2){ CLI::PrintActionStart($overallAction . ' page ' . $baseurl); } // Hard-set pages get removed upon disabling. They'll be recreated if re-enabled. if($install){ // Just something to help the log. $action = ($m->exists()) ? 'Updated' : 'Added'; $admin = $subnode->getAttribute('admin'); $selectable = ($admin ? '0' : '1'); // Defaults $group = ($admin ? $subnode->getAttribute('group') : ''); if($subnode->getAttribute('selectable') !== ''){ $selectable = $subnode->getAttribute('selectable'); } $indexable = ($subnode->getAttribute('indexable') !== '') ? $subnode->getAttribute('indexable') : $selectable; $editurl = $subnode->getAttribute('editurl') ? $subnode->getAttribute('editurl') : ''; $access = ($subnode->getAttribute('access')) ? $subnode->getAttribute('access') : null; // Do not "update" value, keep whatever the user set previously. if (!$m->get('rewriteurl')) { if ($subnode->getAttribute('rewriteurl')) $m->set('rewriteurl', $subnode->getAttribute('rewriteurl')); else $m->set('rewriteurl', $subnode->getAttribute('baseurl')); } // Do not "update" value, keep whatever the user set previously. if (!$m->get('title')) $m->set('title', $subnode->getAttribute('title')); if($access !== null){ $m->set('access', $access); } // Do not update parent urls if the page already exists. if(!$m->exists()) $m->set('parenturl', $subnode->getAttribute('parenturl')); //$m->set('widget', $subnode->getAttribute('widget')); $m->set('admin', $admin); $m->set('admin_group', $group); $m->set('selectable', $selectable); $m->set('indexable', $indexable); $m->set('component', $this->getKeyName()); $m->set('editurl', $editurl); if ($m->save()){ $changes[] = $action . ' page [' . $baseurl . ']'; if($verbosity == 2){ CLI::PrintActionStatus(true); } } else{ if($verbosity == 2){ CLI::PrintActionStatus('skip'); } } } else{ $m->delete(); $changes[] = 'Removed page [' . $subnode->getAttribute('baseurl') . ']'; if($verbosity == 2){ CLI::PrintActionStatus(true); } } } return ($changes > 0) ? $changes : false; }