1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-12 09:44:38 +02:00

Some updates to PagesType class and update all PagesType hooks from deprecated to fully functional per processwire/processwire-issues#360

This commit is contained in:
Ryan Cramer
2019-03-26 10:08:26 -04:00
parent 768b27f4c8
commit f121425f0d
4 changed files with 137 additions and 50 deletions

View File

@@ -138,7 +138,15 @@ class Pages extends Wire {
* @var PagesTrash
*
*/
protected $trasher;
protected $trasher;
/**
* Array of PagesType managers
*
* @var PagesType[]
*
*/
protected $types = array();
/**
* Create the Pages object
@@ -1385,6 +1393,24 @@ class Pages extends Wire {
return $this->trasher;
}
/**
* Get array of all PagesType managers
*
* #pw-internal
*
* @param PagesType|string Specify a PagesType object to add a Manager, or specify class name to retrieve manager
* @return array|PagesType|null|bool Returns requested type, null if not found, or boolean true if manager added.
*
*/
public function types($type = null) {
if(!$type) return $this->types;
if(is_string($type)) return isset($this->types[$type]) ? $this->types[$type] : null;
if(!$type instanceof PagesType) return null;
$name = $type->className();
$this->types[$name] = $type;
return true;
}
/**
* Get or set debug state
*
@@ -1428,9 +1454,8 @@ class Pages extends Wire {
/** @var WireCache $cache */
$cache = $this->wire('cache');
$cache->maintenance($page);
if($page->className() != 'Page') {
$manager = $page->getPagesManager();
if($manager instanceof PagesType) $manager->saved($page, $changes, $values);
foreach($this->types as $manager) {
if($manager->hasValidTemplate($page)) $manager->saved($page, $changes, $values);
}
}
@@ -1444,9 +1469,8 @@ class Pages extends Wire {
*/
public function ___added(Page $page) {
$this->log("Added page", $page);
if($page->className() != 'Page') {
$manager = $page->getPagesManager();
if($manager instanceof PagesType) $manager->added($page);
foreach($this->types as $manager) {
if($manager->hasValidTemplate($page)) $manager->added($page);
}
$page->setQuietly('_added', true);
}
@@ -1526,9 +1550,10 @@ class Pages extends Wire {
*/
public function ___saveReady(Page $page) {
$data = array();
if($page->className() != 'Page') {
$manager = $page->getPagesManager();
if($manager instanceof PagesType) $data = $manager->saveReady($page);
foreach($this->types as $manager) {
if(!$manager->hasValidTemplate($page)) continue;
$a = $manager->saveReady($page);
if(!empty($a) && is_array($a)) $data = array_merge($data, $a);
}
return $data;
}
@@ -1545,9 +1570,8 @@ class Pages extends Wire {
*
*/
public function ___deleteReady(Page $page) {
if($page->className() != 'Page') {
$manager = $page->getPagesManager();
if($manager instanceof PagesType) $manager->deleteReady($page);
foreach($this->types as $manager) {
if($manager->hasValidTemplate($page)) $manager->deleteReady($page);
}
}
@@ -1564,9 +1588,8 @@ class Pages extends Wire {
/** @var WireCache $cache */
$cache = $this->wire('cache');
$cache->maintenance($page);
if($page->className() != 'Page') {
$manager = $page->getPagesManager();
if($manager instanceof PagesType) $manager->deleted($page);
foreach($this->types as $manager) {
if($manager->hasValidTemplate($page)) $manager->deleted($page);
}
}

View File

@@ -85,6 +85,8 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
$this->setWire($wire);
$this->addTemplates($templates);
$this->addParents($parents);
$wire->pages->types($this);
parent::__construct();
}
/**
@@ -113,7 +115,7 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
if($template) $this->templates[$template->id] = $template;
}
}
if(empty($this->template)) $this->template = reset($this->templates); // legacy deprecated
if(empty($this->template)) $this->template = reset($this->templates);
}
/**
@@ -191,30 +193,18 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
public function isValid(Page $page) {
// quick exit when possible
if($this->template->id == $page->template->id && $this->parent_id == $page->parent_id) return true;
$validTemplate = false;
foreach($this->templates as $template) {
if($page->template->id == $template->id) {
$validTemplate = true;
break;
}
if($this->template && $this->template->id === $page->template->id) {
if($this->parent_id && $this->parent_id === $page->parent_id) return true;
}
$validTemplate = $this->hasValidTemplate($page);
if(!$validTemplate && count($this->templates)) {
$validTemplates = implode(', ', array_keys($this->templates));
$this->error("Page $page->path must have template: $validTemplates");
return false;
}
$validParent = false;
foreach($this->parents as $parent_id) {
if($parent_id == $page->parent_id) {
$validParent = true;
break;
}
}
$validParent = $this->hasValidParent($page);
if(!$validParent && count($this->parents)) {
$validParents = implode(', ', $this->parents);
$this->error("Page $page->path must have parent: $validParents");
@@ -224,6 +214,80 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
return true;
}
/**
* Does given Page use a template managed by this type?
*
* #pw-internal
*
* @param Page $page
* @return bool
* @since 3.0.128
*
*/
public function hasValidTemplate(Page $page) {
$tid = (int) $page->templates_id;
if($this->template && count($this->templates) === 1) {
return $this->template->id === $tid;
}
$valid = false;
foreach($this->templates as $template) {
if($tid !== $template->id) continue;
$valid = true;
break;
}
return $valid;
}
/**
* Does given Page have a parent managed by this type?
*
* #pw-internal
*
* @param Page $page
* @return bool
* @since 3.0.128
*
*/
public function hasValidParent(Page $page) {
$parent_id = (int) $page->parent_id;
if($this->parent_id && $this->parent_id === $parent_id) return true;
$valid = false;
foreach($this->parents as $parent_id) {
if($parent_id !== $page->parent_id) continue;
$valid = true;
break;
}
return $valid;
}
/**
* Does given Page have a Page class name managed by this type?
*
* #pw-internal
*
* @param Page $page
* @return bool
* @since 3.0.128
*
*/
public function hasValidClass(Page $page) {
$pageClass = $page->className();
if($this->pageClass && $pageClass === $this->pageClass) return true;
$valid = false;
foreach($this->templates as $template) {
/** @var Template $template */
if($template->pageClass) {
// template specifies a class
if($template->pageClass === $pageClass) $valid = true;
} else {
// template specifies NO Page class, which implies "Page" as a class name is valid
if($pageClass === 'Page') $valid = true;
}
if($valid) break;
}
return $valid;
}
/**
* Get options that will be passed to Pages::getById()
*
@@ -564,13 +628,13 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
*/
/**
* Hook called just before a page is saved
* Hook called just before a page of this type is saved
*
* #pw-internal
* #pw-hooker
*
* @param Page $page The page about to be saved
* @return array Optional extra data to add to pages save query.
* @deprecated
* @since 3.0.128
*
*/
public function ___saveReady(Page $page) {
@@ -579,28 +643,25 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
}
/**
* Hook called after a page is successfully saved
* Hook called after a page of this type is successfully saved
*
* This is the same as Pages::save, except that it occurs before other save-related hooks (below),
* Whereas Pages::save occurs after. In most cases, the distinction does not matter.
*
* #pw-internal
* #pw-hooker
*
* @param Page $page The page that was saved
* @param array $changes Array of field names that changed
* @param array $values Array of values that changed, if values were being recorded, see Wire::getChanges(true) for details.
* @deprecated
* @since 3.0.128
*
*/
public function ___saved(Page $page, array $changes = array(), $values = array()) { }
/**
* Hook called when a new page has been added
* Hook called when a new page of this type has been added
*
* #pw-internal
* #pw-hooker
*
* @param Page $page
* @deprecated
* @since 3.0.128
*
*/
public function ___added(Page $page) { }
@@ -608,10 +669,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
/**
* Hook called when a page is about to be deleted, but before data has been touched
*
* #pw-internal
* #pw-hooker
*
* @param Page $page
* @deprecated
* @since 3.0.128
*
*/
public function ___deleteReady(Page $page) { }
@@ -619,9 +680,10 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
/**
* Hook called when a page and its data have been deleted
*
* #pw-internal
* #pw-hooker
*
* @param Page $page
* @since 3.0.128
*
*/
public function ___deleted(Page $page) { }

View File

@@ -157,7 +157,7 @@ class Users extends PagesType {
$role = $this->wire('roles')->get($this->wire('config')->guestUserRolePageID);
if($role->id && !$user->hasRole($role)) $user->addRole($role);
}
return array();
return parent::___saveReady($user);
}
}

View File

@@ -533,6 +533,7 @@ class Languages extends PagesType {
*/
public function ___deleted(Page $language) {
$this->updated($language, 'deleted');
parent::___deleted($language);
}
/**
@@ -545,6 +546,7 @@ class Languages extends PagesType {
*/
public function ___added(Page $language) {
$this->updated($language, 'added');
parent::___added($language);
}
/**