Ejemplo n.º 1
0
 /**
  * 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;
 }
Ejemplo n.º 2
0
 /**
  * Save the provided item to database
  *
  */
 public function ___save(Saveable $item)
 {
     $blank = $this->makeBlankItem();
     if (!$item instanceof $blank) {
         throw new WireException("WireSaveableItems::save(item) requires item to be of type '" . $blank->className() . "'");
     }
     $db = $this->getFuel('db');
     $table = $this->getTable();
     $sql = "`{$table}` SET ";
     $id = (int) $item->id;
     $data = $item->getTableData();
     foreach ($data as $key => $value) {
         if (!$this->saveItemKey($key)) {
             continue;
         }
         if ($key == 'data') {
             if (is_array($value)) {
                 $value = $this->encodeData($value);
             } else {
                 $value = '';
             }
         }
         $sql .= "`{$key}`='" . $db->escape_string("{$value}") . "', ";
     }
     $sql = rtrim($sql, ", ");
     if ($id) {
         $result = $db->query("UPDATE {$sql} WHERE id={$id}");
     } else {
         $result = $db->query("INSERT INTO {$sql}");
         if ($result) {
             $item->id = $db->insert_id;
             $this->getAll()->add($item);
         }
     }
     if ($result) {
         $this->resetTrackChanges();
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Delete a template and unset it from this object. 
  *
  */
 public function ___delete(Saveable $item)
 {
     $cnt = $item->getNumPages();
     if ($cnt > 0) {
         throw new WireException("Can't delete template '{$item->name}' because it is used by {$cnt} pages.");
     }
     return parent::___delete($item);
 }
Ejemplo n.º 4
0
 /**
  * Delete a template and unset it from this object. 
  *
  */
 public function ___delete(Saveable $item)
 {
     if ($item->flags & Template::flagSystem) {
         throw new WireException("Can't delete template '{$item->name}' because it is a system template.");
     }
     $cnt = $item->getNumPages();
     if ($cnt > 0) {
         throw new WireException("Can't delete template '{$item->name}' because it is used by {$cnt} pages.");
     }
     $return = parent::___delete($item);
     $this->wire('cache')->maintenance($item);
     return $return;
 }
Ejemplo n.º 5
0
 public function getForeignItems(Saveable $class, $relation_details)
 {
     if (!$class->getB2DBID()) {
         return array();
     }
     list($criteria, $item_class, $item_column) = $this->generateForeignItemsCriteria($class, $relation_details);
     if (!$item_class) {
         $items = array();
         $resultset = $this->doSelect($criteria);
         if ($resultset) {
             $column = "{$this->getB2DBName()}." . $relation_details['column'];
             while ($row = $resultset->getNextRow()) {
                 $items[] = $row->get($column);
             }
         }
         return $items;
     } elseif (!$relation_details['manytomany']) {
         return $this->select($criteria);
     } else {
         $resultset = $this->doSelect($criteria);
         return $this->_populateFromResultset($resultset, $item_class, $item_column, $item_column);
     }
 }
Ejemplo n.º 6
0
 /**
  * Save the Fieldgroup to DB
  *
  * If fields were removed from the Fieldgroup, then track them down and remove them from the associated field_* tables
  *
  * @param Saveable $item Fieldgroup to save
  * @return bool True on success, false on failure
  *
  */
 public function ___save(Saveable $item)
 {
     if ($item->id && $item->removedFields) {
         foreach ($this->fuel('templates') as $template) {
             if ($template->fieldgroup->id !== $item->id) {
                 continue;
             }
             foreach ($item->removedFields as $field) {
                 if ($field->flags & Field::flagGlobal && !$template->noGlobal) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$item->name}' because it is globally required (Field::flagGlobal)");
                 }
                 if ($field->flags & Field::flagPermanent) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$item->name}' because it is permanent.");
                 }
                 $pages = $this->fuel('pages')->find("templates_id={$template->id}, check_access=0, status<" . Page::statusMax);
                 foreach ($pages as $page) {
                     try {
                         $field->type->deletePageField($page, $field);
                         $page->save($field->name);
                         if ($this->fuel('config')->debug) {
                             $this->message("Deleted '{$field->name}' from '{$page->path}'");
                         }
                     } catch (Exception $e) {
                         $this->error($e->getMessage());
                     }
                 }
                 $item->finishRemove($field);
             }
         }
     }
     $contextData = array();
     if ($item->id) {
         // save context data
         $result = wire('db')->query("SELECT fields_id, data FROM fieldgroups_fields WHERE fieldgroups_id=" . (int) $item->id);
         while ($row = $result->fetch_assoc()) {
             $contextData[$row['fields_id']] = $row['data'];
         }
     }
     $result = parent::___save($item);
     if (count($contextData)) {
         // restore context data
         foreach ($contextData as $fields_id => $data) {
             $data = wire('db')->escape_string($data);
             wire('db')->query("UPDATE fieldgroups_fields SET data='{$data}' WHERE fieldgroups_id={$item->id} AND fields_id={$fields_id}");
         }
     }
     return $result;
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }
Ejemplo n.º 8
0
 /**
  * Save the Fieldgroup to DB
  *
  * If fields were removed from the Fieldgroup, then track them down and remove them from the associated field_* tables
  *
  * @param Saveable $item Fieldgroup to save
  * @return bool True on success, false on failure
  * @throws WireException
  *
  */
 public function ___save(Saveable $item)
 {
     $database = $this->wire('database');
     if ($item->id && $item->removedFields) {
         foreach ($this->wire('templates') as $template) {
             if ($template->fieldgroup->id !== $item->id) {
                 continue;
             }
             foreach ($item->removedFields as $field) {
                 // make sure the field is valid to delete from this template
                 if ($field->flags & Field::flagGlobal && !$template->noGlobal) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$item->name}' because it is globally required (Field::flagGlobal)");
                 }
                 if ($field->flags & Field::flagPermanent) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$item->name}' because it is permanent.");
                 }
                 $field->type->deleteTemplateField($template, $field);
                 $item->finishRemove($field);
             }
         }
         $item->resetRemovedFields();
     }
     $contextData = array();
     if ($item->id) {
         // save context data
         $query = $database->prepare("SELECT fields_id, data FROM fieldgroups_fields WHERE fieldgroups_id=:item_id");
         $query->bindValue(":item_id", (int) $item->id, PDO::PARAM_INT);
         $query->execute();
         while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
             $contextData[$row['fields_id']] = $row['data'];
         }
         $query->closeCursor();
     }
     $result = parent::___save($item);
     if (count($contextData)) {
         // restore context data
         foreach ($contextData as $fields_id => $data) {
             $fieldgroups_id = (int) $item->id;
             $fields_id = (int) $fields_id;
             $query = $database->prepare("UPDATE fieldgroups_fields SET data=:data WHERE fieldgroups_id=:fieldgroups_id AND fields_id=:fields_id");
             // QA
             $query->bindValue(":data", $data, PDO::PARAM_STR);
             $query->bindValue(":fieldgroups_id", $fieldgroups_id, PDO::PARAM_INT);
             $query->bindValue(":fields_id", $fields_id, PDO::PARAM_INT);
             $query->execute();
         }
     }
     return $result;
 }
