?? customimport.php
字號:
* Get list of Custom Import objects in the database * @return Array Array of CustomImport objects */ public static function getCustomImportList() { return self::_getList(); } /** * Get list of defined Custom Imports in format suitable for view.php * @return Array 2D array representing custom import objects defined in database. */ public static function getCustomImportListForView($pageNO,$schStr,$mode,$sortField = 0, $sortOrder = 'ASC') { $imports = CustomImport::getCustomImportList(); $arrDispArr = null; for($i=0; count($imports) > $i; $i++) { $arrDispArr[$i][0] = $imports[$i]->getId(); $arrDispArr[$i][1] = $imports[$i]->getName(); } return $arrDispArr; } /** * Get the available fields (fields not yet assigned to this CustomImport) * * @return array Array of fields not yet assigned to this CustomImport object */ public function getAvailableFields() { $allFields = CustomImport::getAllFields(); $available = array_diff($allFields, $this->assignedFields); return $available; } /** * Delete custom imports with the given ids * * @param array $ids Array of import id's * @return int the number of CustomImport's actually deleted */ public static function deleteImports($ids) { $count = 0; if (!is_array($ids)) { throw new CustomImportException("Invalid parameter to deleteImports(): ids should be an array", CustomImportException::INVALID_PARAMETERS); } foreach ($ids as $id) { if (!CommonFunctions::isValidId($id)) { throw new CustomImportException("Invalid parameter to deleteImports(): id = $id", CustomImportException::INVALID_PARAMETERS); } } if (!empty($ids)) { $sql = sprintf("DELETE FROM %s WHERE `%s` IN (%s)", self::TABLE_NAME, self::DB_FIELDS_ID, implode(",", $ids)); $conn = new DMLFunctions(); $result = $conn->executeQuery($sql); if ($result) { $count = mysql_affected_rows(); } } return $count; } /** * Save this CustomImport Object. * If an id is available the existing values are updated, if not a new * id is assigned and a new CustomImport is saved * */ public function save() { // Validate fieleds if (empty($this->name)) { throw new CustomImportException("Empty name", CustomImportException::EMPTY_IMPORT_NAME); } if ($this->_isNameInUse()) { throw new CustomImportException("Duplicate name", CustomImportException::DUPLICATE_IMPORT_NAME); } if (empty($this->assignedFields) || !is_array($this->assignedFields)) { throw new CustomImportException("No valid Assigned fields", CustomImportException::NO_ASSIGNED_FIELDS); } $compulsaryFields = self::getCompulsaryFields(); $allFields = self::getAllFields(); foreach ($this->assignedFields as $field) { if (!in_array($field, $allFields)) { throw new CustomImportException("Invalid field name", CustomImportException::INVALID_FIELD_NAME); } $key = array_search($field, $compulsaryFields); if ($key !== FALSE) { unset($compulsaryFields[$key]); } } if (count($compulsaryFields) > 0) { throw new CustomImportException("Missing compulsary fields: " . implode(',', $compulsaryFields), CustomImportException::COMPULSARY_FIELDS_NOT_ASSIGNED); } if (empty($this->id)) { $this->_insert(); } else { $this->_update(); } } /** * Add new CustomImport object to database */ private function _insert() { $fields[0] = self::DB_FIELDS_ID; $fields[1] = self::DB_FIELDS_NAME; $fields[2] = self::DB_FIELDS_FIELDS; $fields[3] = self::DB_FIELDS_HAS_HEADING; $this->id = UniqueIDGenerator::getInstance()->getNextID(self::TABLE_NAME, self::DB_FIELDS_ID); $values[0] = $this->id; $values[1] = "'{$this->name}'"; $values[2] = "'" . implode(",", $this->assignedFields) . "'"; $values[3] = $this->containsHeader ? self::HAS_HEADING : self::NO_HEADING; $sqlBuilder = new SQLQBuilder(); $sqlBuilder->table_name = self::TABLE_NAME; $sqlBuilder->flg_insert = 'true'; $sqlBuilder->arr_insert = $values; $sqlBuilder->arr_insertfield = $fields; $sql = $sqlBuilder->addNewRecordFeature2(); $conn = new DMLFunctions(); $result = $conn->executeQuery($sql); if (!$result || (mysql_affected_rows() != 1)) { throw new CustomImportException("Insert failed. $sql", CustomImportException::DB_EXCEPTION); } } /** * Update existing CustomImport data */ private function _update() { $fields[0] = self::DB_FIELDS_ID; $fields[1] = self::DB_FIELDS_NAME; $fields[2] = self::DB_FIELDS_FIELDS; $fields[3] = self::DB_FIELDS_HAS_HEADING; $values[0] = $this->id; $values[1] = "'{$this->name}'"; $values[2] = "'" . implode(",", $this->assignedFields) . "'"; $values[3] = $this->containsHeader ? self::HAS_HEADING : self::NO_HEADING; $sqlBuilder = new SQLQBuilder(); $sqlBuilder->table_name = self::TABLE_NAME; $sqlBuilder->flg_update = 'true'; $sqlBuilder->arr_update = $fields; $sqlBuilder->arr_updateRecList = $values; $sql = $sqlBuilder->addUpdateRecord1(0); $conn = new DMLFunctions(); $result = $conn->executeQuery($sql); // Here we don't check mysql_affected_rows because update may be called // without any changes. if (!$result) { throw new CustomImportException("Update failed. SQL=$sql", CustomImportException::DB_EXCEPTION); } } /** * Check if this objects name is in use * * @return boolean true if that name is in use, false otherwise */ private function _isNameInUse() { $sql = 'SELECT COUNT(*) FROM ' . self::TABLE_NAME . ' WHERE ' . self::DB_FIELDS_NAME . " = '" . $this->name . "'"; // exclude this object if (!empty($this->id)) { $sql .= ' AND ' . self::DB_FIELDS_ID . ' <> ' . $this->id; } $result = mysql_query($sql); $row = mysql_fetch_array($result, MYSQL_NUM); $count = $row[0]; return ($count != 0); } /** * Get a list of custom import objects with the given conditions. * * @param array $selectCondition Array of select conditions to use. * @return array Array of CustomImport objects. Returns an empty (length zero) array if none found. */ private static function _getList($selectCondition = null) { $fields[0] = self::DB_FIELDS_ID; $fields[1] = self::DB_FIELDS_NAME; $fields[2] = self::DB_FIELDS_FIELDS; $fields[3] = self::DB_FIELDS_HAS_HEADING; $sqlBuilder = new SQLQBuilder(); $sql = $sqlBuilder->simpleSelect(self::TABLE_NAME, $fields, $selectCondition); $actList = array(); $conn = new DMLFunctions(); $result = $conn->executeQuery($sql); while ($result && ($row = mysql_fetch_assoc($result))) { $actList[] = self::_createFromRow($row); } return $actList; } /** * Creates a CustomImport object from a resultset row * * @param array $row Resultset row from the database. * @return CustomImport Custom Import object. */ private static function _createFromRow($row) { $tmp = new CustomImport(); $tmp->setId($row[self::DB_FIELDS_ID]); $tmp->setName($row[self::DB_FIELDS_NAME]); $assignedFields = $row[self::DB_FIELDS_FIELDS]; if (!empty($assignedFields)) { $tmp->setAssignedFields(explode(",", $assignedFields)); } else { $tmp->setAssignedFields(array()); } $hasHeader = ($row[self::DB_FIELDS_HAS_HEADING] == self::HAS_HEADING) ? true : false; $tmp->setContainsHeader($hasHeader); return $tmp; }}class CustomImportException extends Exception { const INVALID_FIELD_NAME = 0; const NO_ASSIGNED_FIELDS = 1; const DUPLICATE_IMPORT_NAME = 2; const EMPTY_IMPORT_NAME = 3; const DB_EXCEPTION = 4; const INVALID_PARAMETERS = 5; const ID_NOT_FOUND = 6; const COMPULSARY_FIELDS_NOT_ASSIGNED = 7;}?>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -