Ejemplo n.º 1
0
 /**
  * Static method to remove field values
  *
  * @param int    $modelId
  * @param string $dir
  * @return void
  */
 public static function remove($modelId, $dir = null)
 {
     $fields = \Phire\Table\FieldValues::findAll(null, array('model_id' => $modelId));
     if (null === $dir) {
         $dir = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . DIRECTORY_SEPARATOR . 'media';
     }
     if (isset($fields->rows[0])) {
         foreach ($fields->rows as $field) {
             // Get the field values with the field type to check for any files to delete
             if (isset($field->field_id)) {
                 $sql = \Phire\Table\FieldValues::getSql();
                 $sql->select(array(DB_PREFIX . 'field_values.field_id', DB_PREFIX . 'field_values.model_id', DB_PREFIX . 'field_values.value', DB_PREFIX . 'fields.type'))->join(DB_PREFIX . 'fields', array('field_id', 'id'), 'LEFT JOIN')->where()->equalTo('field_id', ':field_id')->equalTo('model_id', ':model_id');
                 $fld = \Phire\Table\FieldValues::execute($sql->render(true), array('field_id' => $field->field_id, 'model_id' => $modelId));
                 if (isset($fld->field_id)) {
                     // If field type is file, delete file(s)
                     if ($fld->type == 'file') {
                         $file = json_decode($fld->value, true);
                         if (is_array($file)) {
                             foreach ($file as $f) {
                                 if (file_exists($dir . '/' . $f)) {
                                     \Phire\Model\Media::remove($f, $dir);
                                 } else {
                                     $sites = Table\Sites::findAll();
                                     foreach ($sites->rows as $site) {
                                         if (file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/media/' . $f)) {
                                             \Phire\Model\Media::remove($f, $site->document_root . $site->base_path . CONTENT_PATH . '/media');
                                         }
                                     }
                                 }
                             }
                         } else {
                             if (file_exists($dir . '/' . $file)) {
                                 \Phire\Model\Media::remove($file, $dir);
                             } else {
                                 $sites = Table\Sites::findAll();
                                 foreach ($sites->rows as $site) {
                                     if (file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/media/' . $file)) {
                                         \Phire\Model\Media::remove($file, $site->document_root . $site->base_path . CONTENT_PATH . '/media');
                                     }
                                 }
                             }
                         }
                     }
                     $fld->delete();
                 }
             }
         }
     }
 }
 /**
  * Method to get model types
  *
  * @return void
  */
 public function json()
 {
     $body = '';
     if (null !== $this->request->getPath(1)) {
         // Get the selected field history value
         if ($this->request->getPath(1) == 'history' && null !== $this->request->getPath(2) && is_numeric($this->request->getPath(2)) && null !== $this->request->getPath(3) && is_numeric($this->request->getPath(3)) && null !== $this->request->getPath(4) && is_numeric($this->request->getPath(4))) {
             $modelId = $this->request->getPath(2);
             $fieldId = $this->request->getPath(3);
             $time = $this->request->getPath(4);
             $value = '';
             $encOptions = $this->project->module('Phire')->encryptionOptions->asArray();
             $fv = Table\FieldValues::findById(array($fieldId, $modelId));
             if (isset($fv->field_id) && null !== $fv->history) {
                 $history = json_decode($fv->history, true);
                 if (isset($history[$time])) {
                     $value = $history[$time];
                     $f = Table\Fields::findById($fieldId);
                     $value = Model\FieldValue::decrypt($value, $f->encryption, $encOptions);
                 }
             }
             $body = array('fieldId' => $fieldId, 'modelId' => $modelId, 'value' => html_entity_decode($value, ENT_QUOTES, 'UTF-8'));
             // Get the field history timestamps
         } else {
             if ($this->request->getPath(1) == 'history' && null !== $this->request->getPath(2) && is_numeric($this->request->getPath(2)) && null !== $this->request->getPath(3) && is_numeric($this->request->getPath(3))) {
                 $modelId = $this->request->getPath(2);
                 $fieldId = $this->request->getPath(3);
                 $fv = Table\FieldValues::findById(array($fieldId, $modelId));
                 if (isset($fv->field_id) && null !== $fv->history) {
                     $body = array_keys(json_decode($fv->history, true));
                     rsort($body);
                 }
                 // Get the model types
             } else {
                 $clsAry = $this->request->getPath();
                 unset($clsAry[0]);
                 $cls = implode('_', $clsAry);
                 $types = \Phire\Project::getModelTypes($cls);
                 $body = array('types' => $types);
             }
         }
         // Build the response and send it
         $response = new Response();
         $response->setHeader('Content-Type', 'application/json; charset=utf-8')->setBody(json_encode($body));
         $response->send();
     }
 }