Ejemplo n.º 9
0
 /**
  * Save the Fieldgroup to DB
  *
  * If fields were removed from the Fieldgroup, then track them down and remove them from the associated field_* tables
  *
  */
 public function ___save(Saveable $item)
 {
     if ($item->removedFields) {
         foreach ($this->fuel('templates') as $template) {
             if ($template->fieldgroup->id !== $item->id) {
                 continue;
             }
             foreach ($item->removedFields as $field) {
                 if ($field->flags & Field::flagGlobal && !$template->noGlobal) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$this}' because it is globally required (Field::flagGlobal)");
                 }
                 if ($field->flags & Field::flagPermanent) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$this}' because it is permanent.");
                 }
                 $pages = $this->fuel('pages')->find("templates_id={$template->id}, check_access=0, status<" . Page::statusMax);
                 foreach ($pages as $page) {
                     try {
                         $field->type->deletePageField($page, $field);
                         $page->save($field->name);
                         if ($this->fuel('config')->debug) {
                             $this->message("Deleted '{$field->name}' from '{$page->path}'");
                         }
                     } catch (Exception $e) {
                         $this->error($e->getMessage());
                     }
                 }
                 $item->finishRemove($field);
             }
         }
     }
     return parent::___save($item);
 }
Ejemplo n.º 10
0
 /** 
  * Delete the provided item from the database, only if it's not a permanent user
  *
  */
 public function ___delete(Saveable $item)
 {
     if (!$item instanceof User || $item instanceof NullUser) {
         throw new WireException("Item passed Users::delete is not a User");
     }
     if ($item->isPermanent()) {
         throw new WireException("User ID {$item->id} is permanent and may not be deleted");
     }
     return parent::___delete($item);
 }
