AutoInsertUpdate() публичный Метод

Automatically does an INSERT or UPDATE depending if an existing record exists in a table
public AutoInsertUpdate ( string $tableName, array $valuesArray, array $whereArray ) : boolean
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$whereArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect).
Результат boolean Returns TRUE on success or FALSE on error
Пример #1
0
 /**
  * Save one or more preferences for a user. If no user ID is given the current user is used.
  * The array has to assign every preference key a value to store.
  * Example: array ( 'setting1' => 'value1', 'setting2' => 'value2');
  *
  * A prefix can be specified, which will be prepended to every preference key.
  *
  * @param array   $data   key/value pairs to store
  * @param string  $prefix prefix for all preferences
  * @param integer $userId (optional) id of another user than the current
  * @return boolean        true on success, false on failure
  * @author sl
  */
 public function user_set_preferences(array $data, $prefix = '', $userId = null)
 {
     if ($userId === null) {
         $userId = $this->kga['user']['userID'];
     }
     if (!$this->conn->TransactionBegin()) {
         $this->logLastError('user_set_preferences');
         return false;
     }
     $table = $this->kga['server_prefix'] . "preferences";
     $filter['userID'] = MySQL::SQLValue($userId, MySQL::SQLVALUE_NUMBER);
     $values['userID'] = $filter['userID'];
     foreach ($data as $key => $value) {
         $values['option'] = MySQL::SQLValue($prefix . $key);
         $values['value'] = MySQL::SQLValue($value);
         $filter['option'] = $values['option'];
         $this->conn->AutoInsertUpdate($table, $values, $filter);
     }
     return $this->conn->TransactionEnd();
 }
