public function save($table, $data = false, $data_to_save_options = false)
{
if (is_array($table) and isset($table['table'])) {
$data = $table;
$table = $table['table'];
unset($data['table']);
}
if (is_string($data)) {
$data = parse_params($data);
}
if (!is_array($data)) {
return false;
}
$original_data = $data;
$is_quick = isset($original_data['quick_save']);
$skip_cache = isset($original_data['skip_cache']);
if (!isset($params['skip_timestamps'])) {
if (!isset($params['id']) or isset($params['id']) and $params['id'] == 0) {
if (!isset($params['created_at'])) {
$params['created_at'] = date('Y-m-d H:i:s');
}
}
if (!isset($params['updated_at'])) {
$params['updated_at'] = date('Y-m-d H:i:s');
}
}
if ($is_quick == false) {
if (isset($data['updated_at']) == false) {
$data['updated_at'] = date('Y-m-d H:i:s');
}
}
if ($skip_cache == false and isset($data_to_save_options) and !empty($data_to_save_options)) {
if (isset($data_to_save_options['delete_cache_groups']) and !empty($data_to_save_options['delete_cache_groups'])) {
foreach ($data_to_save_options['delete_cache_groups'] as $item) {
$this->app->cache_manager->delete($item);
}
}
}
$user_sid = $this->app->user_manager->session_id();
$the_user_id = $this->app->user_manager->id();
if (!isset($data['session_id']) and $user_sid) {
$data['session_id'] = $user_sid;
}
if (!isset($data['id'])) {
$data['id'] = 0;
}
if (isset($data['cf_temp'])) {
$cf_temp = $data['cf_temp'];
}
$allow_html = false;
$allow_scripts = false;
if (isset($data['allow_html']) and !isset($_REQUEST['allow_html'])) {
$allow_html = $data['allow_html'];
}
if (isset($data['allow_scripts']) and !isset($_REQUEST['allow_scripts'])) {
$allow_scripts = $data['allow_scripts'];
}
if (isset($data['debug']) and $data['debug'] == true) {
$dbg = 1;
unset($data['debug']);
} else {
$dbg = false;
}
if ($dbg != false) {
var_dump($data);
}
$data['user_ip'] = MW_USER_IP;
if (isset($data['id']) == false or $data['id'] == 0) {
$data['id'] = 0;
$l = $this->last_id($table);
$data['new_id'] = intval($l + 1);
$original_data['new_id'] = $data['new_id'];
}
if (!isset($the_user_id)) {
$the_user_id = 0;
}
if (intval($data['id']) == 0) {
if (isset($data['created_at']) == false) {
$data['created_at'] = date('Y-m-d H:i:s');
}
if ($the_user_id) {
$data['created_by'] = $the_user_id;
}
if ($the_user_id) {
$data['edited_by'] = $the_user_id;
}
} else {
if ($the_user_id) {
$data['edited_by'] = $the_user_id;
}
}
if (isset($data['position'])) {
$data['position'] = intval($data['position']);
}
$table_assoc_name = $this->assoc_table_name($table);
$criteria_orig = $data;
$criteria = $this->map_array_to_table($table, $data);
if ($allow_html == false) {
$criteria = $this->app->format->clean_html($criteria);
} else {
if ($allow_scripts == false) {
$criteria = $this->clean_input($criteria);
}
}
$table = $this->app->format->clean_html($table);
$criteria = $this->app->url_manager->replace_site_url($criteria);
if ($data_to_save_options['use_this_field_for_id'] != false) {
$criteria['id'] = $criteria_orig[$data_to_save_options['use_this_field_for_id']];
}
if ($dbg != false) {
var_dump($criteria);
}
if (!isset($criteria['id'])) {
$criteria['id'] = 0;
}
$criteria['id'] = intval($criteria['id']);
if (intval($criteria['id']) == 0) {
unset($criteria['id']);
$id_to_return = $this->table($table_assoc_name)->insert($criteria);
$id_to_return = $this->last_id($table);
} else {
$id_to_return = $this->table($table_assoc_name)->where('id', $criteria['id'])->update($criteria);
$id_to_return = $criteria['id'];
}
if ($id_to_return == false) {
$id_to_return = $this->last_id($table);
}
$id_to_return = intval($id_to_return);
$original_data['table'] = $table;
$original_data['id'] = $id_to_return;
$cache_group = $this->assoc_table_name($table);
$this->app->cache_manager->delete($cache_group);
if ($skip_cache == false) {
$cache_group = $this->assoc_table_name($table);
$this->app->cache_manager->delete($cache_group . '/global');
$this->app->cache_manager->delete($cache_group . '/' . $id_to_return);
if (isset($criteria['parent_id'])) {
$this->app->cache_manager->delete($cache_group . '/' . intval($criteria['parent_id']));
}
}
return $id_to_return;
}