PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/library/com/mokoala/Module/RecordModule.class.php

https://bitbucket.org/rkandpal/nustechgmgitfork
PHP | 346 lines | 251 code | 92 blank | 3 comment | 32 complexity | 5d228c8f7d7f7c93981b74bb15f116b8 MD5 | raw file
  1. <?php
  2. class MK_RecordModule extends MK_Record{
  3. protected $fields = array();
  4. protected $deleted = false;
  5. public function __construct( $id = null )
  6. {
  7. if( !empty($id) )
  8. {
  9. $this
  10. ->setId($id)
  11. ->buildFieldList();
  12. }
  13. else
  14. {
  15. $this->meta = $this->getModule()->getFieldList();
  16. }
  17. }
  18. protected function buildFieldList()
  19. {
  20. $config = MK_Config::getInstance();
  21. $get_module = mysql_query("SELECT * FROM `modules` WHERE `id` = '".mysql_real_escape_string($this->getId(), $config->db->con)."'", $config->db->con);
  22. $this->meta = mysql_fetch_assoc($get_module);
  23. mysql_free_result($get_module);
  24. $actual_field_module = 'fields';
  25. if($this->getSlug() === $actual_field_module){
  26. $field_module = $this;
  27. }else{
  28. $field_module = MK_RecordModuleManager::getFromSlug($actual_field_module);
  29. }
  30. $get_fields = mysql_query("SELECT * FROM `modules_fields` WHERE `module_id` = '".mysql_real_escape_string($this->getId(), $config->db->con)."' ORDER BY `order` ASC", $config->db->con);
  31. while($res_fields = mysql_fetch_assoc($get_fields)){
  32. $this->fields[$res_fields['name']] = $res_fields['id'];
  33. }
  34. mysql_free_result($get_fields);
  35. return $this;
  36. }
  37. public function _getRecords(&$records, MK_Paginator &$paginator = null, $options = array(), $parent = 0, $level = 0){
  38. $sql_where = array();
  39. $sql_orderby = array();
  40. $config = MK_Config::getInstance();
  41. $field_module = MK_RecordModuleManager::getFromType('module_field');
  42. if($this->getFieldParent()){
  43. $field = MK_RecordManager::getFromId($field_module->getId(), $this->getFieldParent());
  44. $sql_where[] = "`".$field->getName()."` = '".mysql_real_escape_string($parent, $config->db->con)."'";
  45. }
  46. if( !empty($options['orderby']) ){
  47. $sql_orderby[] = "`".$options['orderby']['field']."` ".$options['orderby']['direction']."";
  48. }
  49. $get_records = mysql_query("SELECT * FROM `".$this->getTable()."`".(count($sql_where) > 0 ? " WHERE ".implode(" AND ", $sql_where) : '').(count($sql_orderby) > 0 ? " ORDER BY ".implode(", ", $sql_orderby) : ''), $config->db->con);
  50. if(mysql_num_rows($get_records) > 0){
  51. while($res_records = mysql_fetch_assoc($get_records)){
  52. if($paginator && $paginator->getTotalRecords() == count($records)){
  53. break;
  54. }
  55. $new_field = MK_RecordManager::getFromId($this->getId(), $res_records['id']);
  56. $new_field->setNestedLevel($level);
  57. $records[] = $new_field;
  58. if($this->getFieldParent()){
  59. $this->_getRecords($records, $paginator, $options, $new_field->getId(), $level + 1);
  60. }
  61. }
  62. mysql_free_result($get_records);
  63. }
  64. }
  65. public function getRecords(MK_Paginator &$paginator = null, $options = array()){
  66. $config = MK_Config::getInstance();
  67. $records = array();
  68. if( $this->deleted === true )
  69. {
  70. return $records;
  71. }
  72. $field_module = MK_RecordModuleManager::getFromType('module_field');
  73. if( !empty($options['orderby']) ){
  74. $field = MK_RecordManager::getFromId($field_module->getId(), $options['orderby']['field']);
  75. $orderby_field = $field->getName();
  76. $orderby_direction = $options['orderby']['direction'];
  77. }elseif( $this->getFieldOrderby() && $this->getOrderbyDirection() ){
  78. $field = MK_RecordManager::getFromId($field_module->getId(), $this->getFieldOrderby());
  79. $orderby_field = $field->getName();
  80. $orderby_direction = $this->getOrderbyDirection();
  81. }
  82. if( isset($orderby_field, $orderby_direction) ){
  83. $options = array(
  84. 'orderby' => array(
  85. 'field' => $orderby_field,
  86. 'direction' => $orderby_direction
  87. )
  88. );
  89. }
  90. if($paginator){
  91. $get_total_records = mysql_query("SELECT COUNT(`id`) as 'total' FROM `".$this->getTable()."`", $config->db->con);
  92. $res_total_records = mysql_fetch_assoc($get_total_records);
  93. $paginator->setTotalRecords($res_total_records['total']);
  94. mysql_free_result($get_total_records);
  95. }
  96. $this->_getRecords($records, $paginator, $options);
  97. if($paginator){
  98. $records = array_slice( $records, $paginator->getRecordStart(), $paginator->getPerPage() );
  99. }
  100. return $records;
  101. }
  102. public function searchRecords($search_criteria, $paginator = null, $options = array()){
  103. $config = MK_Config::getInstance();
  104. $sql_parts = array();
  105. $search_results = array();
  106. $field_module = MK_RecordModuleManager::getFromType('module_field');
  107. // Search criteria
  108. foreach($search_criteria as $criteria){
  109. if( empty($criteria['literal']) ){
  110. $field = $criteria['field'];
  111. $value = $criteria['value'];
  112. if( !empty( $criteria['wildcard'] ) && $criteria['wildcard'] === true){
  113. $sql_parts[] = "`".mysql_real_escape_string($field, $config->db->con)."` LIKE '".mysql_real_escape_string($value, $config->db->con)."'";
  114. }else{
  115. $sql_parts[] = "`".mysql_real_escape_string($field, $config->db->con)."` = '".mysql_real_escape_string($value, $config->db->con)."'";
  116. }
  117. }else{
  118. $sql_parts[] = $criteria['literal'];
  119. }
  120. }
  121. // Ordering
  122. if( !empty($options['orderby']) ){
  123. $field = MK_RecordManager::getFromId($field_module->getId(), $options['orderby']['field']);
  124. $orderby_field = $field->getName();
  125. $orderby_direction = $options['orderby']['direction'];
  126. }elseif( $this->getFieldOrderby() && $this->getOrderbyDirection() ){
  127. $field = MK_RecordManager::getFromId($field_module->getId(), $this->getFieldOrderby());
  128. $orderby_field = $field->getName();
  129. $orderby_direction = $this->getOrderbyDirection();
  130. }
  131. if( isset($orderby_field, $orderby_direction) ){
  132. $options = array(
  133. 'orderby' => array(
  134. 'field' => $orderby_field,
  135. 'direction' => $orderby_direction
  136. )
  137. );
  138. }
  139. $get_records = mysql_query("SELECT * FROM `".$this->getTable()."`".($sql_parts ? " WHERE ".implode(' AND ', $sql_parts) : '').( !empty($options['orderby']) ? ' ORDER BY `'.$options['orderby']['field'].'` '.$options['orderby']['direction'] : "" ).( $paginator ? " LIMIT ".$paginator->getRecordStart().", ".$paginator->getPerPage() : ""), $config->db->con);
  140. if($paginator){
  141. $get_total_records = mysql_query("SELECT COUNT(*) AS `total` FROM `".$this->getTable()."`".($sql_parts ? " WHERE ".implode(' AND ', $sql_parts) : '' ), $config->db->con);
  142. $res_total_records = mysql_fetch_assoc( $get_total_records );
  143. $paginator->setTotalRecords($res_total_records['total']);
  144. mysql_free_result($get_total_records);
  145. }
  146. if(mysql_num_rows($get_records) > 0){
  147. while($res_records = mysql_fetch_assoc($get_records)){
  148. $search_results[] = MK_RecordManager::getFromId($this->getId(), $res_records['id']);
  149. }
  150. }
  151. mysql_free_result($get_records);
  152. return $search_results;
  153. }
  154. public function countRecords($search_criteria){
  155. $config = MK_Config::getInstance();
  156. $sql_parts = array();
  157. $field_module = MK_RecordModuleManager::getFromType('module_field');
  158. // Search criteria
  159. foreach($search_criteria as $criteria){
  160. if( empty($criteria['literal']) ){
  161. $field = $criteria['field'];
  162. $value = $criteria['value'];
  163. if( !empty( $criteria['wildcard'] ) && $criteria['wildcard'] === true){
  164. $sql_parts[] = "`".mysql_real_escape_string($field, $config->db->con)."` LIKE '".mysql_real_escape_string($value, $config->db->con)."'";
  165. }else{
  166. $sql_parts[] = "`".mysql_real_escape_string($field, $config->db->con)."` = '".mysql_real_escape_string($value, $config->db->con)."'";
  167. }
  168. }else{
  169. $sql_parts[] = $criteria['literal'];
  170. }
  171. }
  172. $get_total_records = mysql_query("SELECT COUNT(*) AS `total` FROM `".$this->getTable()."`".($sql_parts ? " WHERE ".implode(' AND ', $sql_parts) : '' ), $config->db->con);
  173. $res_total_records = mysql_fetch_assoc( $get_total_records );
  174. return $res_total_records['total'];
  175. }
  176. public function searchRecordExact($search_criteria){
  177. $config = MK_Config::getInstance();
  178. $sql_parts = array();
  179. foreach($search_criteria as $field => $value){
  180. $sql_parts[] = "`".mysql_real_escape_string($field, $config->db->con)."` = '".mysql_real_escape_string($value, $config->db->con)."'";
  181. }
  182. $get_record = mysql_query("SELECT `id` FROM `".$this->getTable()."` WHERE ".implode(' AND ', $sql_parts)." LIMIT 1", $config->db->con);
  183. if($res_record = mysql_fetch_assoc($get_record)){
  184. return MK_RecordManager::getFromId($this->getId(), $res_record['id']);
  185. }else{
  186. return null;
  187. }
  188. mysql_free_result($get_record);
  189. }
  190. public function getDataGridFields()
  191. {
  192. return $this->getFields(true);
  193. }
  194. public function getFields($display_grid_only = false){
  195. $return_fields = array();
  196. $field_module = MK_RecordModuleManager::getFromSlug('fields');
  197. foreach($this->fields as $field_key => $field){
  198. $field = MK_RecordManager::getFromId($field_module->getId(), $field);
  199. if($display_grid_only === true){
  200. if($field->getDisplayWidth()){
  201. $return_fields[] = $field;
  202. }
  203. }else{
  204. $return_fields[] = $field;
  205. }
  206. }
  207. return $return_fields;
  208. }
  209. public function getField($field_name){
  210. if( !empty($this->fields[$field_name]) ){
  211. return $this->fields[$field_name];
  212. }
  213. }
  214. public function save( $update_meta = true ){
  215. $config = MK_Config::getInstance();
  216. if( !empty( $this->meta['id'] ) )
  217. {
  218. $old_data = $this->build( $this->getId() );
  219. $rename_table = mysql_query("RENAME TABLE `".$old_data['table']."` TO `".$this->getTable()."`", $config->db->con);
  220. return parent::save( $update_meta );
  221. }else{
  222. $create_table = mysql_query("CREATE TABLE `".$this->getTable()."` (id INT(16) NOT NULL AUTO_INCREMENT PRIMARY KEY) CHARSET=utf8 COLLATE=utf8_unicode_ci", $config->db->con);
  223. parent::save();
  224. $insert_record = mysql_query("INSERT INTO `modules_fields` (`order`, `module_id`, `name`, `label`, `type`, `editable`, `display_width`) VALUES ('1', '".$this->getId()."', 'id', 'ID', 'id', '0', '')", $config->db->con);
  225. $index_id = mysql_insert_id();
  226. $this->setFieldUid($index_id);
  227. return parent::save( $update_meta );
  228. }
  229. }
  230. public function getModule(){
  231. $module = MK_RecordModuleManager::getFromType('module');
  232. return $module;
  233. }
  234. public function getFieldList()
  235. {
  236. $fields = $this->fields;
  237. if(count($fields) === 1){
  238. $this->buildFieldList();
  239. $fields = $this->fields;
  240. }
  241. return array_fill_keys( array_keys( $fields ), '' );
  242. }
  243. public function delete(){
  244. $config = MK_Config::getInstance();
  245. $records = $this->getRecords();
  246. foreach($records as $record){
  247. $record->delete();
  248. }
  249. parent::delete();
  250. $this->deleted = true;
  251. $delete_table = mysql_query("DROP TABLE `".$this->getTable()."`", $config->db->con);
  252. }
  253. }
  254. ?>