/**
  * Instantiate the model object.
  *
  * @param  array $data
  * @return self
  */
 public function __construct(array $data = null)
 {
     if (null !== $data) {
         $this->data = $data;
     }
     $this->config = \Phire\Table\Config::getSystemConfig();
     if (!headers_sent()) {
         $sess = \Pop\Web\Session::getInstance();
     }
     $this->i18n = Table\Config::getI18n();
     if (isset($sess->user)) {
         $this->data['user'] = $sess->user;
         $this->data['role'] = \Phire\Table\UserRoles::getRole($sess->user->role_id);
         $this->data['globalAccess'] = $sess->user->global_access;
     }
 }
Example #2
0
 /**
  * Send install notification email to user
  *
  * @param  \Phire\Form\User $form
  * @return void
  */
 public static function send(\Phire\Form\User $form)
 {
     $i18n = Table\Config::getI18n();
     // Get the domain
     $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']);
     // Set the recipient
     $rcpt = array('name' => $form->username, 'email' => $form->email1, 'url' => 'http://' . $_SERVER['HTTP_HOST'] . BASE_PATH, 'login' => 'http://' . $_SERVER['HTTP_HOST'] . BASE_PATH . APP_URI, 'domain' => $domain);
     $config = \Phire\Table\Config::findById('system_email');
     $config->value = $form->email1;
     $config->update();
     $config = \Phire\Table\Config::findById('reply_email');
     $config->value = 'noreply@' . $domain;
     $config->update();
     if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/themes/phire/mail')) {
         $mailTmpl = file_get_contents($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/extensions/themes/phire/mail/install.txt');
     } else {
         $mailTmpl = file_get_contents(__DIR__ . '/../../../view/phire/mail/install.txt');
     }
     $mailTmpl = str_replace(array('Dear', 'Thank you for installing Phire CMS for', 'The website will be viewable here:', 'To manage the website, you can login to Phire here:', 'Thank You'), array($i18n->__('Dear'), $i18n->__('Thank you for installing Phire CMS for'), $i18n->__('The website will be viewable here:'), $i18n->__('To manage the website, you can login to Phire here:'), $i18n->__('Thank You')), $mailTmpl);
     // Send email verification
     $mail = new Mail($domain . ' - ' . $i18n->__('Phire CMS Installation'), $rcpt);
     $mail->from('noreply@' . $domain);
     $mail->setText($mailTmpl);
     $mail->send();
 }
 /**
  * Prepare view method
  *
  * @param  string $template
  * @param  array  $data
  * @return void
  */
 public function prepareView($template = null, array $data = array())
 {
     $site = \Phire\Table\Sites::getSite();
     if (null !== $template) {
         $template = $this->getCustomView($template, $site);
     }
     $sess = \Pop\Web\Session::getInstance();
     $config = \Phire\Table\Config::getSystemConfig();
     $i18n = \Phire\Table\Config::getI18n();
     $this->live = (bool) $config->live;
     $jsVars = null;
     $this->view = View::factory($template, $data);
     $this->view->set('base_path', $site->base_path)->set('content_path', CONTENT_PATH);
     // Check for an override Phire theme for the header/footer
     if (file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/header.phtml') && file_exists($site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/footer.phtml')) {
         $this->view->set('phireHeader', $site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/header.phtml')->set('phireFooter', $site->document_root . $site->base_path . CONTENT_PATH . '/extensions/themes/phire/footer.phtml');
         // Else, just use the default header/footer
     } else {
         $this->view->set('phireHeader', $site->document_root . $site->base_path . APP_PATH . '/vendor/Phire/view/phire/header.phtml')->set('phireFooter', $site->document_root . $site->base_path . APP_PATH . '/vendor/Phire/view/phire/footer.phtml');
     }
     if (isset($this->view->assets)) {
         $jsVars = '?lang=' . $config->default_language;
     }
     if (isset($sess->user)) {
         if (isset($this->sess->user->last_action)) {
             $this->sess->user->last_action = date('Y-m-d H:i:s');
         }
         // Set the timeout warning, giving a 30 second buffer to act
         if (isset($this->view->assets)) {
             if (isset($this->view->acl) && $this->view->acl->getType()->session_expiration > 0 && $this->view->acl->getType()->timeout_warning) {
                 $exp = $this->view->acl->getType()->session_expiration * 60 - 30;
                 $uri = $site->base_path . (strtolower($this->view->acl->getType()->type) != 'user' ? '/' . strtolower($this->view->acl->getType()->type) : APP_URI);
                 $jsVars .= '&_exp=' . $exp . '&_base=' . urlencode($uri);
             }
         }
         $this->view->set('user', $sess->user)->set('role', \Phire\Table\UserRoles::getRole($sess->user->role_id))->set('globalAccess', $sess->user->global_access);
         if (isset($this->view->phireNav) && isset($this->view->acl) && $this->view->acl->hasRole($this->view->role->getName())) {
             $this->view->phireNav->setConfig(array('top' => array('node' => 'ul', 'id' => 'phire-nav')));
             $this->view->phireNav->setAcl($this->view->acl);
             $this->view->phireNav->setRole($this->view->role);
             $tree = $this->view->phireNav->getTree();
             // If the sub-children haven't been added yet
             if (isset($tree[0])) {
                 // And any user types to the main phire nav
                 $userTypes = \Phire\Table\UserTypes::findAll('id ASC');
                 if (isset($userTypes->rows)) {
                     foreach ($userTypes->rows as $type) {
                         $perm = 'index_' . $type->id;
                         if ($this->view->acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'index') && $this->view->acl->isAuth('Phire\\Controller\\Phire\\User\\IndexController', 'index_' . $type->id)) {
                             $perm = 'index';
                         }
                         $this->view->phireNav->addLeaf('Users', array('name' => ucwords(str_replace('-', ' ', $type->type)), 'href' => 'index/' . $type->id, 'acl' => array('resource' => 'Phire\\Controller\\Phire\\User\\IndexController', 'permission' => $perm)), 1);
                     }
                 }
                 // Set the language
                 $tree = $this->view->phireNav->getTree();
                 foreach ($tree as $key => $value) {
                     if (isset($value['name'])) {
                         $tree[$key]['name'] = $i18n->__($value['name']);
                         if (isset($value['children']) && count($value['children']) > 0) {
                             foreach ($value['children'] as $k => $v) {
                                 if ($v['name'] == 'Fields' && isset($tree[$key]['children'][$k]['children'][0]['name'])) {
                                     $tree[$key]['children'][$k]['children'][0]['name'] = $i18n->__($tree[$key]['children'][$k]['children'][0]['name']);
                                 }
                                 $tree[$key]['children'][$k]['name'] = $i18n->__($v['name']);
                             }
                         }
                     }
                 }
                 $this->view->phireNav->setTree($tree);
             }
             $this->view->phireNav->rebuild();
             $this->view->phireNav->nav()->setIndent('    ');
         }
     }
     if (isset($this->view->assets)) {
         $this->view->assets = str_replace('jax.3.2.0.min.js', 'jax.3.2.0.min.js' . $jsVars, $this->view->assets);
     }
     if (isset($sess->errors)) {
         $this->view->set('errors', $sess->errors);
     }
     // Set config object and system/site default data
     $this->view->set('i18n', $i18n)->set('system_title', $config->system_title)->set('system_email', $config->system_email)->set('site_title', $config->site_title)->set('base_path', $config->base_path)->set('separator', $config->separator)->set('default_language', $config->default_language)->set('datetime_format', $config->datetime_format);
 }
Example #4
0
 /**
  * Event-based route error check
  *
  * @param  \Pop\Mvc\Router $router
  * @return void
  */
 public static function error($router)
 {
     $view = \Pop\Mvc\View::factory(__DIR__ . '/../../view/route.phtml', array('i18n' => \Phire\Table\Config::getI18n(), 'title' => 'Routing Error', 'msg' => '    <p>There was no controller assigned for this route.</p>'));
     $response = new \Pop\Http\Response(404);
     $response->setBody($view->render(true));
     $response->send();
 }
Example #5
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;
 }