/core/CommonModule.php

https://github.com/rasstroen/audio · PHP · 149 lines · 97 code · 23 blank · 29 comment · 9 complexity · 10ce26feeffe2f297844edce3c081ec2 MD5 · raw file

  1. <?php
  2. // класс, генерирующий xml для вывода объектов,списков объектов,редактирования и создания объектов
  3. class CommonModule extends BaseModule {
  4. /* @var $Collection Collection */
  5. protected $Collection = null;
  6. public $ConditionsEnabled = false;
  7. function generateData() {
  8. $this->_before_process();
  9. $this->checkConditions();
  10. $this->setCollectionClass();
  11. $this->_process($this->action, $this->mode);
  12. $this->_after_process();
  13. }
  14. function checkConditions() {
  15. if (isset($this->params['per_page'])) {
  16. if ($this->params['per_page']) {
  17. $this->ConditionsEnabled = true;
  18. }
  19. }
  20. }
  21. function prepareSelect($fields = '*', $where = false, $order = false, $limit = false) {
  22. $limit = $limit ? 'LIMIT ' . $limit : '';
  23. $order = $order ? 'ORDER BY ' . $order : '';
  24. $where = $where ? 'WHERE ' . $where : '';
  25. $query = 'SELECT ' . $fields . ' FROM ' . $this->Collection->tableName . ' ' . $where . ' ' . $order . ' ' . $limit;
  26. return $query;
  27. }
  28. function getCountBySQL($where = false) {
  29. if (isset($this->cachedCount[$where]))
  30. return $this->cachedCount[$where];
  31. $where = $where ? 'WHERE ' . $where : '';
  32. $query = 'SELECT COUNT(1) FROM ' . $this->Collection->tableName . ' ' . $where;
  33. $this->cachedCount[$where] = max(0, (int) Database::sql2single($query));
  34. return $this->cachedCount[$where];
  35. }
  36. // для объектов какого класса мы выполняем действия?
  37. function setCollectionClass() {
  38. throw new Exception('setCollectionClass must be overriden');
  39. }
  40. function getParam($field, $default = false) {
  41. return isset($this->params[$field]) ? $this->params[$field] : $default;
  42. }
  43. // выполняем действия
  44. function _process($action, $mode) {
  45. throw new Exception('CommonModule::_process must be overriden');
  46. }
  47. // выводим объект
  48. function _show($id) {
  49. $object = $this->Collection->getByIdLoaded($id);
  50. if (!$object->exists) {
  51. throw new Exception('К сожалению, такого у нас в базе совсем нет');
  52. }
  53. $this->data[$this->Collection->itemName] = $object->_show();
  54. }
  55. /**
  56. * Возвращает данные
  57. * @param type $where
  58. * @param type $sortings
  59. * @return type
  60. */
  61. function _list_get($where, $sortings = false) {
  62. return $this->_list($where, $sortings, $return = true);
  63. }
  64. /**
  65. * устанавливает $this->data[items_name]
  66. * @param type $where
  67. * @param type $sortings
  68. * @param type $return
  69. */
  70. function _list($where, $sortings = false, $return = false) {
  71. $limit = false;
  72. $order = false;
  73. $sorting_order = false;
  74. $cond = new Conditions();
  75. if ($this->ConditionsEnabled) {
  76. // пейджинг, сортировка
  77. if ($sortings) {
  78. $cond->setSorting($sortings);
  79. $order = $cond->getSortingField();
  80. $sorting_order = $cond->getSortingOrderSQL();
  81. }
  82. $per_page = isset($this->params['per_page']) ? $this->params['per_page'] : 0;
  83. $pagingName = isset($this->params['paging_parameter_name']) ? $this->params['paging_parameter_name'] : 'p';
  84. if ($per_page) {
  85. $cond->setPaging($this->getCountBySQL($where), $per_page, $pagingName);
  86. $limit = $cond->getLimit();
  87. }
  88. }
  89. $query = $this->prepareSelect('id', $where, $order ? ($order . ' ' . $sorting_order) : '', $limit);
  90. $ids = Database::sql2array($query, 'id'); // нашли объекты, которые хотим вывести
  91. if ($return)
  92. return $this->_idsToData(array_keys($ids)); // отдаем массив
  93. else
  94. $this->data = $this->_idsToData(array_keys($ids)); // отдаем массив
  95. $this->data['conditions'] = $cond->getConditions();
  96. return true;
  97. }
  98. /**
  99. * возвращает массив listData() объектов
  100. * @param type $ids
  101. * @param type $limit
  102. * @return type
  103. */
  104. function _idsToData($ids, $limit = 100) {
  105. if ($limit) {
  106. $ids = array_slice($ids, 0, $limit);
  107. }
  108. return $this->Collection->idsToData($ids);
  109. }
  110. // выводим список объектов по id
  111. function _list_($ids) {
  112. $objects = $this->Collection->getByIdsLoaded($ids);
  113. }
  114. // выводим данные для редактирования
  115. function _edit() {
  116. }
  117. // выводим данные для создания нового объекта
  118. function _new() {
  119. }
  120. // выполняется перед обработкой
  121. function _before_process() {
  122. }
  123. // выполняется после обработки
  124. function _after_process() {
  125. }
  126. }