ColumnCrm.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * Columns data in $_SESSION['columns']
  4. *
  5. * Data structure:
  6. * $_SESSION['columns'][$TableID] = array(
  7. * [ID] => array(
  8. * [type] => int(10)
  9. * [null] => bool
  10. * [default] =>
  11. * )
  12. * );
  13. *
  14. *
  15. */
  16. class ColumnCrm {
  17. private $_table;
  18. private $_data;
  19. private $_acl = null;
  20. private $_initialized;
  21. public function __construct($table, $colData) {
  22. $this->_table = $table;
  23. $this->_data = $colData;
  24. $this->_initialized = (V::get('__checked', '', $colData))? true : false;
  25. }
  26. /**
  27. * Initialize column and store data i session.
  28. *
  29. */
  30. public static function init($tableID, $tblAcl, $force = false) {
  31. $col = self::getInstance($tableID);
  32. if ($col && $col->initialized() && $force == false) {
  33. return;
  34. }
  35. $colData = self::fetchTableStruct($tblAcl->getName(), $tblAcl->getDB());
  36. $colData['__checked'] = true;
  37. self::saveColumn($tableID, $colData);
  38. }
  39. public function setAcl($acl) {
  40. $this->_acl = $acl;
  41. foreach ($this->_acl->getFields() as $fieldID => $vField) {
  42. if (array_key_exists($vField['name'], $this->_data)) {
  43. $vField['type'] = $this->_data[$vField['name']];
  44. } else {
  45. $this->_acl->removeField($fieldID);
  46. }
  47. }
  48. }
  49. public function getAcl() {
  50. return $this->_acl;
  51. }
  52. public function getTableID() {
  53. return $this->_table;
  54. }
  55. public function getTableName() {
  56. if ($this->_acl) {
  57. return $this->_acl->getName();
  58. }
  59. return null;
  60. }
  61. public function getDB() {
  62. if ($this->_acl) {
  63. return $this->_acl->getDB();
  64. }
  65. return null;
  66. }
  67. public function initialized() {
  68. return $this->_initialized;
  69. }
  70. public function type($fieldName) {
  71. $type = V::get($fieldName, '', $this->_data['TYPE']);
  72. return $type;
  73. }
  74. public static function fetchTableStruct($tblName, $dbID) {
  75. $struct = array();
  76. $db = DB::getDB($dbID);
  77. $res = $db->query("show fields from `$tblName` ");
  78. while ($h = $db->fetch_row($res)) {
  79. $fieldName = $h[0];
  80. $fieldType = $h[1];
  81. $struct[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
  82. }
  83. return $struct;
  84. }
  85. public static function fetchHistTableStruct($tblName, $dbID) {
  86. $col = array();
  87. $tblHistName = "{$tblName}_HIST";
  88. $db = DB::getDB($dbID);
  89. $res = $db->query("show fields from `$tblHistName` ");
  90. while ($h = $db->fetch_row($res)) {
  91. $fieldName = $h[0];
  92. $fieldType = $h[1];
  93. $col[$fieldName] = $fieldType;
  94. }
  95. return $col;
  96. }
  97. public function getVisibleFieldList() {
  98. $cols = array();
  99. foreach ($this->_data as $fieldName => $fieldType) {
  100. if ($fieldName == '__checked') {
  101. continue;
  102. }
  103. $cols[] = $fieldName;
  104. }
  105. return $cols;
  106. }
  107. public function getFieldList() {
  108. $cols = array();
  109. foreach ($this->_data['ID'] as $field_id) {
  110. $field_name = $this->_data['DESC'][$field_id];
  111. $cols[$field_id] = $field_name;
  112. }
  113. return $cols;
  114. }
  115. /**
  116. * Get column object. Not initialize
  117. * @returns object - column instance if exists else null
  118. *
  119. * static
  120. */
  121. public static function getInstance($tableID) {
  122. static $_cache;
  123. if (!$_cache) $_cache = array();
  124. if (array_key_exists($tableID, $_cache)) {
  125. return $_cache[$tableID];
  126. }
  127. if (!empty($_SESSION['columns'][$tableID])) {
  128. $col = new ColumnCrm($tableID, $_SESSION['columns'][$tableID]);
  129. if ($col->initialized()) {
  130. $_cache[$tableID] = $col;
  131. return $_cache[$tableID];
  132. }
  133. }
  134. return null;
  135. }
  136. /**
  137. * static
  138. */
  139. public static function saveColumn($tableID, $data) {
  140. $_SESSION['columns'][$tableID] = $data;
  141. }
  142. }