/** * Clean the data before saving * * @access public * @param mixed an array of values to be saved * @return array */ public function clean($values = array()) { if (empty($values)) { $values = $_POST; } // get table information to clean against $fields = $this->table_info(); $clean = array(); foreach ($fields as $key => $val) { if (isset($values[$key])) { $values[$key] = $this->auto_trim ? trim($values[$key]) : $values[$key]; } } // process linked fields $values = $this->process_linked($values); foreach ($fields as $key => $field) { if ($field['type'] == 'time') { if (isset($values[$key . '_hour']) and is_numeric($values[$key . '_hour'])) { if (empty($values[$key]) or (int) $values[$key] == 0) { $values[$key] = $date_func('H:i:s'); } //the js seem like only supply minute field, assign 00 for sec now if (empty($values[$key . '_sec'])) { $values[$key . '_sec'] = '00'; } $values[$key] = date("H:i:s", strtotime(@$values[$key . '_hour'] . ':' . @$values[$key . '_min'] . ':' . @$values[$key . '_sec'] . ' ' . @$values[$key . '_am_pm'])); } } else { if ($field['type'] == 'datetime') { if (empty($values[$key]) or (int) $values[$key] == 0) { $values[$key] = $this->default_date; } if (isset($values[$key . '_hour'])) { if (!empty($values[$key])) { $values[$key] = english_date_to_db_format($values[$key], @$values[$key . '_hour'], @$values[$key . '_min'], @$values[$key . '_sec'], @$values[$key . '_am_pm']); } } } else { if ($field['type'] == 'date') { if (empty($values[$key]) or (int) $values[$key] == 0) { $values[$key] = $this->default_date; } if (!empty($values[$key]) and !is_date_db_format($values[$key])) { $values[$key] = english_date_to_db_format($values[$key]); } } } } $date_func = $this->date_use_gmt ? 'gmdate' : 'date'; // create dates for date added and last updated fields automatically if (($field['type'] == 'datetime' or $field['type'] == 'timestamp' or $field['type'] == 'date') and in_array($key, $this->auto_date_add)) { $test_date = isset($values[$key]) ? (int) $values[$key] : 0; // if no key field then we assume it is a new save and so we add the date if it's empty' if (!$this->_has_key_field_value($values) and empty($test_date)) { $values[$key] = $field['type'] == 'date' ? $date_func('Y-m-d') : $date_func('Y-m-d H:i:s'); } } else { if (($field['type'] == 'datetime' or $field['type'] == 'timestamp' or $field['type'] == 'date') and in_array($key, $this->auto_date_update)) { $values[$key] = $field['type'] == 'date' ? $date_func('Y-m-d') : $date_func('Y-m-d H:i:s'); } } if (isset($values[$key])) { // format dates if (!in_array($key, $this->auto_date_add)) { if ($field['type'] == 'datetime' or $field['type'] == 'timestamp' or $field['type'] == 'date') { if (isset($values[$key]) and strncmp($values[$key], '0000', 4) !== 0) { if ($field['type'] == 'date') { $values[$key] = $values[$key] != 'invalid' ? $date_func('Y-m-d', strtotime($values[$key])) : $this->default_date; } else { $values[$key] = $values[$key] != 'invalid' ? $date_func('Y-m-d H:i:s', strtotime($values[$key])) : $this->default_date; } } } } // safe_htmlspecialchars is buggy for unserialize so we use the cleanup_ms_word if ($this->auto_encode_entities) { if (is_array($this->auto_encode_entities) and in_array($key, $this->auto_encode_entities) or is_string($this->auto_encode_entities) and $key == $this->auto_encode_entities or $this->auto_encode_entities === TRUE) { $values[$key] = safe_htmlentities($values[$key]); } } if ($this->xss_clean) { if (is_array($this->xss_clean) and in_array($key, $this->xss_clean) or is_string($this->xss_clean) and $key == $this->xss_clean or $this->xss_clean === TRUE) { $values[$key] = xss_clean($values[$key]); } } $clean[$key] = $values[$key]; } } $this->cleaned_data = $clean; return $clean; }
</pre> <h2>time_verbose('<var>time</var>', <var>[include_seconds]</var>)</h2> <p>Returns the time into a verbose format (e.g. 12hrs 10mins 10secs).</p> <pre class="brush: php"> time_verbose(time(), TRUE); //returns <?=time_verbose(time(), TRUE)?> </pre> <h2>english_date_to_db_format(<var>date</var>, <var>[hour]</var>, <var>[min]</var>, <var>[sec]</var>, <var>[ampm]</var>, <var>[delimiter]</var>)</h2> <p>Converts a date from english (e.g. mm/dd/yyyy) to db format (e.g yyyy-mm-dd). The default delimiter parameter is <strong>'/'</strong>.</p> <pre class="brush: php"> english_date_to_db_format('01/01/2010', 12, 10, 0); // returns <?=english_date_to_db_format('01/01/2010', 12, 10, 0)?> </pre> <h2>format_db_date(<var>[y]</var>, <var>[m]</var>, <var>[d]</var>, <var>[h]</var>, <var>[i]</var>, <var>[s]</var>)</h2> <p>Formats a date for the database in (yyyy-mm-dd hh:mm:ss) format. If a value is left out, it will return the current date/time value for that slot.</p> <pre class="brush: php"> format_db_date(2010, 1, 1, 12, 10, 10); // returns <?=format_db_date(2010, 1, 1, 12, 10, 10)?> </pre> <h2>date_range_string(<var>date1</var>, <var>date2</var>)</h2> <p>Creates a date range string (e.g. January 1-10, 2010).</p> <pre class="brush: php">
/** * Clean the data before saving * <code> $cleaned_data = $this->examples_model->clean($_POST); // Be sure to always clean your $_POST variables before using them </code> * * @access public * @param mixed an array of values to be saved (optional) * @param boolean run on_before_clean hook or not (optional) * @return array */ public function clean($values = array(), $run_hook = FALSE) { $CI =& get_instance(); if (empty($values)) { $values = $CI->input->post(); } $original_values = $values; // run clean hook if ($run_hook) { $values = $this->on_before_clean($values); } // get table information to clean against $fields = $this->table_info(); $clean = array(); $values = array(); foreach ($fields as $key => $val) { if (is_array($original_values) and array_key_exists($key, $original_values)) { $values[$key] = ($this->auto_trim and is_string($original_values[$key])) ? trim($original_values[$key]) : $original_values[$key]; } elseif (is_array($original_values) and !array_key_exists($key, $original_values) and (in_array($key, $this->auto_date_add) or in_array($key, $this->auto_date_update))) { $values[$key] = NULL; } } // process linked fields $values = $this->process_linked($values); foreach ($values as $key => $field) { $field = $fields[$key]; if ($field['type'] == 'date' or $field['type'] == 'datetime') { if (empty($values[$key]) or (int) $values[$key] == 0) { $values[$key] = $this->default_date; } if (!empty($values[$key]) and !is_date_db_format($values[$key])) { $values[$key] = english_date_to_db_format($values[$key]); } } $date_func = $this->date_use_gmt ? 'gmdate' : 'date'; // create dates for date added and last updated fields automatically $is_date_field_type = ($field['type'] == 'datetime' or $field['type'] == 'timestamp' or $field['type'] == 'date'); if ($is_date_field_type and in_array($key, $this->auto_date_add)) { $test_date = isset($values[$key]) ? (int) $values[$key] : 0; // if no key field then we assume it is a new save and so we add the date if it's empty' if (!$this->_has_key_field_value($values) and empty($test_date)) { $values[$key] = $field['type'] == 'date' ? $date_func('Y-m-d') : $date_func('Y-m-d H:i:s'); } } else { if ($is_date_field_type and in_array($key, $this->auto_date_update)) { $values[$key] = $field['type'] == 'date' ? $date_func('Y-m-d') : $date_func('Y-m-d H:i:s'); } } if (is_array($values) and array_key_exists($key, $values)) { // format dates if (!in_array($key, $this->auto_date_add)) { if ($field['type'] == 'datetime' or $field['type'] == 'timestamp' or $field['type'] == 'date') { if (isset($values[$key]) and strncmp($values[$key], '0000', 4) !== 0) { if ($field['type'] == 'date') { $values[$key] = $values[$key] != 'invalid' ? $date_func('Y-m-d', strtotime($values[$key])) : $this->default_date; } else { $values[$key] = $values[$key] != 'invalid' ? $date_func('Y-m-d H:i:s', strtotime($values[$key])) : $this->default_date; } } // set it to an empty string an not a 0 so that it will work with the required validator if (empty($values[$key])) { $values[$key] = ''; } } } // safe_htmlspecialchars is buggy for unserialize so we use the encode_and_clean if (is_string($values[$key])) { $values[$key] = $this->encode_and_clean($values[$key], NULL, $key); } else { if (is_array($values[$key])) { array_walk_recursive($values[$key], array($this, 'encode_and_clean'), $key); } } $clean[$key] = $values[$key]; } } $this->cleaned_data = $clean; return $clean; }
<h2>time_verbose('<var>time</var>', <var>[include_seconds]</var>)</h2> <p>Returns the time into a verbose format (e.g. 12hrs 10mins 10secs).</p> <pre class="brush: php"> time_verbose(time(), TRUE); //returns <?php echo time_verbose(time(), TRUE); ?> </pre> <h2>english_date_to_db_format(<var>date</var>, <var>[hour]</var>, <var>[min]</var>, <var>[sec]</var>, <var>[ampm]</var>, <var>[delimiter]</var>)</h2> <p>Converts a date from english (e.g. mm/dd/yyyy) to db format (e.g yyyy-mm-dd). The default delimiter parameter is <strong>'/'</strong>.</p> <pre class="brush: php"> english_date_to_db_format('01/01/2010', 12, 10, 0); // returns <?php echo english_date_to_db_format('01/01/2010', 12, 10, 0); ?> </pre> <h2>format_db_date(<var>[y]</var>, <var>[m]</var>, <var>[d]</var>, <var>[h]</var>, <var>[i]</var>, <var>[s]</var>)</h2> <p>Formats a date for the database in (yyyy-mm-dd hh:mm:ss) format. If a value is left out, it will return the current date/time value for that slot.</p> <pre class="brush: php"> format_db_date(2010, 1, 1, 12, 10, 10); // returns <?php echo format_db_date(2010, 1, 1, 12, 10, 10); ?> </pre> <h2>date_range_string(<var>date1</var>, <var>date2</var>)</h2>