function getField() { $value = $this->getParam('value'); if (!$value) { return ' - '; } $key_field = $this->getParam('key_field'); $values_field = $this->getParam('values_field'); $lookup_table = $this->getParam('lookup_table'); $external_link = $this->getParam('external_link'); $db = DBMysql::getInstance(); $query = " SELECT `{$values_field}` FROM `{$lookup_table}` "; $query .= " WHERE `{$key_field}` = '{$value}' "; $db->setQuery($query); $field = $db->getResult(); if ($field === false) { $this->errors[] = $db->getError(); return false; } if ($external_link) { $external_link = str_replace('*', $value, $external_link); $external_link = $this->getURLString($external_link); $field = '<a href="' . $external_link . '">' . $field . '</a>' . "\n"; } return $field; }
/** * 得到实例 * * @access public * @return mixed */ public static function getInstance() { $dbinfo = self::getDbInfo(); switch ($dbinfo['type']) { default: return DBMysql::getInstance($dbinfo); } }
function loadItems() { // if ($this->items) { // return true; // } $this->items = array(); $value = $this->getParam('value'); $add_no_answer = $this->getParam('add_no_answer'); $mode = $this->getParam('mode'); if ($mode != 'list' && $add_no_answer || $mode == 'list' && !$value) { $this->items[0] = ' - '; } $lookup_table = $this->getParam('lookup_table'); $key_field = $this->getParam('key_field'); $values_field = $this->getParam('values_field'); $filters = $this->getParam('filters'); if (!$lookup_table || !$key_field || !$values_field) { $name = $this->getParam('name'); $this->errors[] = "Some lookup table parameters not set. Field '{$name}' "; return false; } $db = DBMysql::getInstance(); $query = " SELECT `{$key_field}`, `{$values_field}` FROM `{$lookup_table}` "; $where_params = array(); if ($mode == 'list' && $value) { $where_params[] = " `{$key_field}` = '{$value}' "; } if ($filters && is_array($filters)) { foreach ($filters as $filter) { $where_params[] = " `{$filter['field']}` = '{$filter['value']}' "; } } if ($where_params) { $query .= " WHERE " . implode(' AND ', $where_params); } $db->setQuery($query); $result = $db->getArrays(); if ($result === false) { $this->errors[] = $db->getError(); return false; } if (!$result) { $this->items[0] = ' - '; } else { foreach ($result as $value) { $this->items[$value[$key_field]] = $value[$values_field]; } } return true; }
function getValue() { $value = (int) $this->getParam('value'); $name = $this->getParam('name'); $table = $this->getParam('table'); if (!$table) { return 1; } if (!$value) { $db = DBMysql::getInstance(); $query = "SELECT MAX(`{$name}`) + 1 FROM `{$table}` "; $db->setQuery($query); $value = $db->getResult(); if (!$value) { return 1; } } return $value; }
function beforeDelete() { $check_foreign_key = $this->getParam('check_foreign_key'); $key_field_value = $this->getParam('key_field_value'); // $many_to_many_table = $this->getParam('many_to_many_table'); if (!$check_foreign_key) { return true; } $db = DBMysql::getInstance(); foreach ($check_foreign_key as $foreign_key) { if (empty($foreign_key['is_allow_delete'])) { continue; } $query = "\n\t\t\t\tDELETE FROM `{$foreign_key['table']}`\n\t\t\t\tWHERE `{$foreign_key['field']}` = '{$key_field_value}'\n\t\t\t"; $db->setQuery($query); if (!$db->query()) { $this->errors[] = $db->getError(); return false; } } return true; }
function afterSave() { // var_dump($this);exit; $value = $this->getParam('value'); $table = $this->getParam('db_table'); $file_size_field = $this->getParam('file_size_field'); $is_size_in_human_readable_format = $this->getParam('is_size_in_human_readable_format'); $upload_path = $this->getParam('upload_path'); $key_field_name = $this->getParam('key_field'); $key_field_value = $this->getParam('key_field_value'); if (!$file_size_field || $value === false) { return true; } $fileSize = ''; if ($value !== '') { $fileSize = filesize($upload_path . $value); if ($is_size_in_human_readable_format) { switch ($fileSize) { case $fileSize < 1024: $fileSize = $fileSize . 'B'; break; case $fileSize >= 1024 && $fileSize < 1048576: $fileSize = round($fileSize / 1024) . 'KB'; break; case $fileSize >= 1048576 && $fileSize < 1073741824: $fileSize = round($fileSize / 1048576) . 'MB'; } } } $db = DBMysql::getInstance(); $query = "\n\t\t\tUPDATE `{$table}`\n\t\t\tSET `{$file_size_field}` = '{$fileSize}'\n\t\t\tWHERE `{$key_field_name}` = '{$key_field_value}'\n\t\t"; // var_dump($query);exit; $db->setQuery($query); if (!$db->query()) { $this->errors[] = $db->getError(); return false; } return true; }
function getRowsCount() { $db = DBMysql::getInstance(); $query = "\n\t\t\tSELECT COUNT(*)\n\t\t\tFROM `{$this->table}`\n\t\t"; if (isset($this->join_table['name'])) { $query .= "\n\t\t\t\tINNER JOIN `{$this->join_table['name']}` ON\n\t\t\t\t\t`{$this->join_table['name']}`.`{$this->join_table['join_field']}`\n\t\t\t\t\t\t= `{$this->table}`.`{$this->key_field}`\n\t\t\t"; } if ($this->where_conditions) { $query .= ' WHERE ' . $this->getWhereStr(); } $db->setQuery($query); $result = $db->getResult(); if ($result === false) { $this->errors[] = $db->getError(); return false; } return $result; }
function save($data) { $this->setMode('save'); if (!$this->loadFields()) { return false; } $update_fields_params = array(); foreach ($this->fields as $field) { if ($field['type'] != 'key') { $field = array_merge($field, $this->fields_params); $update_fields_params[] = $field; } } $renderer = new DMRenderer(); $renderer->setFields($update_fields_params); $renderer->setData($data); $renderer->setFieldsPrefix($this->fields_prefix); $mode = $this->getMode(); $aditional_params = array('key_field' => $this->key_field, 'mode' => $mode); $renderer->addParams($aditional_params); $data = $renderer->getFieldsData(); if ($data === false) { $this->errors = $renderer->getErrors(); return false; } $this->key_value = isset($data[$this->key_field]) ? $data[$this->key_field] : 0; if ($this->key_field && $this->key_value) { $this->addWhereCondition(" `{$this->key_field}` = '{$this->key_value}' "); $result = parent::update($data, $this->table); if (!$result) { return $result; } $renderer->triggerEvent('afterSave', $this->key_value, $this->table); if (!empty($this->join_table['name'])) { $this->where_conditions = array(); $join_table_key_field_value = $data[$this->join_table['key_field']]; $this->addWhereCondition(" `{$this->join_table['key_field']}` = '{$join_table_key_field_value}' "); $result = parent::update($data, $this->join_table['name']); $renderer->triggerEvent('afterSave', $this->key_value, $this->table); } } else { $result = parent::insert($data, $this->table); $db = DBMysql::getInstance(); $this->key_value = $db->insertid(); if (!$result) { return $result; } $renderer->triggerEvent('afterSave', $this->key_value, $this->table); if (!empty($this->join_table['name'])) { $this->table_fields[] = array('table' => $this->join_table['name'], 'name' => $this->join_table['join_field']); $data[$this->join_table['join_field']] = $this->key_value; $this->key_field = $this->join_table['key_field']; $result = parent::insert($data, $this->join_table['name']); $join_table_key_field_value = $db->insertid(); $renderer->triggerEvent('afterSave', $join_table_key_field_value, $this->join_table['name']); } } if (!$result) { return $result; } return $result; }
function afterSave() { $name = $this->getParam('name'); $key_field_value = $this->getParam('key_field_value'); $many_to_many_table = $this->getParam('many_to_many_table'); $link_field = $this->getParam('link_field'); $lookup_field = $this->getParam('lookup_field'); $many_to_many_table_filters = $this->getParam('many_to_many_table_filters'); $db = DBMysql::getInstance(); /// deleting $query = "\n\t\t\tDELETE FROM `{$many_to_many_table}`\n\t\t\tWHERE `{$link_field}` = '{$key_field_value}'\n\t\t"; if ($many_to_many_table_filters && is_array($many_to_many_table_filters)) { foreach ($many_to_many_table_filters as $field => $value) { $query .= " AND `{$field}` = '{$value}' "; } } $db->setQuery($query); if (!$db->query()) { $this->errors[] = $db->getError(); return false; } // inserting if (!isset($_POST[$name])) { return true; } $fields = array($link_field, $lookup_field); if ($many_to_many_table_filters && is_array($many_to_many_table_filters)) { foreach ($many_to_many_table_filters as $field => $value) { $fields[] = $field; } } $insert_params = array(); foreach ($_POST[$name] as $lookup_value) { $values = array($key_field_value, $lookup_value); if ($many_to_many_table_filters && is_array($many_to_many_table_filters)) { foreach ($many_to_many_table_filters as $filter_value) { $values[] = $filter_value; } } $insert_params[] = "('" . implode("', '", $values) . "')"; } $insert_str = implode(', ', $insert_params); $query = "\n\t\t\tINSERT INTO `{$many_to_many_table}`\n\t\t\t( `" . implode('`, `', $fields) . "` )\n\t\t\tVALUES {$insert_str}\n\t\t"; $db->setQuery($query); if (!$db->query()) { $this->errors[] = $db->getError(); return false; } return true; }