Ejemplo n.º 3
0
 /**
  * Static method to get field definitions by model and
  * return them for consumption by a Pop\Form\Form object
  *
  * @param string $model
  * @param int    $tid
  * @param int    $mid
  * @return array
  */
 public static function getByModel($model, $tid = 0, $mid = 0)
 {
     $fieldsAry = array();
     $curFields = array();
     $groups = array();
     $dynamic = false;
     $hasFile = false;
     $i18n = Table\Config::getI18n();
     // Get fields
     $fields = array();
     $flds = Table\Fields::findAll('order ASC');
     foreach ($flds->rows as $f) {
         $models = null !== $f->models ? unserialize($f->models) : array();
         foreach ($models as $m) {
             if ($m['model'] == $model && ($m['type_id'] == $tid || $m['type_id'] == 0)) {
                 $fields[] = $f;
             }
         }
     }
     // If fields exist
     if (count($fields) > 0) {
         foreach ($fields as $field) {
             // Get field group, if applicable
             $groupAryResults = Table\Fields::getFieldGroup($field->id);
             $groupAry = $groupAryResults['fields'];
             $isDynamic = $groupAryResults['dynamic'];
             if ($isDynamic) {
                 $dynamic = true;
             }
             if (count($groupAry) > 0 && !in_array($groupAry, $groups)) {
                 $groups[$groupAryResults['group_id']] = $groupAry;
             }
             $rmFile = null;
             $fld = array('type' => strpos($field->type, '-') !== false ? substr($field->type, 0, strpos($field->type, '-')) : $field->type);
             // Get field label
             if ($field->label != '') {
                 if (isset($groupAry[0]) && $groupAry[0] == $field->id && $isDynamic) {
                     $fld['label'] = '<a href="#" onclick="phire.addFields([' . implode(', ', $groupAry) . ']); return false;">[+]</a> ' . $field->label;
                 } else {
                     $fld['label'] = $field->label;
                 }
             }
             $fld['name'] = $field->name;
             $fld['required'] = (bool) $field->required;
             // Get field values and default values
             if ($field->type == 'select' || $field->type == 'checkbox' || $field->type == 'radio') {
                 if ($field->values != '') {
                     // Get fields values of a multiple value field
                     if (strpos($field->values, '[') !== false && strpos($field->values, ']') !== false) {
                         $valsAry = explode('|', $field->values);
                         $fieldValues = [];
                         foreach ($valsAry as $vals) {
                             $key = substr($vals, 0, strpos($vals, '['));
                             $vls = substr($vals, strpos($vals, '[') + 1);
                             $vls = substr($vls, 0, strpos($vls, ']'));
                             $vls = str_replace('::', '|', $vls);
                             $vlsAry = explode('|', $vls);
                             $vs = [];
                             foreach ($vlsAry as $va) {
                                 // If the values are a name/value pair
                                 if (strpos($va, ',') !== false) {
                                     $vAry = explode(',', $va);
                                     if (count($vAry) >= 2) {
                                         // If the values are to be pulled from a database table
                                         if (strpos($vAry[0], 'Table') !== false) {
                                             $class = $vAry[0];
                                             $order = $vAry[1] . (isset($vAry[2]) ? ', ' . $vAry[2] : null);
                                             $order .= ' ' . (isset($vAry[3]) ? $vAry[3] : 'ASC');
                                             $id = $vAry[1];
                                             $name = isset($vAry[2]) ? $vAry[2] : $vAry[1];
                                             $valRows = $class::findAll($order);
                                             if (isset($valRows->rows[0])) {
                                                 foreach ($valRows->rows as $vRow) {
                                                     $vs[$vRow->{$id}] = $vRow->{$name};
                                                 }
                                             }
                                             // Else, if the value is a simple name/value pair
                                         } else {
                                             $vs[$vAry[0]] = $vAry[1];
                                         }
                                     }
                                 } else {
                                     $vs[$va] = $va;
                                 }
                             }
                             $fieldValues[$key] = $vs;
                         }
                         $fld['value'] = $fieldValues;
                     } else {
                         if (strpos($field->values, '|') !== false) {
                             $vals = explode('|', $field->values);
                             $valAry = array();
                             foreach ($vals as $v) {
                                 // If the values are a name/value pair
                                 if (strpos($v, ',') !== false) {
                                     $vAry = explode(',', $v);
                                     if (count($vAry) >= 2) {
                                         // If the values are to be pulled from a database table
                                         if (strpos($vAry[0], 'Table') !== false) {
                                             $class = $vAry[0];
                                             $order = $vAry[1] . (isset($vAry[2]) ? ', ' . $vAry[2] : null);
                                             $order .= ' ' . (isset($vAry[3]) ? $vAry[3] : 'ASC');
                                             $id = $vAry[1];
                                             $name = isset($vAry[2]) ? $vAry[2] : $vAry[1];
                                             $valRows = $class::findAll($order);
                                             if (isset($valRows->rows[0])) {
                                                 foreach ($valRows->rows as $vRow) {
                                                     $valAry[$vRow->{$id}] = $vRow->{$name};
                                                 }
                                             }
                                             // Else, if the value is a simple name/value pair
                                         } else {
                                             $valAry[$vAry[0]] = $vAry[1];
                                         }
                                     }
                                 } else {
                                     $valAry[$v] = $v;
                                 }
                             }
                             $fld['value'] = $valAry;
                             // If the values are to be pulled from a database table
                         } else {
                             if (strpos($field->values, 'Table') !== false) {
                                 $valAry = array();
                                 if (strpos($field->values, ',') !== false) {
                                     $vAry = explode(',', $field->values);
                                     $class = $vAry[0];
                                     $order = $vAry[1] . (isset($vAry[2]) ? ', ' . $vAry[2] : null);
                                     $order .= ' ' . (isset($vAry[3]) ? $vAry[3] : 'ASC');
                                     $id = $vAry[1];
                                     $name = isset($vAry[2]) ? $vAry[2] : $vAry[1];
                                 } else {
                                     $class = $field->values;
                                     $order = null;
                                     $id = 'id';
                                     $name = 'id';
                                 }
                                 $valRows = $class::findAll($order);
                                 if (isset($valRows->rows[0])) {
                                     foreach ($valRows->rows as $vRow) {
                                         $valAry[$vRow->{$id}] = $vRow->{$name};
                                     }
                                 }
                                 $fld['value'] = $valAry;
                                 // Else, if the value is Select constant
                             } else {
                                 if (strpos($field->values, 'Select::') !== false) {
                                     $fld['value'] = str_replace('Select::', '', $field->values);
                                     // Else, the value is a simple value
                                 } else {
                                     $aryValues = array();
                                     if (strpos($field->values, ',') !== false) {
                                         $vls = explode(',', $field->values);
                                         $aryValues[$vls[0]] = $vls[1];
                                     } else {
                                         $aryValues[$field->values] = $field->values;
                                     }
                                     $fld['value'] = $aryValues;
                                 }
                             }
                         }
                     }
                 }
                 // Set default values
                 if ($field->default_values != '') {
                     $fld['marked'] = strpos($field->default_values, '|') !== false ? explode('|', $field->default_values) : $field->default_values;
                 }
                 // If field is a file field
             } else {
                 if ($field->type == 'file' && count($groupAry) == 0) {
                     $dynamic = true;
                     $hasFile = true;
                     if ($mid != 0) {
                         $fileValue = Table\FieldValues::findById(array($field->id, $mid));
                         if (isset($fileValue->field_id)) {
                             $fileName = json_decode($fileValue->value, true);
                             $fileInfo = \Phire\Model\Media::getFileIcon($fileName);
                             $fld['label'] .= '<br /><a href="' . BASE_PATH . CONTENT_PATH . '/media/' . $fileName . '" target="_blank"><img style="padding-top: 3px;" src="' . BASE_PATH . CONTENT_PATH . $fileInfo['fileIcon'] . '" width="50" /></a><br /><a href="' . BASE_PATH . CONTENT_PATH . '/media/' . $fileName . '" target="_blank">' . $fileName . '</a><br /><span style="font-size: 0.9em;">(' . $fileInfo['fileSize'] . ')</span><br /><em style="font-size: 0.9em; font-weight:normal;">' . $i18n->__('Replace?') . '</em>';
                             $fld['required'] = false;
                             $rmFile = array('rm_file_' . $field->id => array('type' => 'checkbox', 'value' => array($fileName => $i18n->__('Remove') . '?')));
                         }
                     }
                     // Else, if the field is a normal field
                 } else {
                     if ($field->default_values != '') {
                         $fld['value'] = $field->default_values;
                     }
                 }
             }
             // Get field attributes
             if ($field->attributes != '') {
                 $attAry = array();
                 $attributes = explode('" ', $field->attributes);
                 foreach ($attributes as $attrib) {
                     $att = explode('=', $attrib);
                     $attAry[$att[0]] = str_replace('"', '', $att[1]);
                 }
                 $fld['attributes'] = $attAry;
             }
             // Get field validators
             if ($field->validators != '') {
                 $valAry = array();
                 $validators = unserialize($field->validators);
                 foreach ($validators as $key => $value) {
                     $valClass = '\\Pop\\Validator\\' . $key;
                     if ($value['value'] != '') {
                         $v = new $valClass($value['value']);
                     } else {
                         $v = new $valClass();
                     }
                     if ($value['message'] != '') {
                         $v->setMessage($value['message']);
                     }
                     $valAry[] = $v;
                 }
                 $fld['validators'] = $valAry;
             }
             // Detect any dynamic field group values
             $values = Table\FieldValues::findAll(null, array('field_id' => $field->id));
             if (isset($values->rows[0])) {
                 foreach ($values->rows as $value) {
                     $val = json_decode($value->value);
                     if (count($groupAry) > 0 && $value->model_id == $mid) {
                         if (is_array($val)) {
                             foreach ($val as $k => $v) {
                                 $curFld = $fld;
                                 if ($field->type == 'select' || $field->type == 'checkbox' || $field->type == 'radio') {
                                     $curFld['marked'] = $v;
                                 } else {
                                     $curFld['value'] = $v;
                                 }
                                 if (isset($curFld['label']) && $dynamic) {
                                     $curFld['label'] = '&nbsp;';
                                 }
                                 if (!isset($curFields[$field->id])) {
                                     $curFields[$field->id] = array('field_' . $field->id . '_cur_' . ($k + 1) => $curFld);
                                 } else {
                                     $curFields[$field->id]['field_' . $field->id . '_cur_' . ($k + 1)] = $curFld;
                                 }
                             }
                         } else {
                             $curFld = $fld;
                             if ($field->type == 'select' || $field->type == 'checkbox' || $field->type == 'radio') {
                                 $curFld['marked'] = $val;
                             } else {
                                 $curFld['value'] = $val;
                             }
                             if (isset($curFld['label']) && $dynamic) {
                                 $curFld['label'] = '&nbsp;';
                             }
                             if (!isset($curFields[$field->id])) {
                                 $curFields[$field->id] = array('field_' . $field->id => $curFld);
                             } else {
                                 $curFields[$field->id]['field_' . $field->id] = $curFld;
                             }
                         }
                     }
                 }
             }
             // If field is assigned to a dynamic field group, set field name accordingly
             if (count($groupAry) > 0 && $isDynamic) {
                 $fieldName = 'field_' . $field->id . '_new_1';
             } else {
                 $fieldName = 'field_' . $field->id;
             }
             // Add field to the field array
             $fieldsAry[$fieldName] = $fld;
             // If in the system back end, and the field is a textarea, add history select field
             if ($mid != 0 && strpos($field->type, '-history') !== false && count($groupAry) == 0 && strpos($_SERVER['REQUEST_URI'], APP_URI) !== false) {
                 $fv = Table\FieldValues::findById(array($field->id, $mid));
                 if (isset($fv->field_id) && null !== $fv->history) {
                     $history = array(0 => '(' . $i18n->__('Current') . ')');
                     $historyAry = json_decode($fv->history, true);
                     krsort($historyAry);
                     foreach ($historyAry as $time => $fieldValue) {
                         $history[$time] = date('M j, Y H:i:s', $time);
                     }
                     $fieldsAry['history_' . $mid . '_' . $field->id] = array('type' => 'select', 'label' => $i18n->__('Select Revision'), 'value' => $history, 'marked' => 0, 'attributes' => array('onchange' => "phire.changeHistory(this, '" . BASE_PATH . APP_URI . "');", 'style' => 'width: 160px;'));
                 }
             }
             if (strpos($field->type, 'textarea') !== false) {
                 if (null !== $field->editor && $field->editor != 'source') {
                     $fieldsAry[$fieldName]['label'] .= ' <span style="float: right; margin-right: 4%; font-weight: normal;">[ <a href="#" class="editor-link" id="editor-' . $field->id . '" data-editor="' . $field->editor . '" data-editor-status="on" onclick="phire.changeEditor(this); return false;">' . $i18n->__('Source') . '</a> ]</span>';
                 }
             }
             // Add a remove field
             if (null !== $rmFile) {
                 foreach ($rmFile as $rmKey => $rmValue) {
                     $fieldsAry[$rmKey] = $rmValue;
                 }
             }
             if (isset($group) && count($group) > 0) {
                 if (isset($group[count($group) - 1]) && $field->id == $group[count($group) - 1]) {
                     $fieldsAry[implode('_', $group)] = null;
                     $group = array();
                 }
             }
         }
     }
     // Add fields from dynamic field group in the correct order
     $realCurFields = array();
     $groupRmAry = array();
     if (count($curFields) > 0) {
         $fieldCount = count($curFields);
         $keys = array_keys($curFields);
         $valueCounts = array();
         foreach ($groups as $key => $value) {
             foreach ($curFields as $k => $v) {
                 if (in_array($k, $value)) {
                     $valueCounts[$key] = count($v);
                 }
             }
         }
         foreach ($valueCounts as $gKey => $valueCount) {
             for ($i = 0; $i < $valueCount; $i++) {
                 $fileName = null;
                 $gDynamic = false;
                 for ($j = 0; $j < $fieldCount; $j++) {
                     if (in_array($keys[$j], $groups[$gKey])) {
                         if (isset($curFields[$keys[$j]]['field_' . $keys[$j] . '_cur_' . ($i + 1)])) {
                             $gDynamic = true;
                             $f = $curFields[$keys[$j]]['field_' . $keys[$j] . '_cur_' . ($i + 1)];
                             if ($f['type'] == 'file') {
                                 $hasFile = true;
                                 $dynamic = true;
                                 $fileName = $f['value'];
                                 // Calculate file icon, set label
                                 if (!empty($fileName)) {
                                     $fileInfo = \Phire\Model\Media::getFileIcon($fileName);
                                     $f['label'] = '<br /><a href="' . BASE_PATH . CONTENT_PATH . '/media/' . $fileName . '" target="_blank"><img style="padding-top: 3px;" src="' . BASE_PATH . CONTENT_PATH . $fileInfo['fileIcon'] . '" width="50" /></a><br /><a href="' . BASE_PATH . CONTENT_PATH . '/media/' . $fileName . '" target="_blank">' . $fileName . '</a><br /><span style="font-size: 0.9em;">(' . $fileInfo['fileSize'] . ')</span><br /><em style="font-size: 0.9em; font-weight:normal;">' . $i18n->__('Replace?') . '</em>';
                                 } else {
                                     $f['label'] = $i18n->__('Replace?');
                                 }
                                 $fld['required'] = false;
                             }
                             $realCurFields['field_' . $keys[$j] . '_cur_' . ($i + 1)] = $f;
                         } else {
                             if (isset($curFields[$keys[$j]]['field_' . $keys[$j]])) {
                                 $gDynamic = false;
                                 $f = $curFields[$keys[$j]]['field_' . $keys[$j]];
                                 if ($f['type'] == 'file') {
                                     $hasFile = true;
                                     $dynamic = true;
                                     $fileName = $f['value'];
                                     // Calculate file icon, set label
                                     if (!empty($fileName)) {
                                         $fileInfo = \Phire\Model\Media::getFileIcon($fileName);
                                         $f['label'] = '<br /><a href="' . BASE_PATH . CONTENT_PATH . '/media/' . $fileName . '" target="_blank"><img style="padding-top: 3px;" src="' . BASE_PATH . CONTENT_PATH . $fileInfo['fileIcon'] . '" width="50" /></a><br /><a href="' . BASE_PATH . CONTENT_PATH . '/media/' . $fileName . '" target="_blank">' . $fileName . '</a><br /><span style="font-size: 0.9em;">(' . $fileInfo['fileSize'] . ')</span><br /><em style="font-size: 0.9em; font-weight:normal;">' . $i18n->__('Replace?') . '</em>';
                                     } else {
                                         $f['label'] = $i18n->__('Replace?');
                                     }
                                     $fld['required'] = false;
                                 }
                                 $fieldsAry['field_' . $keys[$j]] = $f;
                             }
                         }
                     }
                 }
                 // Add a remove field
                 if ($gDynamic) {
                     $fieldId = implode('_', $groups[$gKey]) . '_' . ($i + 1);
                     $realCurFields['rm_fields_' . $fieldId] = array('type' => 'checkbox', 'value' => array($fieldId => $i18n->__('Remove') . '?'));
                 } else {
                     $fieldId = implode('_', $groups[$gKey]);
                     $groupRmAry[$key] = array('type' => 'checkbox', 'value' => array($fieldId => $i18n->__('Remove') . '?'));
                 }
             }
         }
     }
     // Merge new fields and current fields together in the right order.
     $realFieldsAry = array('dynamic' => $dynamic, 'hasFile' => $hasFile, '0' => array());
     if (count($groups) > 0) {
         foreach ($groups as $id => $fields) {
             $realFieldsAry[$id] = array();
         }
     }
     $cnt = 0;
     foreach ($fieldsAry as $key => $value) {
         $id = substr($key, strpos($key, '_') + 1);
         if (strpos($id, '_') !== false) {
             $id = substr($id, 0, strpos($id, '_'));
         }
         $curGroupId = 0;
         foreach ($groups as $gId => $gFields) {
             if (in_array($id, $gFields)) {
                 $curGroupId = $gId;
             }
         }
         if (strpos($key, 'new_') !== false) {
             $cnt = 0;
             $curGroup = null;
             foreach ($groups as $group) {
                 if (in_array($id, $group)) {
                     $curGroup = $group;
                 }
             }
             $realFieldsAry[$curGroupId][$key] = $value;
             if (null !== $curGroup && $id == $curGroup[count($curGroup) - 1]) {
                 foreach ($realCurFields as $k => $v) {
                     if (strpos($k, 'rm_field') === false) {
                         $i = substr($k, strpos($k, '_') + 1);
                         $i = substr($i, 0, strpos($i, '_'));
                         if (in_array($i, $curGroup)) {
                             $realFieldsAry[$curGroupId][$k] = $v;
                         }
                     } else {
                         $i = substr($k, strpos($k, 'rm_fields_') + 10);
                         $i = substr($i, 0, strrpos($i, '_'));
                         $grp = explode('_', $i);
                         if ($grp == $curGroup) {
                             $realFieldsAry[$curGroupId][$k] = $v;
                         }
                     }
                 }
             }
         } else {
             $cnt++;
             $realFieldsAry[$curGroupId][$key] = $value;
             if (isset($groupRmAry[$curGroupId]) && $cnt == count($groups[$curGroupId])) {
                 $realFieldsAry[$curGroupId]['rm_fields_' . implode('_', $groups[$curGroupId])] = $groupRmAry[$curGroupId];
             }
         }
     }
     return $realFieldsAry;
 }