/** * set new record in options * @param [array] $_args contain key and value of new record */ public static function set($_args, $_ifExistUpdate = true) { $datarow = ['option_status' => 'enable']; // add option user if set if (isset($_args['user'])) { $op_user = $_args['user']; if ($op_user === true) { $op_user = \lib\utility\visitor::user_id(false); if (!$op_user) { $op_user = '******'; } } if ($op_user) { $datarow['user_id'] = $op_user; } } // add option post if set if (isset($_args['post'])) { $datarow['post_id'] = $_args['post']; } // add option cat if set if (isset($_args['cat'])) { $datarow['option_cat'] = $_args['cat']; } else { return false; } // add option key if set if (isset($_args['key'])) { // replace _USER_ with user_id if exist $replace = ""; if (isset($datarow['user_id'])) { $replace = $datarow['user_id']; } $_args['key'] = str_replace('_USER_', $replace, $_args['key']); $datarow['option_key'] = $_args['key']; } else { return false; } // add option value if set if (isset($_args['value'])) { $datarow['option_value'] = $_args['value']; } else { $datarow['option_value'] = null; } // add option meta if set if (isset($_args['meta'])) { $datarow['option_meta'] = $_args['meta']; if (is_array($datarow['option_meta'])) { $datarow['option_meta'] = json_encode($datarow['option_meta'], JSON_UNESCAPED_UNICODE); } } // add option status if set if (isset($_args['status'])) { // only allow defined$_args['status'])e switch ($_args['status']) { case 'enable': case 'disable': case 'expire': break; default: $_args['status'] = 'enable'; break; } $datarow['option_status'] = $_args['status']; } // add date modified manually if (isset($_args['modify']) && $_args['modify'] === 'now') { $datarow['date_modified'] = 'now()'; } // create query string $qry_fields = implode(', ', array_keys($datarow)); foreach ($datarow as $key => $value) { switch ($key) { case 'user_id': case 'post_id': case 'date_modified': $datarow[$key] = $value; break; case 'option_meta': if ($value === '++') { $datarow[$key] = "coalesce({$key}, 0)" . '+1'; } else { $datarow[$key] = "'" . $value . "'"; } break; default: $datarow[$key] = "'" . $value . "'"; break; } } $qry_values = implode(', ', $datarow); // connect to database if ($_ifExistUpdate) { // start creating query data $qry_data = null; foreach ($datarow as $key => $value) { $qry_data .= $key . '=' . $datarow[$key] . ', '; } // remove last , $qry_data = substr($qry_data, 0, -2); if (isset($_args['id']) && is_numeric($_args['id'])) { $qry = "UPDATE options SET {$qry_data} WHERE `id` = " . $_args['id']; // var_dump($qry); } else { $qry = "UPDATE options\n\t\t\t\t\tSET {$qry_data}\n\t\t\t\t\tWHERE\n\t\t\t\t\t\t`option_cat` =" . $datarow['option_cat'] . " AND\n\t\t\t\t\t\t`option_key` =" . $datarow['option_key'] . " AND\n\t\t\t\t\t\t`option_value` =" . $datarow['option_value']; } $result = \lib\db::query($qry); // if row is match then return true // this means row is same and data is duplicate or not // affecting row is not important in this condition if ($result && \lib\db::qry_info('Rows matched')) { return true; } } // create query string $qry = "INSERT INTO options ( {$qry_fields} ) VALUES ( {$qry_values} );"; // execute query $result = \lib\db::query($qry); // give last insert id $last_id = @mysqli_insert_id(\lib\db::$link); // if have last insert it return it if ($last_id) { return $last_id; } // return default value return false; }