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 = $this->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 built-in fields
     foreach (array('label', 'description', 'notes', 'viewRoles', 'editRoles') as $key) {
         $newValues[$key] = $field->{$key};
         $oldValues[$key] = $fieldOriginal->{$key};
     }
     // account for flags that may be applied as part of context
     $flags = $field->flags & ~Field::flagFieldgroupContext;
     if ($flags != $fieldOriginal->flags) {
         $flagsAdd = 0;
         $flagsDel = 0;
         // flags that are allowed to be set via context
         $contextFlags = array(Field::flagAccess, Field::flagAccessAPI, Field::flagAccessEditor);
         foreach ($contextFlags as $flag) {
             if ($fieldOriginal->hasFlag($flag) && !$field->hasFlag($flag)) {
                 $flagsDel = $flagsDel | $flag;
             }
             if (!$fieldOriginal->hasFlag($flag) && $field->hasFlag($flag)) {
                 $flagsAdd = $flagsAdd | $flag;
             }
         }
         if ($flagsAdd) {
             $data['flagsAdd'] = $flagsAdd;
         }
         if ($flagsDel) {
             $data['flagsDel'] = $flagsDel;
         }
     }
     // 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;
     }
     // remove runtime properties (those that start with underscore)
     foreach ($data as $key => $value) {
         if (strpos($key, '_') === 0) {
             unset($data[$key]);
         }
     }
     // 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;
 }