| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- /**
- * Columns data in $_SESSION['columns']
- *
- * Data structure:
- * $_SESSION['columns'][$TableID] = array(
- * [ID] => array(
- * [type] => int(10)
- * [null] => bool
- * [default] =>
- * )
- * );
- *
- *
- */
- class ColumnCrm {
- private $_table;
- private $_data;
- private $_acl = null;
- private $_initialized;
- public function __construct($table, $colData) {
- $this->_table = $table;
- $this->_data = $colData;
- $this->_initialized = (V::get('__checked', '', $colData))? true : false;
- }
- /**
- * Initialize column and store data i session.
- *
- */
- public static function init($tableID, $tblAcl, $force = false) {
- $col = self::getInstance($tableID);
- if ($col && $col->initialized() && $force == false) {
- return;
- }
- $colData = self::fetchTableStruct($tblAcl->getName(), $tblAcl->getDB());
- $colData['__checked'] = true;
- self::saveColumn($tableID, $colData);
- }
- public function setAcl($acl) {
- $this->_acl = $acl;
- foreach ($this->_acl->getFields() as $fieldID => $vField) {
- if (array_key_exists($vField['name'], $this->_data)) {
- $vField['type'] = $this->_data[$vField['name']];
- } else {
- $this->_acl->removeField($fieldID);
- }
- }
- }
- public function getAcl() {
- return $this->_acl;
- }
- public function getTableID() {
- return $this->_table;
- }
- public function getTableName() {
- if ($this->_acl) {
- return $this->_acl->getName();
- }
- return null;
- }
- public function getDB() {
- if ($this->_acl) {
- return $this->_acl->getDB();
- }
- return null;
- }
- public function initialized() {
- return $this->_initialized;
- }
- public function type($fieldName) {
- $type = V::get($fieldName, '', $this->_data['TYPE']);
- return $type;
- }
- public static function fetchTableStruct($tblName, $dbID) {
- $struct = array();
- $db = DB::getDB($dbID);
- $res = $db->query("show fields from `$tblName` ");
- while ($h = $db->fetch_row($res)) {
- $fieldName = $h[0];
- $fieldType = $h[1];
- $struct[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
- }
- return $struct;
- }
- public static function fetchHistTableStruct($tblName, $dbID) {
- $col = array();
- $tblHistName = "{$tblName}_HIST";
- $db = DB::getDB($dbID);
- $res = $db->query("show fields from `$tblHistName` ");
- while ($h = $db->fetch_row($res)) {
- $fieldName = $h[0];
- $fieldType = $h[1];
- $col[$fieldName] = $fieldType;
- }
- return $col;
- }
- public function getVisibleFieldList() {
- $cols = array();
- foreach ($this->_data as $fieldName => $fieldType) {
- if ($fieldName == '__checked') {
- continue;
- }
- $cols[] = $fieldName;
- }
- return $cols;
- }
- public function getFieldList() {
- $cols = array();
- foreach ($this->_data['ID'] as $field_id) {
- $field_name = $this->_data['DESC'][$field_id];
- $cols[$field_id] = $field_name;
- }
- return $cols;
- }
- /**
- * Get column object. Not initialize
- * @returns object - column instance if exists else null
- *
- * static
- */
- public static function getInstance($tableID) {
- static $_cache;
- if (!$_cache) $_cache = array();
- if (array_key_exists($tableID, $_cache)) {
- return $_cache[$tableID];
- }
- if (!empty($_SESSION['columns'][$tableID])) {
- $col = new ColumnCrm($tableID, $_SESSION['columns'][$tableID]);
- if ($col->initialized()) {
- $_cache[$tableID] = $col;
- return $_cache[$tableID];
- }
- }
- return null;
- }
- /**
- * static
- */
- public static function saveColumn($tableID, $data) {
- $_SESSION['columns'][$tableID] = $data;
- }
- }
|