/** * Static method to update field values * * @param array $fields * @param int $modelId * @param string $method * @param string $dir * @return void */ public static function update(array $fields, $modelId, $method = 'POST', $dir = null) { $config = static::factory()->config(); $docRoot = $_SERVER['DOCUMENT_ROOT']; $basePath = BASE_PATH; if (isset($fields['site_id'])) { $site = Table\Sites::findById((int) $fields['site_id']); if (isset($site->id)) { $docRoot = $site->document_root; $basePath = $site->base_path; } } if (null === $dir) { $dir = $docRoot . $basePath . CONTENT_PATH . DIRECTORY_SEPARATOR . 'media'; } $valueAry = array(); $postKeys = $_POST ? self::getPostKeys() : array(); $fileKeys = $_FILES ? self::getFileKeys() : array(); $keys = array_merge($postKeys, $fileKeys); sort($keys); $groups = Table\FieldValues::getGroups($keys); // Get history count, if applicable $historyCount = null; // Check for an overriding config setting if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/config/phire.php')) { $fldCfg = (include $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/modules/config/phire.php'); $encOptions = $fldCfg['Phire']->encryptionOptions->asArray(); $historyCount = isset($fldCfg['Phire']->history) ? (int) $fldCfg['Phire']->history : null; } else { $fldCfg = (include __DIR__ . '/../../../config/module.php'); $encOptions = $fldCfg['Phire']->encryptionOptions->asArray(); $historyCount = isset($fldCfg['Phire']->history) ? (int) $fldCfg['Phire']->history : null; } // Save new dynamic fields (not files) foreach ($fields as $key => $value) { if (strpos($key, 'field_') !== false && !isset($_FILES[$key])) { $id = self::getFieldId($key); if ($method == 'POST' && $_POST || $method == 'GET') { // If it's a dynamic field value, store in array if (strpos($key, 'field_' . $id . '_cur_') !== false) { if (!isset($valueAry[$id])) { $valueAry[$id] = array($value); } else { $valueAry[$id][] = $value; } // Else, save the non-dynamic field value } else { if (strpos($key, 'new_') === false) { $f = Table\Fields::findById($id); $field = Table\FieldValues::findById(array($id, $modelId)); $realValue = isset($_POST['field_' . $id]) ? $value : null; // If existing field, update if (isset($field->field_id)) { if (!empty($realValue) && $realValue != '[Encrypted]') { $realValue = self::encrypt($realValue, $f->encryption, $encOptions); } // If history tracking is available for this field, update history if (null !== $historyCount && $historyCount > 0) { $f = Table\Fields::findById($field->field_id); if (isset($f->id) && strpos($f->type, '-history') !== false) { $oldValue = json_decode($field->value, true); // If value is different that the last value if ($realValue != $oldValue) { $ts = null !== $field->timestamp ? $field->timestamp : time() - 300; if (null !== $field->history) { $history = json_decode($field->history, true); $history[$ts] = $oldValue; if (count($history) > $historyCount) { $history = array_slice($history, 1, $historyCount, true); } $field->history = json_encode($history); } else { $field->history = json_encode(array($ts => $oldValue)); } } } } $field->value = json_encode($realValue); $field->timestamp = time(); $field->update(); // Else, save new field } else { $realValue = self::encrypt($realValue, $f->encryption, $encOptions); $f = new Table\FieldValues(array('field_id' => $id, 'model_id' => $modelId, 'value' => json_encode($realValue), 'timestamp' => time())); $f->save(); } } } } } } // Check for current files if ($_FILES) { foreach ($_FILES as $key => $value) { if (strpos($key, 'field_') !== false) { $id = self::getFieldId($key); // If it's a dynamic field value, store in array if (strpos($key, 'cur_') !== false) { $fileName = ''; if ($value['tmp_name'] != '') { $num = substr($key, strrpos($key, '_') + 1) - 1; $fv = Table\FieldValues::findById(array($id, $modelId)); // If file exists and is being replaced, remove it if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (isset($fValue[$num])) { if ($_FILES) { \Phire\Model\Media::remove($fValue[$num], $docRoot . $basePath); } } } $fileName = File::checkDupe($value['name'], $dir); File::upload($value['tmp_name'], $dir . DIRECTORY_SEPARATOR . $fileName, $config->media_max_filesize, $config->media_allowed_types); chmod($dir . DIRECTORY_SEPARATOR . $fileName, 0777); if ($_FILES && preg_match(\Phire\Model\Media::getImageRegex(), $fileName)) { \Phire\Model\Media::process($fileName, $config, $dir); } } else { $num = substr($key, strrpos($key, '_') + 1) - 1; $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (isset($fValue[$num])) { $fileName = $fValue[$num]; } } } if (!isset($valueAry[$id])) { $valueAry[$id] = array($fileName); } else { $valueAry[$id][] = $fileName; } // Else, save the non-dynamic field value } else { if (strpos($key, 'new_') === false) { $fileName = ''; if ($value['tmp_name'] != '') { $fileName = File::checkDupe($value['name'], $dir); File::upload($value['tmp_name'], $dir . DIRECTORY_SEPARATOR . $fileName, $config->media_max_filesize, $config->media_allowed_types); chmod($dir . DIRECTORY_SEPARATOR . $fileName, 0777); if ($_FILES && preg_match(\Phire\Model\Media::getImageRegex(), $fileName)) { \Phire\Model\Media::process($fileName, $config, $dir); } } if ($fileName != '') { $f = Table\Fields::findById($id); $fileName = self::encrypt($fileName, $f->encryption, $encOptions); $field = Table\FieldValues::findById(array($id, $modelId)); // If file field value exists, update if (isset($field->field_id)) { if ($_FILES) { \Phire\Model\Media::remove(json_decode($field->value, true), $docRoot . $basePath); } $field->value = json_encode($fileName); $field->timestamp = time(); $field->update(); // Else, save new } else { $f = new Table\FieldValues(array('field_id' => $id, 'model_id' => $modelId, 'value' => json_encode($fileName), 'timestamp' => time())); $f->save(); } } } } } } } // Remove any fields that have been marked for deletion foreach ($_POST as $key => $value) { if (strpos($key, 'rm_fields_') !== false) { $ids = substr($key, 10); $num = substr($ids, strrpos($ids, '_') + 1) - 1; $ids = substr($ids, 0, strrpos($ids, '_')); $ids = explode('_', $ids); foreach ($ids as $id) { $f = Table\Fields::findById($id); // If it's a file, remove the file too if (isset($f->id) && $f->type == 'file') { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (is_array($fValue) && isset($fValue[$num])) { if ($_FILES) { \Phire\Model\Media::remove($fValue[$num], $docRoot . $basePath); } } else { if ($_FILES) { \Phire\Model\Media::remove($fValue, $docRoot . $basePath); } } } } if (null !== $num && isset($valueAry[$id]) && isset($valueAry[$id][$num])) { unset($valueAry[$id][$num]); } else { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fv->delete(); } } } } // Remove any non-dynamic single files if (strpos($key, 'rm_file_') !== false) { $id = substr($key, strrpos($key, '_') + 1); if (isset($value[0])) { if ($_FILES) { \Phire\Model\Media::remove($value[0], $docRoot . $basePath); } } $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fv->delete(); } } } // Removal clean-up foreach ($_POST as $key => $value) { if (strpos($key, 'rm_fields_') !== false) { $ids = substr($key, 10); $ids = substr($ids, 0, strrpos($ids, '_')); $ids = explode('_', $ids); foreach ($ids as $id) { if (isset($valueAry[$id])) { $valueAry[$id] = array_values($valueAry[$id]); } } } } // Check for new dynamic field values (not files) if ($_POST) { foreach ($_POST as $k => $v) { if (strpos($k, 'field_') !== false && !isset($_FILES[$k])) { $id = self::getFieldId($k); // If it's a dynamic field value, store in array if (strpos($k, 'field_' . $id . '_new_') !== false) { if (!isset($valueAry[$id])) { $valueAry[$id] = array($v); } else { $valueAry[$id][] = $v; } } } } } // Get new files if ($_FILES) { foreach ($_FILES as $key => $value) { if (strpos($key, 'field_') !== false) { $id = self::getFieldId($key); // If it's a dynamic field value, store in array if (strpos($key, 'new_') !== false) { $fileName = ''; if ($value['tmp_name'] != '') { $fileName = File::checkDupe($value['name'], $dir); File::upload($value['tmp_name'], $dir . DIRECTORY_SEPARATOR . $fileName, $config->media_max_filesize, $config->media_allowed_types); chmod($dir . DIRECTORY_SEPARATOR . $fileName, 0777); if ($_FILES && preg_match(\Phire\Model\Media::getImageRegex(), $fileName)) { \Phire\Model\Media::process($fileName, $config, $dir); } } if (!isset($valueAry[$id])) { $valueAry[$id] = array($fileName); } else { $valueAry[$id][] = $fileName; } } } } } // Save the new dynamic field values if (count($valueAry) > 0) { // Clean up, check for empties foreach ($groups as $group) { if (isset($group['fields']) && isset($group['fields'][0]) && isset($valueAry[$group['fields'][0]])) { $keys = array_keys($valueAry[$group['fields'][0]]); foreach ($keys as $key) { $i = 0; $removal = array(); foreach ($group['fields'] as $id) { if (isset($valueAry[$id][$key])) { if ($valueAry[$id][$key] == '----' || empty($valueAry[$id][$key]) || is_array($valueAry[$id][$key]) && count($valueAry[$id][$key]) == 1 && empty($valueAry[$id][$key][0])) { $f = Table\Fields::findById($id); if (isset($f->id) && $f->type == 'file') { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fValue = json_decode($fv->value, true); if (isset($fValue[$key])) { $valueAry[$id][$key] = $fValue[$key]; } } } $i++; } } else { if (isset($valueAry[$id])) { if (is_array($valueAry[$id]) && count($valueAry[$id]) == 0) { $i++; } else { if (is_array($valueAry[$id]) && count($valueAry[$id]) == 1 && (empty($valueAry[$id][0]) || $valueAry[$id][0] == '----')) { $i++; } } } } } if ($i == count($group['fields'])) { foreach ($valueAry as $k => $v) { if (in_array($k, $group['fields'])) { unset($valueAry[$k][$key]); if (isset($valueAry[$k]) && count($valueAry[$k]) == 0) { $removal[] = $k; unset($valueAry[$k]); } } } } sort($removal); // Final clean up of empties if (count($removal) > 0) { foreach ($groups as $grp) { if (in_array($removal, $grp)) { foreach ($removal as $id) { $fv = Table\FieldValues::findById(array($id, $modelId)); if (isset($fv->field_id)) { $fv->delete(); } } } } } } } } foreach ($valueAry as $key => $value) { $valueAry[$key] = array_values($value); } // Either update existing field values or save new ones foreach ($valueAry as $id => $value) { $f = Table\Fields::findById($id); $field = Table\FieldValues::findById(array($id, $modelId)); if (isset($field->field_id)) { if (!empty($value) && $value != '[Encrypted]') { $value = self::encrypt($value, $f->encryption, $encOptions); } else { $value = json_decode($field->value, true); } $field->value = json_encode($value); $field->timestamp = time(); $field->update(); } else { $value = self::encrypt($value, $f->encryption, $encOptions); $f = new Table\FieldValues(array('field_id' => $id, 'model_id' => $modelId, 'value' => json_encode($value), 'timestamp' => time())); $f->save(); } } } }
/** * 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(); } }
/** * 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'] = ' '; } 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'] = ' '; } 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; }