/** Quên mật khẩu admin cp */
function admin_cp_lostpw()
{
    global $hmuser;
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    hook_action('admin_cp_lostpw');
    $user_email = hm_post('email');
    $reset_password = hm_post('reset-password');
    if (is_numeric($reset_password)) {
        $tableName = DB_PREFIX . "users";
        $whereArray = array('user_email' => MySQL::SQLValue($user_email));
        $hmdb->SelectRows($tableName, $whereArray);
        if ($hmdb->HasRecords()) {
            $row = $hmdb->Row();
            $user_id = $row->id;
            $user_login = $row->user_login;
            $lostpw_key = generateRandomString(10);
            $values["name"] = MySQL::SQLValue('lostpw_key');
            $values["val"] = MySQL::SQLValue($lostpw_key);
            $values["object_id"] = MySQL::SQLValue($user_id, MySQL::SQLVALUE_NUMBER);
            $values["object_type"] = MySQL::SQLValue('user');
            $whereArray = array('object_id' => MySQL::SQLValue($user_id, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('user'), 'name' => MySQL::SQLValue('lostpw_key'));
            $tableName = DB_PREFIX . "field";
            $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
            $newpw_url = SITE_URL . FOLDER_PATH . HM_ADMINCP_DIR . '?run=login.php&action=newpw&key=' . $lostpw_key;
            $mail_content = 'Một ai đó đã yêu cầu đặt lại mật khẩu cho tài khoản ' . $user_login . ', nếu bạn đã làm điều đó vui lòng <a href="' . $newpw_url . '">bấm vào đây để đặt mật khẩu mới</a>';
            //send mail
            $send_mail = mail($user_email, _('Lấy lại mật khẩu:') . SITE_URL, $message);
            if ($send_mail) {
                return json_encode(array('status' => 'success', 'mes' => _('Mật khẩu mới đã được gửi về email')));
            } else {
                return json_encode(array('status' => 'error', 'mes' => _('Máy chủ không thể gửi mail xác nhận đổi mật khẩu, vui lòng báo với chủ trang web')));
            }
        } else {
            return json_encode(array('status' => 'error', 'mes' => _('Email này không khớp với bất kì tài khoản nào')));
        }
    }
}
/** Active / Deactive plugin */
function disable_plugin($status, $plugin_key = FALSE)
{
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    if ($plugin_key == FALSE) {
        $plugin_key = hm_post('plugin');
    }
    if (is_dir(BASEPATH . HM_PLUGIN_DIR . '/' . $plugin_key) and is_file(BASEPATH . HM_PLUGIN_DIR . '/' . $plugin_key . '/' . $plugin_key . '.php')) {
        $tableName = DB_PREFIX . 'plugin';
        $values["active"] = MySQL::SQLValue($status);
        $values["key"] = MySQL::SQLValue($plugin_key);
        $whereArray = array('key' => MySQL::SQLValue($plugin_key));
        $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
        $mes = NULL;
        if ($status == '1') {
            $mes = $plugin_key . ' active';
        } elseif ($status == '0') {
            $mes = $plugin_key . ' deactive';
        }
        $args = array('status' => 'success', 'mes' => $mes);
        return json_encode($args);
    }
}
Пример #4
0
/** Load template user box */
function ajax_add_user($args = array())
{
    global $hmuser;
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    hook_action('ajax_add_user');
    if (isset($args['id_update'])) {
        $id_update = $args['id_update'];
    } else {
        $id_update = NULL;
    }
    $user_login = hm_post('user_login');
    $password = hm_post('password');
    $password2 = hm_post('password2');
    $nicename = hm_post('nicename');
    $user_email = hm_post('user_email');
    $userrole = hm_post('userrole');
    $user_group = hm_post('user_group', 0);
    $salt = rand(100000, 999999);
    $user_activation_key = '0';
    if ($password != $password2) {
        return json_encode(array('status' => 'error', 'mes' => _('Hai mật khẩu nhập vào không khớp')));
        hm_exit();
    }
    $tableName = DB_PREFIX . "users";
    /** check trùng user login */
    if (!is_numeric($id_update)) {
        $whereArray = array('user_login' => MySQL::SQLValue($user_login));
        $hmdb->SelectRows($tableName, $whereArray);
        if ($hmdb->HasRecords()) {
            return json_encode(array('status' => 'error', 'mes' => _('Tài khoản này đã tồn tại')));
            hm_exit();
        }
    }
    $password_encode = hm_encode_str(md5($password . $salt));
    /** Thêm tài khoản */
    $values["user_login"] = MySQL::SQLValue($user_login);
    $values["user_nicename"] = MySQL::SQLValue($nicename);
    $values["user_email"] = MySQL::SQLValue($user_email);
    $values["user_activation_key"] = MySQL::SQLValue($user_activation_key);
    $values["user_role"] = MySQL::SQLValue($userrole);
    $values["user_group"] = MySQL::SQLValue($user_group);
    if (is_numeric($id_update)) {
        if ($password != '') {
            $values["user_pass"] = MySQL::SQLValue($password_encode);
            $values["salt"] = MySQL::SQLValue($salt);
        }
        $whereArray = array('id' => $id_update);
        $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
        $insert_id = $id_update;
    } else {
        $values["user_pass"] = MySQL::SQLValue($password_encode);
        $values["salt"] = MySQL::SQLValue($salt);
        $insert_id = $hmdb->InsertRow($tableName, $values);
    }
    /** Lưu user field */
    foreach ($_POST as $post_key => $post_val) {
        if (is_numeric($insert_id)) {
            if (is_array($post_val)) {
                $post_val = json_encode($post_val);
            }
            $tableName = DB_PREFIX . 'field';
            if ($post_key != 'password' and $post_key != 'password2') {
                $values["name"] = MySQL::SQLValue($post_key);
                $values["val"] = MySQL::SQLValue($post_val);
                $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                $values["object_type"] = MySQL::SQLValue('user');
                if (is_numeric($id_update)) {
                    $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('user'), 'name' => MySQL::SQLValue($post_key));
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
            }
            unset($values);
        }
    }
    if (is_numeric($id_update)) {
        return json_encode(array('status' => 'updated', 'mes' => _('Đã sửa thông tin tài khoản : ' . $user_login)));
    } else {
        return json_encode(array('status' => 'success', 'mes' => _('Đã thêm tài khoản : ' . $user_login)));
    }
}
function content_ajax_add($args = array())
{
    global $hmcontent;
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    hook_action('content_ajax_add');
    if (isset($args['content_key'])) {
        $key = $args['content_key'];
    } else {
        $key = NULL;
    }
    if (isset($args['id_update'])) {
        $id_update = $args['id_update'];
    } else {
        $id_update = NULL;
    }
    if (isset($args['parent'])) {
        $parent = $args['parent'];
    } else {
        $parent = 0;
    }
    if (isset($args['status'])) {
        $status = $args['status'];
    } else {
        $status = 'public';
    }
    $con = $hmcontent->hmcontent;
    if (isset($_POST['taxonomy'])) {
        $method_post_taxonomy = $_POST['taxonomy'];
    } else {
        $method_post_taxonomy = array();
    }
    if ($key != NULL and isset($con[$key])) {
        $this_con = $con[$key];
        foreach ($_POST as $post_key => $post_val) {
            $primary = $this_con['content_field']['primary_name_field']['name'];
            /** input này được dùng làm khóa tên chính */
            if ($post_key == $primary) {
                $con_name = $post_val;
                if (isset($_POST['slug_of_' . $post_key])) {
                    if (is_numeric($id_update)) {
                        $con_slug = $_POST['slug_of_' . $post_key];
                        $con_slug = str_replace('#', '-', $con_slug);
                    } else {
                        $con_slug = add_request_uri_custom($_POST['slug_of_' . $post_key]);
                    }
                } else {
                    switch ($_POST['accented_of_' . $post_key]) {
                        case 'true':
                            if (is_numeric($id_update)) {
                                $con_slug = sanitize_title_with_accented($post_val);
                            } else {
                                $con_slug = add_request_uri_with_accented($post_val);
                            }
                            break;
                        case 'false':
                            if (is_numeric($id_update)) {
                                $con_slug = sanitize_title($post_val);
                            } else {
                                $con_slug = add_request_uri($post_val);
                            }
                            break;
                        default:
                            if (is_numeric($id_update)) {
                                $con_slug = sanitize_title($post_val);
                            } else {
                                $con_slug = add_request_uri($post_val);
                            }
                    }
                }
                /** lưu content vào database */
                $tableName = DB_PREFIX . 'content';
                $values["name"] = MySQL::SQLValue($con_name);
                $values["slug"] = MySQL::SQLValue($con_slug);
                $values["key"] = MySQL::SQLValue($key);
                $values["status"] = MySQL::SQLValue($status);
                $values["parent"] = MySQL::SQLValue($parent);
                if (is_numeric($id_update)) {
                    $args_con = content_data_by_id($id_update);
                    $key = $args_con['content']->key;
                    $status = $args_con['content']->status;
                    $parent = $args_con['content']->parent;
                    $values["key"] = MySQL::SQLValue($key);
                    $values["status"] = MySQL::SQLValue($status);
                    $values["parent"] = MySQL::SQLValue($parent);
                    $whereArray = array('id' => $id_update);
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                    $insert_id = $id_update;
                    up_date_request_uri_object($con_slug, $insert_id, 'content', 'has_object');
                } else {
                    $insert_id = $hmdb->InsertRow($tableName, $values);
                    up_date_request_uri_object($con_slug, $insert_id, 'content', 'null_object');
                }
                unset($values);
            }
        }
        foreach ($_POST as $post_key => $post_val) {
            /** lưu content field vào data */
            if (is_numeric($insert_id)) {
                if (is_array($post_val)) {
                    $post_val = json_encode($post_val);
                }
                $tableName = DB_PREFIX . 'field';
                $values["name"] = MySQL::SQLValue($post_key);
                $values["val"] = MySQL::SQLValue($post_val);
                $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                $values["object_type"] = MySQL::SQLValue('content');
                if (is_numeric($id_update)) {
                    $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('content'), 'name' => MySQL::SQLValue($post_key));
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
                unset($values);
            }
        }
        /** Lưu content time */
        if (is_numeric($insert_id)) {
            $day = $_POST['day'];
            if (!is_numeric($day)) {
                $day = 0;
            }
            $month = $_POST['month'];
            if (!is_numeric($month)) {
                $month = 0;
            }
            $year = $_POST['year'];
            if (!is_numeric($year)) {
                $year = 0;
            }
            $hour = $_POST['hour'];
            if (!is_numeric($hour)) {
                $hour = 0;
            }
            $minute = $_POST['minute'];
            if (!is_numeric($minute)) {
                $minute = 0;
            }
            $public_time = strtotime($day . '-' . $month . '-' . $year . ' ' . $hour . ':' . $minute);
            $values["name"] = MySQL::SQLValue('public_time');
            $values["val"] = MySQL::SQLValue($public_time);
            $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
            $values["object_type"] = MySQL::SQLValue('content');
            if (is_numeric($id_update)) {
                $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('content'), 'name' => MySQL::SQLValue('public_time'));
                $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
            } else {
                $hmdb->InsertRow($tableName, $values);
            }
            unset($values);
        }
        /** lưu relationship content - taxonomy */
        $tableName = DB_PREFIX . 'relationship';
        if (isset($method_post_taxonomy) and is_array($method_post_taxonomy)) {
            foreach ($method_post_taxonomy as $taxonomy_id) {
                $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                $values["target_id"] = MySQL::SQLValue($taxonomy_id, MySQL::SQLVALUE_NUMBER);
                $values["relationship"] = MySQL::SQLValue('contax');
                if (is_numeric($id_update)) {
                    $hmdb->AutoInsertUpdate($tableName, $values, $values);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
                unset($values);
            }
        }
        /** Gỡ bỏ contax đã bỏ chọn */
        if (is_numeric($id_update)) {
            $id_deletes = array();
            $whereArray = array('object_id' => MySQL::SQLValue($insert_id), 'relationship' => MySQL::SQLValue('contax'));
            $hmdb->SelectRows($tableName, $whereArray);
            if ($hmdb->HasRecords()) {
                while ($row = $hmdb->Row()) {
                    $id_relationship = $row->id;
                    $target_id = $row->target_id;
                    if (!in_array($target_id, $method_post_taxonomy)) {
                        $id_deletes[] = $id_relationship;
                    }
                }
            } else {
                $id_deletes = array();
            }
            foreach ($id_deletes as $id_delete) {
                $whereArray = array('id' => MySQL::SQLValue($id_delete, MySQL::SQLVALUE_NUMBER));
                $hmdb->DeleteRows($tableName, $whereArray);
            }
        }
        /** Gỡ bỏ contag cũ */
        if (is_numeric($id_update)) {
            $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'relationship' => MySQL::SQLValue('contag'));
            $hmdb->DeleteRows($tableName, $whereArray);
        }
        /** lưu tags vào data */
        if (isset($_POST['tags'])) {
            $tags = explode(',', $_POST['tags']);
            $tags = array_map("trim", $tags);
            foreach ($tags as $tag) {
                /** Lưu tag vào bảng taxonomy */
                $tableName = DB_PREFIX . 'taxonomy';
                $tag_slug = sanitize_title($tag);
                $values["name"] = MySQL::SQLValue($tag);
                $values["slug"] = MySQL::SQLValue($tag_slug);
                $values["key"] = MySQL::SQLValue('tag');
                $whereArray = array('key' => MySQL::SQLValue('tag'), 'slug' => MySQL::SQLValue($tag_slug));
                $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                unset($values);
                /** lưu relationship content - tag */
                $tableName = DB_PREFIX . "taxonomy";
                $whereArray = array('key' => MySQL::SQLValue('tag'), 'slug' => MySQL::SQLValue($tag_slug));
                $hmdb->SelectRows($tableName, $whereArray);
                if ($hmdb->HasRecords()) {
                    $row = $hmdb->Row();
                    $tableName = DB_PREFIX . 'relationship';
                    $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                    $values["target_id"] = MySQL::SQLValue($row->id, MySQL::SQLVALUE_NUMBER);
                    $values["relationship"] = MySQL::SQLValue('contag');
                    if (is_numeric($id_update)) {
                        $hmdb->AutoInsertUpdate($tableName, $values, $values);
                    } else {
                        $hmdb->InsertRow($tableName, $values);
                    }
                    unset($values);
                }
            }
        }
        /** show latest */
        $latest = array('id' => $insert_id, 'name' => $con_name, 'slug' => $con_slug, 'key' => $key);
        return json_encode(array('latest' => $latest));
    }
}
function taxonomy_ajax_add($key, $id_update = NULL)
{
    global $hmtaxonomy;
    $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET);
    hook_action('taxonomy_ajax_add');
    $tax = $hmtaxonomy->hmtaxonomy;
    if (isset($tax[$key])) {
        $this_tax = $tax[$key];
        foreach ($_POST as $post_key => $post_val) {
            $primary = $this_tax['taxonomy_field']['primary_name_field']['name'];
            /** input này được dùng làm khóa tên chính */
            if ($post_key == $primary) {
                $tax_name = $post_val;
                $parent = hm_post('parent');
                if (!is_numeric($parent)) {
                    $parent = 0;
                }
                if ($parent == $id_update) {
                    $parent = 0;
                }
                if (isset($_POST['slug_of_' . $post_key])) {
                    if (is_numeric($id_update)) {
                        $tax_slug = $_POST['slug_of_' . $post_key];
                    } else {
                        $tax_slug = add_request_uri_custom($_POST['slug_of_' . $post_key]);
                    }
                } else {
                    switch ($_POST['accented_of_' . $post_key]) {
                        case 'true':
                            if (is_numeric($id_update)) {
                                $tax_slug = sanitize_title_with_accented($post_val);
                            } else {
                                $tax_slug = add_request_uri_with_accented($post_val);
                            }
                            break;
                        case 'false':
                            if (is_numeric($id_update)) {
                                $tax_slug = sanitize_title($post_val);
                            } else {
                                $tax_slug = add_request_uri($post_val);
                            }
                            break;
                        default:
                            if (is_numeric($id_update)) {
                                $tax_slug = sanitize_title($post_val);
                            } else {
                                $tax_slug = add_request_uri($post_val);
                            }
                    }
                }
                /** lưu taxonomy vào data base */
                $tableName = DB_PREFIX . 'taxonomy';
                $values["name"] = MySQL::SQLValue($tax_name);
                $values["slug"] = MySQL::SQLValue($tax_slug);
                $values["key"] = MySQL::SQLValue($key);
                $values["parent"] = MySQL::SQLValue($parent);
                $values["status"] = MySQL::SQLValue('public');
                if (is_numeric($id_update)) {
                    unset($values["status"]);
                    $whereArray = array('id' => $id_update);
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                    $insert_id = $id_update;
                    up_date_request_uri_object($tax_slug, $insert_id, 'taxonomy', 'has_object');
                } else {
                    $insert_id = $hmdb->InsertRow($tableName, $values);
                    up_date_request_uri_object($tax_slug, $insert_id, 'taxonomy', 'null_object');
                }
                $latest_array = array('id' => $insert_id, 'name' => $tax_name, 'slug' => $tax_slug, 'key' => $key, 'parent' => $parent);
                unset($values);
            }
        }
        foreach ($_POST as $post_key => $post_val) {
            /** lưu taxonomy field vào data */
            if (is_numeric($insert_id)) {
                $tableName = DB_PREFIX . 'field';
                $values["name"] = MySQL::SQLValue($post_key);
                $values["val"] = MySQL::SQLValue($post_val);
                $values["object_id"] = MySQL::SQLValue($insert_id, MySQL::SQLVALUE_NUMBER);
                $values["object_type"] = MySQL::SQLValue('taxonomy');
                if (is_numeric($id_update)) {
                    $whereArray = array('object_id' => MySQL::SQLValue($id_update, MySQL::SQLVALUE_NUMBER), 'object_type' => MySQL::SQLValue('taxonomy'), 'name' => MySQL::SQLValue($post_key));
                    $hmdb->AutoInsertUpdate($tableName, $values, $whereArray);
                } else {
                    $hmdb->InsertRow($tableName, $values);
                }
                unset($values);
            }
        }
        /** show table */
        $status = hm_get('status', 'public');
        $perpage = hm_get('perpage', '30');
        $return_array = taxonomy_show_data($key, $status, $perpage, false);
        $return_array['latest'] = $latest_array;
        return json_encode($return_array, true);
    }
}