Ejemplo n.º 11
0
 public function generateForeignItemsCriteria(Saveable $class, $relation_details)
 {
     $criteria = $this->getCriteria();
     $foreign_table = $class->getB2DBTable();
     $foreign_table_class = '\\' . get_class($foreign_table);
     $item_class = array_key_exists('class', $relation_details) ? $relation_details['class'] : null;
     $item_column = null;
     $item_table_class = null;
     if ($relation_details['manytomany']) {
         $item_table_class = Core::getCachedB2DBTableClass($item_class);
     }
     if ($relation_details['foreign_column']) {
         $saveable_class = '\\' . get_class($class);
         $table_details = $item_class ? Core::getCachedTableDetails($item_class) : Core::getTableDetails($relation_details['joinclass']);
         if ($relation_details['orderby']) {
             $criteria->addOrderBy("{$table_details['name']}." . $relation_details['orderby']);
         }
         $criteria->addWhere("{$table_details['name']}." . $relation_details['foreign_column'], $class->getB2DBSaveablePropertyValue(Core::getCachedColumnPropertyName($saveable_class, $foreign_table->getIdColumn())));
         if (array_key_exists('discriminator', $table_details) && $table_details['discriminator'] && array_key_exists($saveable_class, $table_details['discriminator']['discriminators'])) {
             $criteria->addWhere($table_details['discriminator']['column'], $table_details['discriminator']['discriminators'][$saveable_class]);
         }
     } else {
         foreach ($this->getForeignColumns() as $column => $details) {
             if ($details['class'] == $foreign_table_class) {
                 $foreign_column = $details['key'] ? $details['key'] : $foreign_table->getIdColumn();
                 $property_name = Core::getCachedColumnPropertyName(Core::getCachedTableEntityClass($details['class']), $foreign_column);
                 $value = $class->getB2DBSaveablePropertyValue($property_name);
                 $criteria->addWhere($column, $value);
             } elseif ($item_class && $details['class'] == $item_table_class) {
                 $item_column = $column;
             }
         }
         if ($relation_details['orderby']) {
             $criteria->addOrderBy($foreign_table->getB2DBName() . "." . $relation_details['orderby']);
         }
     }
     return array($criteria, $item_class, $item_column);
 }
Ejemplo n.º 12
0
 public final function B2DBpopulateMorphedData(Saveable $original_object, $keep_id = true)
 {
     $this->_preMorph();
     $data = $original_object->getB2DBMorphedDataArray();
     $table = self::getB2DBTable();
     $id_column = $table->getIdColumn();
     foreach ($table->getColumns() as $column) {
         if (!$keep_id && $column['name'] == $id_column) {
             continue;
         }
         $property_name = $column['property'];
         if (!array_key_exists($property_name, $data)) {
             continue;
         }
         $this->{$property_name} = $data[$property_name];
     }
     $this->_postMorph($original_object);
 }
Ejemplo n.º 13
0
 /**
  * Per WireSaveableItemsLookup interface, delete the given role but only if it's not a permament one
  *
  */
 public function ___delete(Saveable $item)
 {
     if (!$item instanceof Role) {
         throw new WireException("Roles::delete requires Role instance");
     }
     if ($item->isPermanent()) {
         throw new WireException("Role {$item->name} is required by the system and may not be deleted");
     }
     $this->fuel('pagesRoles')->deletePagesFromRole($item);
     $this->fuel('db')->query("DELETE from users_roles WHERE roles_id=" . (int) $item->id);
     return parent::___delete($item);
 }
Ejemplo n.º 14
0
 /**
  * Save the Fieldgroup to DB
  *
  * If fields were removed from the Fieldgroup, then track them down and remove them from the associated field_* tables
  *
  * @param Saveable $item Fieldgroup to save
  * @return bool True on success, false on failure
  * @throws WireException
  *
  */
 public function ___save(Saveable $item)
 {
     $database = $this->wire('database');
     if ($item->id && $item->removedFields) {
         foreach ($this->fuel('templates') as $template) {
             if ($template->fieldgroup->id !== $item->id) {
                 continue;
             }
             foreach ($item->removedFields as $field) {
                 if ($field->flags & Field::flagGlobal && !$template->noGlobal) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$item->name}' because it is globally required (Field::flagGlobal)");
                 }
                 if ($field->flags & Field::flagPermanent) {
                     throw new WireException("Field '{$field}' may not be removed from fieldgroup '{$item->name}' because it is permanent.");
                 }
                 $pages = $this->fuel('pages')->find("templates_id={$template->id}, check_access=0, status<" . Page::statusMax);
                 foreach ($pages as $page) {
                     try {
                         $field->type->deletePageField($page, $field);
                         // $page->save($field->name);
                         if ($this->fuel('config')->debug) {
                             $this->message("Deleted '{$field->name}' from '{$page->path}'");
                         }
                     } catch (Exception $e) {
                         $this->error($e->getMessage());
                     }
                 }
                 $item->finishRemove($field);
             }
         }
         $item->resetRemovedFields();
     }
     $contextData = array();
     if ($item->id) {
         // save context data
         $query = $database->prepare("SELECT fields_id, data FROM fieldgroups_fields WHERE fieldgroups_id=:item_id");
         $query->bindValue(":item_id", (int) $item->id, PDO::PARAM_INT);
         $query->execute();
         while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
             $contextData[$row['fields_id']] = $row['data'];
         }
         $query->closeCursor();
     }
     $result = parent::___save($item);
     if (count($contextData)) {
         // restore context data
         foreach ($contextData as $fields_id => $data) {
             $fieldgroups_id = (int) $item->id;
             $fields_id = (int) $fields_id;
             $query = $database->prepare("UPDATE fieldgroups_fields SET data=:data WHERE fieldgroups_id=:fieldgroups_id AND fields_id=:fields_id");
             // QA
             $query->bindValue(":data", $data, PDO::PARAM_STR);
             $query->bindValue(":fieldgroups_id", $fieldgroups_id, PDO::PARAM_INT);
             $query->bindValue(":fields_id", $fields_id, PDO::PARAM_INT);
             $query->execute();
         }
     }
     return $result;
 }