BxDolAdminBuilder.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /***************************************************************************
  3. * Dolphin Smart Community Builder
  4. * -----------------
  5. * begin : Mon Mar 23 2006
  6. * copyright : (C) 2006 BoonEx Group
  7. * website : http://www.boonex.com/
  8. * This file is part of Dolphin - Smart Community Builder
  9. *
  10. * Dolphin is free software. This work is licensed under a Creative Commons Attribution 3.0 License.
  11. * http://creativecommons.org/licenses/by/3.0/
  12. *
  13. * Dolphin is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the Creative Commons Attribution 3.0 License for more details.
  16. * You should have received a copy of the Creative Commons Attribution 3.0 License along with Dolphin,
  17. * see license.txt file; if not, write to marketing@boonex.com
  18. ***************************************************************************/
  19. class BxDolAdminBuilder {
  20. var $_aContainers;
  21. var $_sPostUrl;
  22. var $_sTable;
  23. var $_sFieldOrder = '`order`';
  24. var $_sFieldContainer = '`active`';
  25. var $_sFieldId = '`id`';
  26. var $_sFieldTitle = '`title`';
  27. /**
  28. * Constructor
  29. * @param array $aContainers - containers, id as key and title as value
  30. */
  31. function BxDolAdminBuilder($sTable, $sPostUrl, $aContainers) {
  32. $this->_sPostUrl = $sPostUrl;
  33. $this->_sTable = $sTable;
  34. $this->_aContainers = $aContainers;
  35. }
  36. /**
  37. * Process post actions here
  38. */
  39. function handlePostActions ($aData) {
  40. switch ($aData['action']) {
  41. case 'SaveItemsOrder':
  42. $this->postSaveItemsOrder ($aData);
  43. break;
  44. }
  45. }
  46. /**
  47. * Post action: SaveItemsOrder
  48. */
  49. function postSaveItemsOrder ($aData) {
  50. if (!is_array($aData['cont_names']) || !is_array($aData['cont_data']))
  51. return;
  52. foreach ($aData['cont_names'] as $i => $sKey) {
  53. $aIds = $this->_filterIds(explode(',', $aData['cont_data'][$i]));
  54. $this->saveItemsOrderForContainer($sKey, $aIds);
  55. }
  56. echo 'ok';
  57. }
  58. /**
  59. * Get ready to print builder page
  60. */
  61. function getBuilderPage () {
  62. $sContanersIds = '';
  63. $aContainersForTemplate = array ();
  64. foreach ($this->_aContainers as $sKey => $sTitle) {
  65. $sContanersIds .= '#bx-sort-cont-' . $sKey . ',';
  66. $aContainersForTemplate[] = array (
  67. 'id' => $sKey,
  68. 'title' => $sTitle,
  69. 'bx_repeat:items' => $this->_generateItemsForContainer($sKey),
  70. );
  71. }
  72. $a = array (
  73. 'post_url' => $this->_sPostUrl,
  74. 'containers_ids' => substr($sContanersIds, 0, -1),
  75. 'bx_repeat:containers' => $aContainersForTemplate,
  76. );
  77. $this->addExternalResources ();
  78. return $GLOBALS['oAdmTemplate']->parseHtmlByName('mobile_builder_page.html', $a);
  79. }
  80. /**
  81. * Add external recources, like JS and CSS file
  82. */
  83. function addExternalResources () {
  84. $GLOBALS['oAdmTemplate']->addJs(array(
  85. 'ui.core.js',
  86. 'ui.sortable.js'
  87. ));
  88. $GLOBALS['oAdmTemplate']->addCss(array(
  89. 'mobile.css',
  90. 'mobile_builder.css',
  91. ));
  92. }
  93. function _filterIds ($a) {
  94. $aRet = array ();
  95. foreach ($a as $iId) {
  96. if (!$iId || $iId < 0)
  97. continue;
  98. $aRet[] = $iId;
  99. }
  100. return $aRet;
  101. }
  102. function _generateItemsForContainer ($sKey) {
  103. $a = $this->getItemsForContainer ($sKey);
  104. foreach ($a as $i => $r) {
  105. $a[$i]['item'] = $this->getItem($r);
  106. }
  107. return $a;
  108. }
  109. /**
  110. * Override this function to return data for particular container, defined in costructor.
  111. * Each record must have 'id' and 'title' record at least.
  112. * @param $sKey container id
  113. * @return array
  114. */
  115. function getItemsForContainer ($sKey) {
  116. // !!! override this
  117. return array (
  118. array ('id' => 1, 'title' => 'Opa1'),
  119. array ('id' => 2, 'title' => 'Opa2'),
  120. );
  121. }
  122. /**
  123. * Override this function to return real item for dragging
  124. * @param $aItem array of item properties from database
  125. * @return ready html
  126. */
  127. function getItem ($aItem) {
  128. // !!! override this
  129. return $aItem['title'];
  130. }
  131. /**
  132. * Override this function to save items order to database, but by default it should be fine in most situations
  133. * @param $sKey container id
  134. * @param $aOrderedIds arrayy of ids in the right order
  135. * @return number of affected rows$aData
  136. */
  137. function saveItemsOrderForContainer($sKey, $aOrderedIds) {
  138. global $MySQL;
  139. if (!isset($this->_aContainers[$sKey]))
  140. return false;
  141. $iRet = 0;
  142. $i = 1;
  143. foreach ($aOrderedIds as $iId) {
  144. $iRet += $MySQL->query("UPDATE {$this->_sTable} SET {$this->_sFieldOrder} = $i, {$this->_sFieldContainer} = '$sKey' WHERE {$this->_sFieldId} = " . (int)$iId);
  145. ++$i;
  146. }
  147. return $iRet;
  148. }
  149. }