/** * Update or insert template to database * * If the template's fieldgroup has changed, then we delete data that's no longer applicable to the new fieldgroup. * */ public function ___save(Saveable $item) { if (!$item->fieldgroup->id) { throw new WireException("You must save Fieldgroup '{$item->fieldgroup}' before adding to Template '{$item}'"); } $result = parent::___save($item); if ($result && $item->fieldgroupPrevious && $item->fieldgroupPrevious->id != $item->fieldgroup->id) { // the fieldgroup has been changed // remove data from all fields that are not part of the new fieldgroup $removeFields = new FieldsArray(); foreach ($item->fieldgroupPrevious as $field) { if (!$item->fieldgroup->has($field)) { $removeFields->add($field); } } if (count($removeFields)) { $pages = $this->fuel('pages')->find("templates_id={$item->id}"); foreach ($pages as $page) { foreach ($removeFields as $field) { $field->type->deletePageField($page, $field); if ($this->config->debug) { $this->message("Removed field '{$field}' on page '{$page->url}'"); } } } } } return $result; }
/** * Update or insert template to database * * If the template's fieldgroup has changed, then we delete data that's no longer applicable to the new fieldgroup. * * @param Saveable|Template $item * @return bool true on success * @throws WireException * */ public function ___save(Saveable $item) { $isNew = $item->id < 1; if (!$item->fieldgroup) { throw new WireException("Template '{$item}' cannot be saved because it has no fieldgroup assigned"); } if (!$item->fieldgroup->id) { throw new WireException("You must save Fieldgroup '{$item->fieldgroup->name}' before adding to Template '{$item}'"); } $rolesChanged = $item->isChanged('useRoles'); if ($this->wire('pages')->get("/")->template->id == $item->id) { if (!$item->useRoles) { throw new WireException("Template '{$item}' is used by the homepage and thus must manage access"); } if (!$item->hasRole("guest")) { throw new WireException("Template '{$item}' is used by the homepage and thus must have the 'guest' role assigned."); } } if (!$item->isChanged('modified')) { $item->modified = time(); } $result = parent::___save($item); if ($result && !$isNew && $item->fieldgroupPrevious && $item->fieldgroupPrevious->id != $item->fieldgroup->id) { // the fieldgroup has been changed // remove data from all fields that are not part of the new fieldgroup $removeFields = new FieldsArray(); foreach ($item->fieldgroupPrevious as $field) { if (!$item->fieldgroup->has($field)) { $removeFields->add($field); } } if (count($removeFields)) { foreach ($removeFields as $field) { $field->type->deleteTemplateField($item, $field); } /* $pages = $this->fuel('pages')->find("templates_id={$item->id}, check_access=0, status<" . Page::statusMax); foreach($pages as $page) { foreach($removeFields as $field) { $field->type->deletePageField($page, $field); if($this->fuel('config')->debug) $this->message("Removed field '$field' on page '{$page->url}'"); } } */ } } if ($rolesChanged) { $access = new PagesAccess(); $access->updateTemplate($item); } $this->wire('cache')->maintenance($item); return $result; }