/** * 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; }
/** * Save the provided User item to the database * */ public function ___save(Saveable $item) { if (!$item instanceof User || $item instanceof NullUser) { throw new WireException("Item passed Users::save is not a User"); } if ($item->isChanged('pass') || !$item->id) { $item->set('salt', md5(mt_rand() . microtime())); $item->set('pass', $this->passHash($item->get('pass'), $item)); } // The owner role is for runtime use only, so users may not be saved with the User role if ($item->hasRole(Role::ownerRoleID)) { $item->removeRole(Role::ownerRoleID); } return parent::___save($item); }