📄 data.php
字号:
<?php//##############################################################################// File: $Source: /home/cvs/osdate/libs/modOsDate/data.php,v $//// RCS: $Header: /home/cvs/osdate/libs/modOsDate/data.php,v 1.7 2006/07/22 16:41:39 cvs Exp $//// Modified: $Date: 2006/07/22 16:41:39 $//// By: $Locker: $//////##############################################################################include_once(MODOSDATE_DIR."db_class.php");include_once(LIB_DIR . "validation_class.php");class Data { var $db; // Database Object Reference var $_tableName; // Name of Table var $_dbName; // Name of database var $message = array(); // Last Message var $_error = FALSE; var $_data; // Array that holds the row data var $valid; // Validation Class var $id; // Service id number used on current query var $fields; var $_idField; var $form = FALSE; // Output data compatable with a form var $config; var $rows; var $request_vars; var $_eof = true; var $_nextPage; // The next page to display var $_ignoreField = array(); // An array of fields to ignore when adding // a row var $_deleteRows; // The number of rows that was available to delete var $_EmptySetDeleteOk = false; // Indicates if an error should be reported if there are no rec found to delete var $sort_process; // The name of the process for the list page to sort var $_DataRows; // Array of data from a row mode add or edit; var $_insertId = false; // this insert id for the last insert var $_table_prefix; // Prefix to database table # Constructor for Option Name object // Call with a config array as described above, or with a table name // function Data($table) { if ( is_array($table) ) { $this->config = $table; $this->db = new myDb(); $this->setTableName($this->config['table']); $this->setIdField($this->config['idField']); } else { $this->setTableName($table); $this->db = new myDb(); $this->setIdField('id'); $this->makeConfig(); } $this->fields = $this->config['fields']; // Initilize Validation // $this->valid = new Validation($this->db); $this->valid->db_table = $this->getDbTableName(); } // Indicates if an error should be reported if there are no rec found to delete // function setEmptySetDeleteOk() { $this->_EmptySetDeleteOk = true; } function clrEmptySetDeleteOk() { $this->_EmptySetDeleteOk = false; } function getEmptySetDeleteOk() { return $this->_EmptySetDeleteOk; } function getIdField() { return $this->_idField; } function setIdField($field) { $this->_idField = $field; $this->valid->id_field = $field; } function getInsertId() { return $this->_insertId; } // Works // // Example: // $data['name'] = 'isdn_address'; // $data['description'] = "ISDN Address"; // $data['amount'] = 5.95; // $data = $serv->add_service($data); // if ( $serv->error ) { // // print "Error: $serv->message\n"; // } // function insertQuery($data,$print = 0) { $this->setEof(); $result = $this->db->insert_query($data, $this->getDbTableName(), $print); if ( $this->getIdField() != "" ) { $this->_insertId = $this->db->get_insert_id($this->getIdField()); } if ( ! $result ) { $this->setErrorMessage( $this->getAddErrorMsgconfig() ); } return $result; } function addRec($data, $opt = false, $print = 0) { $this->setEof(); if ( ! is_array($opt) || ! array_key_exists('novalidate',$opt) ) { $this->validateData($data); } if ( ! $this->valid->error ) { if ( ! is_array($opt) || ! array_key_exists('nosave',$opt) ) { $this->setData( $this->valid->data_out ); $this->insertQuery( $this->getData() , $print); } } else { $this->setErrorMessage($this->valid->get_error_message() ); $this->setData( $data ); } return $this->getData(); } // Sets the error flag and adds a message // function setErrorMessage($message) { $this->setError(); $this->setMessage($message); } // Sets error flag and sets failur page info // function isError() { return $this->_error; } function getError() { return $this->_error; } // Sets error flag and sets failur page info // function setError($error = true) { if ( $error ) { $this->_error = true; } else { $this->clrError(); } } // Clears error flag and sets success page info // function clrError() { $this->_error = false; } function editRec($data, $opt = false,$print = 0) { $data = $this->_editRec($data,$opt, $print); return $data; } function _editRec($data, $opt = false,$print = 0) { if ( is_array($opt) ) { $key_field = ''; if ( array_key_exists('key_field',$opt) ) { $key_field = $opt['key_field']; $this->valid->key_field($key_field); } } elseif ( is_string($opt) ) { $key_field = $opt; $this->valid->key_field($key_field); $opt = array(); } else { $key_field = false; $this->valid->key_field($this->getIdField()); $opt = array(); } $this->setEof(); $this->setData( $data ); // Don't validate if this is set // if ( ! array_key_exists('novalidate',$opt) ) { $this->validateData($data,"update"); } if ( ! $this->valid->error ) { if ( ! array_key_exists('nosave',$opt) ) { $save_data = $this->valid->data_out; // Get the key to use for updating // if ( $key_field != "" ) { $key[$key_field] = $data[$key_field]; } else { $key[$this->getIdField()] = $data[$this->getIdField()]; } $result = $this->updateQuery($save_data, $key, $print); if ( $result ) { // just to keep php from generating a warning about this missing key if ( ! array_key_exists($this->getIdField(), $data) ) { $data[$this->getIdField()] = ''; } $this->setMessage( $this->getIdMessage( $this->getEditMsg(), $data[ $this->getIdField() ] ) ); } else { $this->setErrorMessage($this->getEditErrorMsg() ); } } } else { $this->setErrorMessage( $this->valid->get_error_array() ); $this->setData( $data ); } return $this->getData(); } function formatArray($data) { $data_out = array(); foreach ( $data AS $key => $value ) { foreach ( $value AS $k => $v ) { $data_out[$k][$key] = $v; } } return $data_out; } function addRecRows($data, $opt = false,$print = 0) { // Get options if ( is_array($opt) ) { if ( array_key_exists('key_field',$opt) ) { $key_field = $opt['key_field']; } } else { $opt = array(); } $this->setEof(); $error = false; $message = ""; $all_blank = true; // Validate all records // foreach( $data as $key => $record ) { if ( array_key_exists('empty_lines',$opt) && $this->allBlank($record) ) { // Remove the line so it's not saved // unset($data[$key]); } else { $this->validateData($record); // If there's one error, it's all an error // if ( $this->valid->error ) { $error = true; $message .= "Line " . $key . ": " . $this->valid->error_message . "<br>"; } $all_blank = false; } } $this->setData( $data ); if ( ! $error) { if ( ! array_key_exists('nosave',$opt) ) { foreach( $data as $record ) { $save_data = $record; $this->insertQuery($save_data, $print); $this->_DataRows[$this->_insertId] = $save_data; // If there's one error, it's all an error // if ( $this->isError() ) { $error = true; $message .= $this->Message() . "<br>"; } } } $this->setMessage($message); $this->setError($error); } else { $this->setErrorMessage($message); foreach ( $data AS $id => $rec ) { $data[$id] = $rec; } $this->setData( $data ); } if ( $all_blank ) { $this->setErrorMessage($this->getBlankErrorMsg() ); } return $this->getData(); } // An overridable method to do something on the data before it is validates and adds. // For example set default data // function preRowAdd($data) { return $data; } function setMessage($message) { if ( is_array($message) ) { $this->message = array_merge($this->message, $message); } else { $this->message[$message] = $message; } } function clrMessage() { //$this->message = ""; } function Message() { return join('<br>', $this->message); } // Adds additional items that if found in field is considered duplicate // function addDuplicate($field,$value) { $this->valid->add_duplicate($field,$value); } // Adds sets of fields that must be unique. Accepts a field name or an array of field names // function addDuplicateSet($field) { $this->valid->add_duplicate_field_set($field); } function getMessage() { return $this->Message(); } // Overridable method to do something before saving the rows // function preEditRecRows($data, $opt = false, $print = 0) { return $data; } // editRec_rows($data,$key_field) // // or // // editRec_rows($data,$opt_array) // // $opt_array = array( // empty_lines => true, // Allow empty data lines // nosave => true, // Don't save data to db // common_key => array('order_id' => 5), // apply to all records // ) // function editRecRows($data, $opt = false, $print = 0) { if ( is_array($opt) ) { if ( array_key_exists('key_field',$opt) ) { $key_field = $opt['key_field']; } } else { $key_field = ''; $opt = array(); } $this->setEof(); $error = false; $message = ""; $all_blank = true; // Validate all records // foreach( $data as $key => $record ) { if ( array_key_exists('empty_lines',$opt) && $this->all_blank($record) ) { // Remove the line so it's not saved // unset($data[$key]); } else { $this->validateData($record, "update"); $save_data[$key] = $this->valid->data_out;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -