/** * Save a Field to the database * * @param Field|Saveable $item The field to save * @return bool True on success, false on failure * @throws WireException * */ public function ___save(Saveable $item) { if ($item->flags & Field::flagFieldgroupContext) { throw new WireException("Field {$item} is not saveable because it is in a specific context"); } if (!strlen($item->name)) { throw new WireException("Field name is required"); } $database = $this->wire('database'); $isNew = $item->id < 1; $prevTable = $database->escapeTable($item->prevTable); $table = $database->escapeTable($item->getTable()); if (!$isNew && $prevTable && $prevTable != $table) { // note that we rename the table twice in order to force MySQL to perform the rename // even if only the case has changed. $database->exec("RENAME TABLE `{$prevTable}` TO `tmp_{$table}`"); // QA $database->exec("RENAME TABLE `tmp_{$table}` TO `{$table}`"); // QA $item->prevTable = ''; } if ($item->prevFieldtype && $item->prevFieldtype->name != $item->type->name) { if (!$this->changeFieldtype($item)) { $item->type = $item->prevFieldtype; $this->error("Error changing fieldtype for '{$item}', reverted back to '{$item->type}'"); } else { $item->prevFieldtype = null; } } if (!$item->type) { throw new WireException("Can't save a Field that doesn't have it's 'type' property set to a Fieldtype"); } if (!parent::___save($item)) { return false; } if ($isNew) { $item->type->createField($item); } if ($item->flags & Field::flagGlobal) { // make sure that all template fieldgroups contain this field and add to any that don't. foreach (wire('templates') as $template) { if ($template->noGlobal) { continue; } $fieldgroup = $template->fieldgroup; if (!$fieldgroup->hasField($item)) { $fieldgroup->add($item); $fieldgroup->save(); if (wire('config')->debug) { $this->message("Added field '{$item->name}' to template/fieldgroup '{$fieldgroup->name}'"); } } } } return true; }
/** * Save the provided item to database * * @param Saveable $item * @return bool * @throws WireException * */ public function ___save(Saveable $item) { if (!$item instanceof HasLookupItems) { throw new WireException($this->className() . "::save() requires an item that implements HasLookupItems interface"); } $database = $this->wire('database'); $lookupTable = $database->escapeTable($this->getLookupTable()); $lookupField = $database->escapeCol($this->getLookupField()); $table = $database->escapeTable($this->getTable()); $item_id = (int) $item->id; if ($item_id) { $query = $database->prepare("DELETE FROM {$lookupTable} WHERE {$table}_id=:item_id"); $query->bindValue(":item_id", $item_id, PDO::PARAM_INT); $query->execute(); } $result = parent::___save($item); $item_id = (int) $item->id; // reload, in case it was 0 before $sort = 0; if ($item_id) { foreach ($item->getLookupItems() as $key => $value) { $value_id = (int) $value->id; $query = $database->prepare("INSERT INTO {$lookupTable} SET {$table}_id=:item_id, {$lookupField}=:value_id, sort=:sort"); $query->bindValue(":item_id", $item_id); $query->bindValue(":value_id", $value_id); $query->bindValue(":sort", $sort); $query->execute(); $this->resetTrackChanges(); $sort++; } } 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. * */ 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; }
/** * Per WireSaveableItems interface, save a Field to the database * */ public function ___save(Saveable $item) { if ($item->prevTable && strtolower($item->prevTable) != strtolower($item->getTable())) { $this->fuel('db')->query("RENAME TABLE {$item->prevTable} TO " . $item->getTable()); $item->prevTable = ''; } if ($item->prevFieldtype && $item->prevFieldtype->name != $item->type->name) { if (!$this->changeFieldtype($item)) { $item->type = $item->prevFieldtype; $this->error("Error changing fieldtype for '{$item}', reverted back to '{$item->type}'"); } else { $item->prevFieldtype = null; } } $isNew = !$item->id; if (!$item->type) { throw new WireException("Can't save a Field that doesn't have it's 'type' property set to a Fieldtype"); } if (!parent::___save($item)) { return false; } if ($isNew) { $item->type->createField($item); } return true; }
/** * Save the provided item to database * */ public function ___save(Saveable $item) { if (!$item instanceof HasLookupItems) { throw new WireException($this->className() . "::save() requires an item that implements HasLookupItems interface"); } $lookupTable = $this->getLookupTable(); $lookupField = $this->getLookupField(); $table = $this->getTable(); if ($item->id) { $this->fuel('db')->query("DELETE FROM {$lookupTable} WHERE {$table}_id={$item->id}"); } $result = parent::___save($item); // if($item->id) foreach($item->get($lookupField) as $key => $value) { $sort = 0; if ($item->id) { foreach ($item->getLookupItems() as $key => $value) { $this->fuel('db')->query("INSERT INTO {$lookupTable} SET {$table}_id='{$item->id}', {$lookupField}='{$value->id}', sort='{$sort}'"); $this->resetTrackChanges(); $sort++; } } return $result; }