BxAdsGridCategories.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Ads Ads
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxAdsGridCategories extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sBaseUrl;
  16. protected $_iParentId;
  17. protected $_aParentInfo;
  18. public function __construct ($aOptions, $oTemplate = false)
  19. {
  20. parent::__construct ($aOptions, $oTemplate);
  21. $this->_sModule = 'bx_ads';
  22. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  23. $this->_sBaseUrl = bx_append_url_params(BX_DOL_URL_STUDIO . 'module.php', array(
  24. 'name' => $this->_sModule,
  25. 'page' => 'categories'
  26. ));
  27. $this->_iParentId = bx_get('parent_id') !== false ? (int)bx_get('parent_id') : 0;
  28. $this->_aQueryAppend['parent_id'] = $this->_iParentId;
  29. if(!empty($this->_iParentId))
  30. $this->_aParentInfo = $this->_oModule->_oDb->getCategories(array('type' => 'id', 'id' => $this->_iParentId));
  31. }
  32. public function performActionAdd()
  33. {
  34. $CNF = &$this->_oModule->_oConfig->CNF;
  35. $sAction = 'add';
  36. $oForm = $this->_getFormObject($sAction);
  37. $oForm->initChecker();
  38. if($oForm->isSubmittedAndValid()) {
  39. $aValsToAdd = array('active' => 1);
  40. $iParentId = (int)$oForm->getCleanValue('parent_id');
  41. if($iParentId != 0) {
  42. $aParentInfo = $this->_oModule->_oDb->getCategories(array('type' => 'id', 'id' => $iParentId));
  43. $aValsToAdd['level'] = $aParentInfo['level'] + 1;
  44. $aValsToAdd['order'] = $this->_oModule->_oDb->getCategories(array('type' => 'parent_id_order', 'parent_id' => $iParentId)) + 1;
  45. }
  46. else {
  47. $aValsToAdd['level'] = 0;
  48. $aValsToAdd['order'] = $this->_oModule->_oDb->getCategories(array('type' => 'parent_id_order', 'parent_id' => 0)) + 1;
  49. }
  50. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  51. $aLanguages = $oLanguage->getLanguagesExt();
  52. $sLanguageCurrent = $oLanguage->getCurrentLangName(false);
  53. $sTitle = BxDolForm::getSubmittedValue('title-' . $sLanguageCurrent, $oForm->aFormAttrs['method']);
  54. $sName = uriGenerate(strtolower($sTitle), $CNF['TABLE_CATEGORIES'], 'name', 'cat', '_');
  55. $aValsToAdd['name'] = $sName;
  56. BxDolForm::setSubmittedValue('title', '_bx_ads_cat_' . $sName, $oForm->aFormAttrs['method']);
  57. BxDolForm::setSubmittedValue('text', '_bx_ads_cat_' . $sName, $oForm->aFormAttrs['method']);
  58. if($oForm->getCleanValue('type_clone') == 'on') {
  59. $sTypeNewTitle = BxDolForm::getSubmittedValue('type_title-' . $sLanguageCurrent, $oForm->aFormAttrs['method']);
  60. if(empty($sTypeNewTitle))
  61. $sTypeNewTitle = $sTitle;
  62. $sTypeNewName = uriGenerate(strtolower($sTypeNewTitle), $CNF['TABLE_CATEGORIES_TYPES'], 'name', 'cat_type', '_');
  63. $iType = (int)$oForm->getCleanValue('type');
  64. $aType = $this->_oModule->_oDb->getCategoryTypes(array('type' => 'id', 'id' => $iType));
  65. $aTypeNew = array('name' => $sTypeNewName, 'title' => '_bx_ads_cat_type_' . $sTypeNewName);
  66. foreach($aLanguages as $sLanguage => $aLanguage) {
  67. $sTypeNewTitleLang = bx_process_input(BxDolForm::getSubmittedValue('type_title-' . $sLanguage, $oForm->aFormAttrs['method']));
  68. if(!empty($sTypeNewTitleLang))
  69. $sTypeNewKeys[$aTypeNew['title']][$aLanguage['id']] = $sTypeNewTitleLang;
  70. }
  71. $bDisplayCloned = true;
  72. $aDisplayTypes = array('add', 'edit', 'view');
  73. foreach($aDisplayTypes as $sDisplayType) {
  74. $sDisplay = 'display_' . $sDisplayType;
  75. if(empty($aType[$sDisplay])) {
  76. $aTypeNew[$sDisplay] = '';
  77. continue;
  78. }
  79. $sDisplayNewName = 'bx_ads_entry_' . $sTypeNewName . '_' . $sDisplayType;
  80. $sDisplayNewTitle = '_bx_ads_form_display_' . $sTypeNewName . '_' . $sDisplayType;
  81. if(!$this->_oModule->_oDb->cloneDisplay($aType[$sDisplay], $sDisplayNewName, $sDisplayNewTitle)) {
  82. $bDisplayCloned = false;
  83. break;
  84. }
  85. $aTypeNew[$sDisplay] = $sDisplayNewName;
  86. $sTypeNewKeys[$sDisplayNewTitle] = _t($CNF['T']['txt_display_' . $sDisplayType], $sTypeNewTitle);
  87. }
  88. if($bDisplayCloned && ($iTypeNewId = $this->_oModule->_oDb->insertCategoryType($aTypeNew)) !== 0) {
  89. BxDolForm::setSubmittedValue('type', $iTypeNewId, $oForm->aFormAttrs['method']);
  90. foreach($sTypeNewKeys as $sKey => $mixedString)
  91. $oLanguage->addLanguageString($sKey, $mixedString, 0, 0, false);
  92. $oLanguage->compileLanguage();
  93. }
  94. }
  95. unset($oForm->aInputs['type_clone'], $oForm->aInputs['type_title']);
  96. $iId = (int)$oForm->insert($aValsToAdd);
  97. if($iId != 0)
  98. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  99. else
  100. $aRes = array('msg' => _t('_bx_ads_grid_action_err_add_category'));
  101. echoJson($aRes);
  102. }
  103. else {
  104. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_sModule . '-category-add-popup', _t('_bx_ads_grid_popup_title_add'), $this->_oModule->_oTemplate->parseHtmlByName('category_form.html', array(
  105. 'form_id' => $oForm->aFormAttrs['id'],
  106. 'form' => $oForm->getCode(true),
  107. 'object' => $this->_sObject,
  108. 'action' => $sAction
  109. )));
  110. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  111. }
  112. }
  113. public function performActionEdit()
  114. {
  115. $CNF = &$this->_oModule->_oConfig->CNF;
  116. $sAction = 'edit';
  117. $aIds = bx_get('ids');
  118. if(!$aIds || !is_array($aIds)) {
  119. $iId = (int)bx_get('id');
  120. if(!$iId)
  121. return false;
  122. $aIds = array($iId);
  123. }
  124. $iId = $aIds[0];
  125. $aCategory = $this->_oModule->_oDb->getCategories(array('type' => 'id', 'id' => $iId));
  126. if(empty($aCategory) || !is_array($aCategory))
  127. return echoJson(array());
  128. $oForm = $this->_getFormObject($sAction, $aCategory);
  129. $oForm->initChecker($aCategory);
  130. if($oForm->isSubmittedAndValid()) {
  131. $aValsToAdd = array('active' => 1);
  132. $iParentId = (int)$oForm->getCleanValue('parent_id');
  133. if($iParentId != 0) {
  134. $aParentInfo = $this->_oModule->_oDb->getCategories(array('type' => 'id', 'id' => $iParentId));
  135. $aValsToAdd['level'] = $aParentInfo['level'] + 1;
  136. $aValsToAdd['order'] = $this->_oModule->_oDb->getCategories(array('type' => 'parent_id_order', 'parent_id' => $iParentId)) + 1;
  137. }
  138. else {
  139. $aValsToAdd['level'] = 0;
  140. $aValsToAdd['order'] = $this->_oModule->_oDb->getCategories(array('type' => 'parent_id_order', 'parent_id' => 0)) + 1;
  141. }
  142. $iId = (int)$oForm->update($iId, $aValsToAdd);
  143. if($iId != 0)
  144. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  145. else
  146. $aRes = array('msg' => _t('_bx_ads_grid_action_err_edit_category'));
  147. echoJson($aRes);
  148. }
  149. else {
  150. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_sModule . '-category-edit-popup', _t('_bx_ads_grid_popup_title_edit', _t($aCategory['title'])), $this->_oModule->_oTemplate->parseHtmlByName('category_form.html', array(
  151. 'form_id' => $oForm->aFormAttrs['id'],
  152. 'form' => $oForm->getCode(true),
  153. 'object' => $this->_sObject,
  154. 'action' => $sAction
  155. )));
  156. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  157. }
  158. }
  159. protected function _getFormObject($sAction, $aCategory = array())
  160. {
  161. $CNF = &$this->_oModule->_oConfig->CNF;
  162. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_CATEGORY'], $CNF['OBJECT_FORM_CATEGORY_DISPLAY_' . strtoupper($sAction)]);
  163. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&parent_id=' . $this->_iParentId;
  164. if(!empty($aCategory['id']))
  165. $oForm->aFormAttrs['action'] .= '&id=' . $aCategory['id'];
  166. if(isset($oForm->aInputs['parent_id'])) {
  167. if(empty($this->_iParentId)) {
  168. $oForm->aInputs['parent_id']['values'][] = array('key' => 0, 'value' => _t('_bx_ads_form_category_input_parent_id_empty'));
  169. $aCategories = $this->_oModule->_oDb->getCategories(array('type' => 'parent_id', 'parent_id' => $this->_iParentId));
  170. foreach($aCategories as $aCategory)
  171. $oForm->aInputs['parent_id']['values'][] = array(
  172. 'key' => $aCategory['id'],
  173. 'value' => _t($aCategory['title'])
  174. );
  175. }
  176. else {
  177. $oForm->aInputs['parent_id']['type'] = 'hidden';
  178. $oForm->aInputs['parent_id']['value'] = $this->_iParentId;
  179. }
  180. }
  181. return $oForm;
  182. }
  183. protected function _getActionBack($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  184. {
  185. if(empty($this->_iParentId))
  186. return '';
  187. $sUrl = bx_append_url_params($this->_sBaseUrl, array('parent_id' => $this->_aParentInfo['parent_id']));
  188. $a['attr'] = array_merge($a['attr'], array(
  189. "onclick" => "window.open('" . $sUrl . "','_self');"
  190. ));
  191. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  192. }
  193. protected function _getCellIcon ($mixedValue, $sKey, $aField, $aRow)
  194. {
  195. $mixedValue = $this->_oTemplate->getIcon($mixedValue, array('class' => 'bx-def-border'));
  196. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  197. }
  198. protected function _getCellSubcategories ($mixedValue, $sKey, $aField, $aRow)
  199. {
  200. $sContent = _t('_bx_ads_grid_column_value_subcategories', $this->_oModule->_oDb->getCategories(array('type' => 'parent_id_count', 'parent_id' => $aRow['id'])));
  201. $mixedValue = $this->_oTemplate->parseLink(bx_append_url_params($this->_sBaseUrl, array('parent_id' => $aRow['id'])), $sContent, array(
  202. 'title' => _t('_bx_ads_grid_column_info_subcategories')
  203. ));
  204. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  205. }
  206. protected function _addJsCss()
  207. {
  208. parent::_addJsCss();
  209. $this->_oModule->_oTemplate->addStudioJs(array('jquery.form.min.js', 'studio.js'));
  210. $oForm = new BxTemplStudioFormView(array());
  211. $oForm->addCssJs();
  212. }
  213. protected function _isVisibleGrid ($a)
  214. {
  215. return isAdmin();
  216. }
  217. protected function _delete ($mixedId)
  218. {
  219. $sTable = $this->_aOptions['table'];
  220. $sFieldId = $this->_aOptions['field_id'];
  221. $aCategory = $this->_oModule->_oDb->getRow("SELECT * FROM `" . $sTable . "` WHERE `" . $sFieldId . "`=:id", array('id' => $mixedId));
  222. if(!empty($aCategory['title'])) {
  223. $oLanguage = BxDolStudioLanguagesUtils::getInstance();
  224. $oLanguage->deleteLanguageString($aCategory['title']);
  225. $oLanguage->deleteLanguageString($aCategory['text']);
  226. }
  227. return parent::_delete($mixedId);
  228. }
  229. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  230. {
  231. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `parent_id`=?", $this->_iParentId);
  232. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  233. }
  234. }
  235. /** @} */