Пример #1
0
 protected function getFieldValues(&$field_list, $accessTable = false)
 {
     $output = array();
     $dependenciesMet = true;
     foreach ($field_list as $f => $field) {
         // check for settings that override user input
         if ($this->action == "insert" && !$this->get_value_on_new($field)) {
             continue;
         } elseif ($this->action == "update" && !$this->get_value_on_update($field)) {
             continue;
         }
         if ($field['type'] == 'note') {
             continue;
         }
         if (!empty($field['nocolumn'])) {
             continue;
         }
         if (!empty($field['table']) && $field['table'] == "access" && !$accessTable) {
             continue;
         } elseif (!isset($field['table']) && $accessTable) {
             continue;
         }
         unset($val);
         $sanitize = false;
         $html = false;
         $ignore = false;
         if (!isset($field['form_field'])) {
             $field['form_field'] = $field['field'];
         }
         // GET THE FIELD VALUE
         // OVERRIDES
         if (!empty($field['force_default_new']) && $this->action == "insert") {
             $val = $field['default'];
             // developer entered, could need sanitization
             $sanitize = true;
         } elseif ($this->parentLink == $field['field']) {
             // parent link
             $val = $this->parentId;
             // already sanitized, not needed
             // FUNCTIONS
         } elseif ($this->action == 'insert' && isset($field['insert_function'])) {
             // function when modified
             $this->preset[$field['field']]['insert_function']($output);
             continue;
         } elseif ($this->action == 'update' && isset($field['modified_function'])) {
             $this->preset[$field['field']]['modified_function']($output);
             continue;
         } elseif (isset($field['submit_function'])) {
             // covers both insert_function and modified_function
             $this->preset[$field['field']]['submit_function']($output);
             continue;
         } else {
             switch (preg_replace('/\\([0-9]+\\)/', '', $field['type'])) {
                 case 'image':
                 case 'file':
                     if ($_FILES[$field['field']]['size'] > 0 && $_FILES[$field['field']]['error'] == UPLOAD_ERR_OK && ((!isset($field['replaceable']) || $field['replaceable'] === false) && $this->action == 'update' || $this->action == 'insert')) {
                         // delete previous file
                         $this->get_row();
                         if ($field['type'] == 'file') {
                             $val = $this->saveFile($field, $_FILES[$field['field']]);
                         } else {
                             $val = $this->saveImage($field, $_FILES[$field['field']]);
                         }
                     } else {
                         $ignore = true;
                     }
                     break;
                 case 'date':
                     $val = Time::getDate($field['form_field'], !empty($field['allow_blank']));
                     break;
                 case 'time':
                     $val = Time::getTime($field['form_field'], !empty($field['allow_blank']));
                     break;
                 case 'datetime':
                     $val = Time::getDateTime($field['form_field'], !empty($field['allow_blank']));
                     break;
                 case 'checkbox':
                     $val = (int) Request::get($field['form_field'], 'boolean');
                     break;
                 case 'checklist':
                     $vals = '';
                     $maxi = 0;
                     foreach ($field['options'] as $i => $opt) {
                         if (is_array($opt)) {
                             $maxi = max($maxi, $opt[0]);
                         } else {
                             $maxi = max($maxi, $i);
                         }
                     }
                     for ($i = 0; $i <= $maxi; $i++) {
                         $vals .= $_POST[$field['form_field'] . '_' . $i] == 1 || $_POST[$field['form_field'] . '_' . $i] == "on" ? 1 : 0;
                     }
                     $val = bindec(strrev($vals));
                     break;
                 case 'bit':
                     $val = ['bit' => decbin(Request::get($field['form_field'], 'int'))];
                     break;
                 case 'html':
                     $val = Request::get($field['form_field'], 'html', !empty($field['allowed_html']) ? $field['allowed_html'] : '', !empty($field['allowed_css']) ? $field['allowed_css'] : '', !empty($field['trusted']), !empty($field['full_page']));
                     break;
                 case 'int':
                 case 'float':
                 case 'email':
                 case 'url':
                     $val = Request::post($field['form_field'], $field['type']);
                     break;
                 default:
                     // This will include 'url'
                     // TODO: this can be set to include the date types above also.
                     $val = Request::get($field['form_field'], $field['type']);
                     break;
             }
         }
         // If there is an alternate default value
         if (!isset($val) && $this->action == "insert" && isset($field['default'])) {
             $val = $field['default'];
             // Developer input - could require sanitization.
             $sanitize = true;
         }
         // Sanitize the input.
         $sanitize_field = $this->action == 'insert' ? 'insert_sanitize' : 'modify_sanitize';
         if ($sanitize && (!isset($field[$sanitize_field]) || $field[$sanitize_field] !== false || (!isset($field['sanitize']) || $field['sanitize'] !== false))) {
             $val = $this->input_sanitize($val, $html);
         }
         // If this value is required.
         if (!empty($field['required']) && empty($val)) {
             Messenger::error('The field ' . $this->fields[$f]['display_name'] . ' is required.');
             $dependenciesMet = false;
         }
         // If the value needs to be encrypted
         if (!empty($field['encrypted'])) {
             $val = $this->encrypt($this->table, $field['field'], $val);
         }
         if (!$ignore && empty($field['no_save'])) {
             $output[$field['field']] = $val;
         }
     }
     $dependenciesMet &= $this->processFieldValues($output);
     return $dependenciesMet ? $output : false;
 }