/** * Check date **/ function carbo_check_date($date, $format) { $this->set_message('carbo_check_date', lang('cg_check_date')); if (!$format) { $format = 'm/d/Y'; } $pformat = preg_replace('/([dDljmMFnYyGgHhAais])/', '%$1', $format); $ret = carbo_strptime($date, $pformat); if ($ret === FALSE or !isset($ret['tm_mon']) or !isset($ret['tm_mday']) or !isset($ret['tm_year'])) { return FALSE; } if (!checkdate($ret['tm_mon'], $ret['tm_mday'], $ret['tm_year'])) { return FALSE; } return carbo_format_date($ret, $format, $format); }
/** * Parse filter value **/ function parse_filter_value($column, $value) { switch ($column->type) { // Boolean //case 'boolean': //return; case 'date': return carbo_format_date($value, $column->date_format, 'Y-m-d'); case 'datetime': return carbo_format_date($value, $column->date_format . ' ' . $column->time_format, 'Y-m-d H:i:s'); case 'time': return carbo_format_date($value, $column->time_format, 'H:i:s'); default: return $value; } }
/** * Save item **/ function save_item($table, $id_name, $fields, $item_id = NULL, $item_data = NULL, $filters = array()) { if (count($fields)) { //$table_data = array("timestamp" => date("Y-m-d H:i:s")); $table_data = array(); foreach ($fields as $key => $field) { $value = $this->input->post("cg_field_" . $key); $value = $value === FALSE ? $field->form_default : $value; //$value = $values[$key]; switch ($field->type) { // Boolean case 'boolean': $table_data[$field->db_name] = $value ? 1 : 0; break; // Date // Date case 'date': // Convert to MySQL date $table_data[$field->db_name] = $value ? carbo_format_date($value, $field->date_format, 'Y-m-d') : NULL; break; case 'datetime': // Convert to MySQL datetime $table_data[$field->db_name] = $value ? carbo_format_date($value, $field->date_format . ' ' . $field->time_format, 'Y-m-d H:i:s') : NULL; break; // Date // Date case 'time': // Convert to MySQL time $table_data[$field->db_name] = $value ? carbo_format_date($value, $field->time_format, 'H:i:s') : NULL; break; // File // File case 'file': $path = rtrim($field->upload_path, '/') . '/'; $path_temp = rtrim($field->upload_path_temp, '/') . '/'; // Delete old file if (!is_null($item_data) and $item_data->{$field->db_name} !== $value and file_exists($path . $item_data->{$field->db_name})) { @unlink($path . $item_data->{$field->db_name}); } // Copy new file to permanent location if (file_exists($path_temp . $value)) { @rename($path_temp . $value, $path . $value); } default: if ($value !== FALSE) { $table_data[$field->db_name] = $value === "" ? NULL : $value; } } } if (is_null($item_id)) { // Insert the item $this->db->insert($table, $table_data); $insert_id = $this->db->insert_id(); } else { // Update the item $this->db->where($id_name, $item_id); $this->set_filters($filters); $this->db->update($table, $table_data); } return TRUE; } return FALSE; }
if ($column->display and method_exists($grid->CI, $column->display)) { echo $grid->CI->{$column->display}($data_id, $key, $data_cell); } else { switch ($column->type) { // Boolean case 'boolean': echo $data_cell ? '<span class="ui-icon ui-icon-check"></span>' : ''; break; case 'date': echo carbo_format_date($data_cell, 'Y-m-d', $column->date_format); break; case 'datetime': echo carbo_format_date($data_cell, 'Y-m-d H:i:s', $column->date_format . ' ' . $column->time_format); break; case 'time': echo carbo_format_date($data_cell, 'H:i:s', $column->time_format); break; case 'url': echo $data_cell ? '<a href="' . $data_cell . '" target="' . $column->url_target . '">' . $data_cell . '</a>' : ' '; break; /*case 'file': $path = rtrim(str_replace('./', '', $grid->columns[$j]['upload_path']), '/') . '/'; echo '<audio src="' . base_url() . $path . $data_cell . '" controls="controls">Your browser does not support audio</audio>'; break;*/ /*case 'file': $path = rtrim(str_replace('./', '', $grid->columns[$j]['upload_path']), '/') . '/'; echo '<audio src="' . base_url() . $path . $data_cell . '" controls="controls">Your browser does not support audio</audio>'; break;*/ default: // Trim string data to max length if (strlen($data_cell) > $grid->max_cell_length) {
/** * Run **/ function run($validate = FALSE) { $this->CI->load->library('form_validation'); $item_data = NULL; if (!is_null($this->item_id)) { $item_data = $this->CI->Carbo_model->get_item($this->table, $this->table_id_name, $this->columns, $this->item_id); } foreach ($this->columns as $key => $column) { // Set up validation rules $validation = 'trim|xss_clean' . ($column->validation ? '|' . $column->validation : ''); // Set type specific validation switch ($column->type) { case 'integer': $validation .= "|integer"; break; case 'date': $validation .= "|carbo_check_date[{$column->date_format}]"; break; case 'datetime': $validation .= "|carbo_check_date[{$column->date_format} {$column->time_format}]"; break; case 'time': $validation .= "|carbo_check_date[{$column->time_format}]"; break; } if (!is_null($column->min_length)) { $validation .= '|min_length[' . $column->min_length . ']'; } if (!is_null($column->max_length)) { $validation .= '|max_length[' . $column->max_length . ']'; } if ($column->required) { $validation .= '|required'; } if ($column->unique) { $validation .= '|carbo_check_unique[' . $this->table . ':' . $this->table_id_name . ':' . $column->where_name . (is_null($this->item_id) ? '' : ':' . $this->item_id) . ']'; } if ($column->form_control == 'file') { $path_temp = rtrim($column->upload_path_temp, '/') . '/'; if (isset($_FILES['cg_field_' . $key]) && $_FILES['cg_field_' . $key]['name']) { // Delete old file if (file_exists($path_temp . $_POST['cg_field_' . $key])) { @unlink($path_temp . $_POST['cg_field_' . $key]); } $_POST['cg_field_' . $key] = $_FILES['cg_field_' . $key]['name']; } else { if ($this->CI->input->post('cg_delete_file_' . $key) !== FALSE) { // Delete old file if (file_exists($path_temp . $_POST['cg_field_' . $key])) { @unlink($path_temp . $_POST['cg_field_' . $key]); } $_POST['cg_field_' . $key] = ''; } } $validation .= '|carbo_check_upload[cg_field_' . $key . ':' . $column->upload_path_temp . ':' . preg_replace('/\\|/', '&', $column->allowed_types) . ':' . $column->max_size . ']'; } $this->CI->form_validation->set_rules('cg_field_' . $key, $column->name, $validation); } $this->CI->form_validation->set_error_delimiters('<li>', '</li>'); if ($validate) { if ($this->CI->form_validation->run() !== FALSE) { $this->CI->Carbo_model->save_item($this->table, $this->table_id_name, $this->columns, $this->item_id, $item_data, $this->filters); return TRUE; } } // Set form values foreach ($this->columns as $key => $column) { if (($value = $this->CI->input->post('cg_field_' . $key)) !== FALSE) { if ($column->form_control == 'file' and form_error('cg_field_' . $key)) { $this->formdata[$key] = ''; } else { $this->formdata[$key] = $value; } } else { $this->formdata[$key] = is_null($this->item_id) ? $column->form_default : $item_data->{$column->unique_name}; // Set type specific value formating switch ($column->type) { case 'date': $this->formdata[$key] = is_null($this->item_id) ? $column->form_default : carbo_format_date($item_data->{$column->unique_name}, 'Y-m-d', $column->date_format); break; case 'datetime': $this->formdata[$key] = is_null($this->item_id) ? $column->form_default : carbo_format_date($item_data->{$column->unique_name}, 'Y-m-d H:i:s', $column->date_format . ' ' . $column->time_format); break; case 'time': $this->formdata[$key] = is_null($this->item_id) ? $column->form_default : carbo_format_date($item_data->{$column->unique_name}, 'H:i:s', $column->time_format); break; case '1-n': $this->formdata[$key] = is_null($this->item_id) ? $column->form_default : $item_data->{$column->unique_name . '_id'}; break; } } } return FALSE; }