コード例 #1
0
ファイル: Fields.php プロジェクト: avatar382/fablab_site
 /**
  * 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;
 }
コード例 #2
0
ファイル: Fields.php プロジェクト: nightsh/ProcessWire
 /**
  * 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
  *
  */
 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 (strlen("{$value}") && $value != $oldValue) {
             $data[$key] = $value;
         }
     }
     // keep all in the same order so that it's easier to compare (by eye) in the DB
     ksort($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->db->escape_string($data) . "'";
     }
     $result = $this->db->query("UPDATE fieldgroups_fields SET data={$data} WHERE fields_id={$field->id} AND fieldgroups_id={$fieldgroup->id}");
     return $result;
 }