Example #1
0
 /**
  * Save the context of the given field for the given fieldgroup
  *
  * @param Field $field Field to save context for
  * @param Fieldgroup $fieldgroup Context for when field is in this fieldgroup
  * @return bool True on success
  * @throws WireException
  *
  */
 public function ___saveFieldgroupContext(Field $field, Fieldgroup $fieldgroup)
 {
     // get field without contxt
     $fieldOriginal = wire('fields')->get($field->name);
     $data = array();
     // make sure given field and fieldgroup are valid
     if (!($field->flags & Field::flagFieldgroupContext)) {
         throw new WireException("Field must be in fieldgroup context before its context can be saved");
     }
     if (!$fieldgroup->has($fieldOriginal)) {
         throw new WireException("Fieldgroup {$fieldgroup} does not contain field {$field}");
     }
     $newValues = $field->getArray();
     $oldValues = $fieldOriginal->getArray();
     // 0 is the same as 100 for columnWidth, so we specifically set it just to prevent this from being saved when it doesn't need to be
     if (!isset($oldValues['columnWidth'])) {
         $oldValues['columnWidth'] = 100;
     }
     // add the label and description built-in fields
     foreach (array('label', 'description') as $key) {
         $newValues[$key] = $field->{$key};
         $oldValues[$key] = $fieldOriginal->{$key};
     }
     // cycle through and determine which values should be saved
     foreach ($newValues as $key => $value) {
         $oldValue = empty($oldValues[$key]) ? '' : $oldValues[$key];
         // if both old and new are empty, then don't store a blank value in the context
         if (empty($oldValue) && empty($value)) {
             continue;
         }
         // if old and new value are the same, then don't duplicate the value in the context
         if ($value == $oldValue) {
             continue;
         }
         // $value differs from $oldValue and should be saved
         $data[$key] = $value;
     }
     // keep all in the same order so that it's easier to compare (by eye) in the DB
     ksort($data);
     // inject updated context back into model
     $fieldgroup->setFieldContextArray($field->id, $data);
     // if there is something in data, then JSON encode it. If it's empty then make it null.
     $data = count($data) ? wireEncodeJSON($data, true) : null;
     if (is_null($data)) {
         $data = 'NULL';
     } else {
         $data = "'" . $this->wire('database')->escapeStr($data) . "'";
     }
     $field_id = (int) $field->id;
     $fieldgroup_id = (int) $fieldgroup->id;
     $database = $this->wire('database');
     $query = $database->prepare("UPDATE fieldgroups_fields SET data={$data} WHERE fields_id=:field_id AND fieldgroups_id=:fieldgroup_id");
     // QA
     $query->bindValue(':field_id', $field_id, PDO::PARAM_INT);
     $query->bindValue(':fieldgroup_id', $fieldgroup_id, PDO::PARAM_INT);
     $result = $query->execute();
     return $result;
 }
 /**
  * Change a field's type
  *
  */
 protected function ___changeFieldtype(Field $field1)
 {
     if (!$field1->prevFieldtype) {
         throw new WireException("changeFieldType requires that the given field has had a type change");
     }
     if ($field1->type instanceof FieldtypeMulti && !$field1->prevFieldtype instanceof FieldtypeMulti || $field1->prevFieldtype instanceof FieldtypeMulti && !$field1->type instanceof FieldtypeMulti) {
         throw new WireException("Cannot convert between single and multiple value field types");
     }
     $field2 = clone $field1;
     $field2->name = $field2->name . "_PWTMP";
     $field2->type->createField($field2);
     $field1->type = $field1->prevFieldtype;
     $schema1 = array();
     $schema2 = array();
     $result = $this->db->query("DESCRIBE `{$field1->table}`");
     while ($row = $result->fetch_assoc()) {
         $schema1[] = $row['Field'];
     }
     $result->free();
     $result = $this->db->query("DESCRIBE `{$field2->table}`");
     while ($row = $result->fetch_assoc()) {
         $schema2[] = $row['Field'];
     }
     $result->free();
     foreach ($schema1 as $key => $value) {
         if (!in_array($value, $schema2)) {
             if ($this->config->debug) {
                 $this->message("changeFieldType loses table field '{$value}'");
             }
             unset($schema1[$key]);
         }
     }
     $sql = "INSERT INTO `{$field2->table}` (`" . implode('`,`', $schema1) . "`) " . "SELECT `" . implode('`,`', $schema1) . "` FROM `{$field1->table}` ";
     try {
         $result = $this->db->query($sql);
     } catch (WireDatabaseException $e) {
         $result = false;
     }
     if (!$result) {
         $this->error("Field type change failed. Database reports: {$this->db->error}");
         $this->db->query("DROP TABLE `{$field2->table}`");
         return false;
     }
     $this->db->query("DROP TABLE `{$field1->table}`");
     $this->db->query("RENAME TABLE `{$field2->table}` TO `{$field1->table}`");
     $field1->type = $field2->type;
     // clear out the custom data, which contains settings specific to the Inputfield and Fieldtype
     foreach ($field1->getArray() as $key => $value) {
         // skip fields that may be shared among any fieldtype
         if (in_array($key, array('description', 'required', 'collapsed', 'notes'))) {
             continue;
         }
         // remove the custom field
         $field1->remove($key);
     }
     return true;
 }