RomanL 7 лет назад
Родитель
Сommit
70813deb65

+ 1 - 0
studio/classes/BxDolStudioForms.php

@@ -15,6 +15,7 @@ define('BX_DOL_STUDIO_FORM_TYPE_PRE_VALUES', 'pre_values');
 define('BX_DOL_STUDIO_FORM_TYPE_SEARCH_FORMS', 'search_forms');
 define('BX_DOL_STUDIO_FORM_TYPE_SEARCH_FIELDS', 'search_fields');
 define('BX_DOL_STUDIO_FORM_TYPE_LABELS', 'labels');
+define('BX_DOL_STUDIO_FORM_TYPE_CATEGORIES', 'categories');
 
 define('BX_DOL_STUDIO_FORM_TYPE_DEFAULT', BX_DOL_STUDIO_FORM_TYPE_FORMS);
 

+ 44 - 0
studio/classes/BxDolStudioFormsCategories.php

@@ -0,0 +1,44 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaStudio UNA Studio
+ * @{
+ */
+
+class BxDolStudioFormsCategories extends BxTemplStudioGrid
+{
+    protected $sModule;
+    protected $_oCategory;
+
+    public function __construct ($aOptions, $oTemplate = false)
+    {
+        parent::__construct ($aOptions, $oTemplate);
+
+        $this->_sDefaultSortingOrder = 'DESC';
+        $this->_oCategory = BxDolCategories::getInstance();
+        
+        $this->oDb = new BxDolStudioFormsQuery();
+
+        $sModule = bx_get('module');
+        if(!empty($sModule)) {
+            $this->sModule = bx_process_input($sModule);
+            $this->_aQueryAppend['module'] = $this->sModule;
+        }
+    }
+
+    protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
+    {
+        $sModule = $this->sModule;
+        if(strpos($sFilter, $this->sParamsDivider) !== false)
+            list($sModule, $sFilter) = explode($this->sParamsDivider, $sFilter);
+
+        if($sModule != '')
+            $this->_aOptions['source'] .= $this->oDb->prepareAsString(" AND `module`=?", $sModule);
+        
+        return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
+    }
+}
+
+/** @} */

+ 54 - 1
studio/classes/BxDolStudioFormsQuery.php

@@ -470,6 +470,59 @@ class BxDolStudioFormsQuery extends BxDolDb
             WHERE 1 AND `tl`.`key`=?", $sKey);
         return (int)$this->getOne($sSql) > 0;
     }
+    
+    function getCategories($aParams, &$aItems, $bReturnCount = true)
+    {
+        $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
+        $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
+
+        if(!isset($aParams['order']) || empty($aParams['order']))
+            $sOrderClause = "ORDER BY `tv`.`module` ASC";
+
+        switch($aParams['type']) {
+            case 'by_id':
+                $aMethod['name'] = 'getRow';
+                $aMethod['params'][1] = array(
+                	'id' => $aParams['value']
+                );
+
+                $sWhereClause = " AND `tv`.`id`=:id ";
+                break;
+
+            case 'counter_by_modules':
+                $aMethod['name'] = 'getPairs';
+                $aMethod['params'][1] = 'module';
+                $aMethod['params'][2] = 'counter';
+                $sSelectClause = ", `tv`.`module` AS `module`, COUNT(`tv`.`id`) AS `counter`";
+                $sGroupClause = "GROUP BY `tv`.`module`";
+                break;
+
+            case 'all':
+                break;
+        }
+
+        $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
+                `tv`.`id` AS `id`,
+                `tv`.`Value` AS `value`,
+                `tv`.`Value` AS `Value` " . $sSelectClause . "
+            FROM `sys_categories` AS `tv` " . $sJoinClause . "
+            WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
+        $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
+
+        if(!$bReturnCount)
+            return !empty($aItems);
+
+        return (int)$this->getOne("SELECT FOUND_ROWS()");
+    }
+    
+    function deleteCategories($CategoryId)
+    {
+        $sSql = "DELETE FROM `sys_categories2objects` WHERE `category_id` = :category_id";
+        $aBindings = array(
+            'category_id' => $CategoryId
+    	);
+        return $this->query($sSql, $aBindings);
+    }
 
     function getValues($aParams, &$aItems, $bReturnCount = true)
     {
@@ -551,7 +604,7 @@ class BxDolStudioFormsQuery extends BxDolDb
 
         return (int)$this->getOne("SELECT FOUND_ROWS()");
     }
-
+    
     function deleteValues($aParams)
     {
     	$aBindings = array();

+ 47 - 0
studio/js/forms_categories.js

@@ -0,0 +1,47 @@
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaStudio UNA Studio
+ * @{
+ */
+function BxDolStudioFormsCategories(oOptions) {
+    this.sActionsUrl = oOptions.sActionUrl;
+    this.sPageUrl = oOptions.sPageUrl;
+    this.sObjNameGrid = oOptions.sObjNameGrid;
+    this.sObjName = oOptions.sObjName == undefined ? 'oBxDolStudioFormsCategories' : oOptions.sObjName;
+    this.sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'fade' : oOptions.sAnimationEffect;
+    this.iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
+    this.sParamsDivider = oOptions.sParamsDivider == undefined ? '#-#' : oOptions.sParamsDivider;
+
+    this.sTextSearchInput = oOptions.sTextSearchInput == undefined ? '' : oOptions.sTextSearchInput;
+}
+
+BxDolStudioFormsCategories.prototype.onChangeModule = function () {
+    this.reloadGrid($('#bx-grid-module-' + this.sObjNameGrid).val());
+};
+
+BxDolStudioFormsCategories.prototype.reloadGrid = function (sModule) {
+    var bReload = false;
+    var oActions = $("[bx_grid_action_independent]");
+    if(!sModule){
+        oActions.addClass('bx-btn-disabled');
+    }
+    else {
+        oActions.removeClass('bx-btn-disabled');
+    }
+
+    if (glGrids[this.sObjNameGrid]._oQueryAppend['module'] != sModule) {
+        glGrids[this.sObjNameGrid]._oQueryAppend['module'] = sModule;
+        bReload = true;
+    }
+
+    if (bReload) {
+        var sValueSearch = $('#bx-grid-search-' + this.sObjNameGrid).val();
+        if (sValueSearch == this.sTextSearchInput)
+            sValueSearch = '';
+
+        glGrids[this.sObjNameGrid].setFilter(sModule + this.sParamsDivider + sValueSearch, true);
+    }
+};
+/** @} */

+ 10 - 0
studio/template/forms_categories.html

@@ -0,0 +1,10 @@
+__content__
+<script type="text/javascript">
+    var __js_object__ = new BxDolStudioFormsCategories({
+        sActionUrl: '<bx_url_root />grid.php',
+        sPageUrl: '__page_url__',
+        sObjNameGrid: '__grid_object__',
+        sParamsDivider: '__params_divider__',
+        sTextSearchInput: '<bx_text_js:_sys_grid_search />'
+    });
+</script>

+ 13 - 1
studio/template/scripts/BxBaseStudioForms.php

@@ -28,6 +28,7 @@ class BxBaseStudioForms extends BxDolStudioForms
             'search_forms' => 'sys_studio_search_forms',
             'search_fields' => 'sys_studio_search_forms_fields',
             'labels' => 'sys_studio_labels',
+            'categories' => 'sys_studio_categories',
     	);
     }
 
@@ -60,6 +61,7 @@ class BxBaseStudioForms extends BxDolStudioForms
             BX_DOL_STUDIO_FORM_TYPE_SEARCH_FORMS => array('icon' => 'search'),
             BX_DOL_STUDIO_FORM_TYPE_SEARCH_FIELDS => array('icon' => 'check-square'),
             BX_DOL_STUDIO_FORM_TYPE_LABELS => array('icon' => 'tags'),
+            BX_DOL_STUDIO_FORM_TYPE_CATEGORIES => array('icon' => 'folder'),
         );
         foreach($aMenuItems as $sMenuItem => $aItem)
             $aMenu[] = array(
@@ -108,7 +110,7 @@ class BxBaseStudioForms extends BxDolStudioForms
         $sModule = bx_process_input($sModule);
         return array('code' => 0, 'message' => '', 'content' => $this->getPreValuesObject()->getListsSelector($sModule));
     }
-
+	
     function actionGetSearchForms()
     {
         if(($sModule = bx_get('form_module')) === false)
@@ -201,6 +203,16 @@ class BxBaseStudioForms extends BxDolStudioForms
     {
         return $this->getGridObject($this->aGridObjects['labels']);
     }
+    
+    protected function getCategories()
+    {
+        return $this->getGrid($this->aGridObjects['categories']);
+    }
+
+    protected function getCategoriesObject()
+    {
+        return $this->getGridObject($this->aGridObjects['categories']);
+    }
 
     protected function getGridObject($sObjectName)
     {

+ 327 - 0
studio/template/scripts/BxBaseStudioFormsCategories.php

@@ -0,0 +1,327 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) UNA, Inc - https://una.io
+ * MIT License - https://opensource.org/licenses/MIT
+ *
+ * @defgroup    UnaView UNA Studio Representation classes
+ * @ingroup     UnaStudio
+ * @{
+ */
+
+bx_import('BxTemplStudioFormView');
+
+class BxStudioFormsCategoriesCheckerHelper extends BxDolFormCheckerHelper
+{
+    public function checkAvailUnique($s, $aExclude = array())
+    {
+        $aLabel = BxDolCategories::getInstance()->getCategories(array(
+            'type' => 'value', 
+            'value' => $s
+        ));
+
+        if(!is_array($aExclude))
+            $aExclude = array($aExclude);
+
+        return empty($aLabel) || !is_array($aLabel) || in_array($aLabel['id'], $aExclude);
+    }
+}
+
+class BxBaseStudioFormsCategories extends BxDolStudioFormsCategories
+{    
+    public function __construct ($aOptions, $oTemplate = false)
+    {
+        parent::__construct ($aOptions, $oTemplate);
+        $this->sUrlPage = BX_DOL_URL_STUDIO . 'builder_forms.php?page=categories';
+    }
+    
+    public function performActionAdd()
+    {
+        $sAction = 'add';
+        
+        if(!$this->canAdd()) {
+            echoJson(array());
+            exit;
+        }
+        
+        $oForm = $this->_getForm($sAction);
+     
+        if (!$oForm)
+            return '';
+        $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('ids', '_r'));
+        $oForm->initChecker();
+        if($oForm->isSubmittedAndValid()) {
+            $mixedResult = $oForm->insert(array('added' => time(), 'module' => $this->sModule));
+            if(is_numeric($mixedResult))
+                $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
+            else
+                $aRes = array('msg' => $mixedResult);
+            echoJson($aRes);
+        }
+        else {
+            $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-categories-add-popup', _t('_adm_form_txt_categories_add_popup'), $this->_oTemplate->parseHtmlByName('form_add_category.html', array(
+               'form_id' => $oForm->aFormAttrs['id'],
+               'form' => $oForm->getCode(true),
+               'object' => $this->_sObject,
+               'action' => $sAction
+           )));
+
+           echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
+            
+        }
+    }
+    
+    public function performActionEdit()
+    {
+        $sAction = 'edit';
+        
+        $aIds = bx_get('ids');
+        if(!$aIds || !is_array($aIds)) {
+            $iId = (int)bx_get('id');
+            if(!$iId)
+                return echoJson(array());
+
+            $aIds = array($iId);
+        }
+
+        $iId = (int)array_shift($aIds);
+
+        $aCategory = $this->_oCategory->getCategories(array('type' => 'id', 'id' => $iId));
+        if(empty($aCategory) || !is_array($aCategory))
+            return echoJson(array());
+        
+        $this->sModule = $aCategory['module'];
+        $oForm = $this->_getForm($sAction, $aCategory);
+        $oForm->initChecker();
+        if($oForm->isSubmittedAndValid()) {
+            $mixedResult = $oForm->update($iId);
+            if(is_numeric($mixedResult))
+                $aRes = array('grid' => $this->getCode(false), 'blink' => $mixedResult);
+            else
+                $aRes = array('msg' => $mixedResult);
+            echoJson($aRes);
+        }
+        else {
+            $sContent = BxTemplStudioFunctions::getInstance()->popupBox('adm-form-categories-edit-popup', _t('_adm_form_txt_labels_edit_popup', $aCategory['value']), $this->_oTemplate->parseHtmlByName('form_add_category.html', array(
+               'form_id' => $oForm->aFormAttrs['id'],
+               'form' => $oForm->getCode(true),
+               'object' => $this->_sObject,
+               'action' => $sAction
+           )));
+
+           echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
+        }
+    }
+
+    public function performActionDelete()
+    {
+        $this->_replaceMarkers ();
+
+        $iAffected = 0;
+        $aIds = bx_get('ids');
+        if (!$aIds || !is_array($aIds)) {
+            echoJson(array());
+            exit;
+        }
+
+        foreach ($aIds as $mixedId){
+            if ($this->_delete($mixedId)){
+                $iAffected ++;
+                $this->oDb->deleteCategories($mixedId);
+            }
+        }
+
+        echo echoJson(array_merge(
+            array(
+                'grid' => $this->getCode(false),
+            ),
+            $iAffected ? array() : array('msg' => _t("_sys_grid_delete_failed"))
+        ));
+    }
+    
+    protected function _switcherChecked2State($isChecked)
+    {
+        return $isChecked ? 'active' : 'hidden';
+    }
+
+    protected function _switcherState2Checked($mixedState)
+    {
+        return 'active' == $mixedState ? true : false;
+    }
+    
+    protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
+    {
+        return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
+    }
+    
+    protected function _getCellAuthor($mixedValue, $sKey, $aField, $aRow)
+    {
+        if ($aRow['author'] > 0){
+    	    $oProfile = BxDolProfile::getInstanceMagic($aRow['author']);
+    	    $sProfile = $oProfile->getDisplayName();
+
+            $mixedValue =  $this->_oTemplate->parseHtmlByName('account_link.html', array(
+                'href' => $oProfile->getUrl(),
+                'title' => $sProfile,
+                'content' => $sProfile,
+                'class' => 'bx-def-font-grayed'
+            ));
+        }
+        else{
+            $mixedValue = '';
+        }
+        return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
+    }
+    
+    protected function _getCellModule($mixedValue, $sKey, $aField, $aRow)
+    {
+        $mixedValue = $this->_limitMaxLength($this->getModuleTitle($aRow['module']), $sKey, $aField, $aRow, $this->_isDisplayPopupOnTextOverflow);
+        return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
+    }
+    
+    protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
+    {
+        return parent::_getCellDefault(_t($mixedValue), $sKey, $aField, $aRow);
+    }
+    
+    protected function _getForm($sAction, $aCategory = array())
+    {
+        $aForm = array(
+            'form_attrs' => array(
+                'id' => 'adm-form-search-form-edit',
+                'action' => BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&module=' . $this->sModule . '&a=' . $sAction,
+                'method' => BX_DOL_STUDIO_METHOD_DEFAULT
+            ),
+            'params' => array (
+                'db' => array(
+                    'table' => 'sys_categories',
+                    'key' => 'id',
+                    'uri' => '',
+                    'uri_title' => '',
+                    'submit_name' => 'do_submit'
+                ),
+                'checker_helper' => 'BxStudioFormsCategoriesCheckerHelper'
+            ),
+            'inputs' => array (
+                'id' => array(
+                    'type' => 'hidden',
+                    'name' => 'id',
+                    'value' => isset($aCategory['id']) ? (int)$aCategory['id'] : 0,
+                    'db' => array (
+                        'pass' => 'Int',
+                    ),
+                ),
+                'value' => array(
+                    'type' => 'text',
+                    'name' => 'value',
+                    'caption' => _t('_adm_form_txt_categories_value'),
+                    'info' => '',
+                    'value' => isset($aCategory['value']) ? $aCategory['value'] : '',
+                    'required' => '1',
+                    'db' => array (
+                        'pass' => 'Xss',
+                    ),
+                    'checker' => array (
+                        'func' => 'AvailUnique',
+                        'params' => array(isset($aCategory['id']) ? (int)$aCategory['id'] : 0),
+                        'error' => _t('_adm_form_err_categories_value'),
+                    ),
+                ),
+                'controls' => array(
+                    'name' => 'controls',
+                    'type' => 'input_set',
+                    array(
+                        'type' => 'submit',
+                        'name' => 'do_submit',
+                        'value' => _t('_adm_form_btn_labels_submit'),
+                    ),
+                    array (
+                        'type' => 'reset',
+                        'name' => 'close',
+                        'value' => _t('_adm_form_btn_labels_cancel'),
+                        'attrs' => array(
+                            'onclick' => "$('.bx-popup-applied:visible').dolPopupHide()",
+                            'class' => 'bx-def-margin-sec-left',
+                        ),
+                    )
+                )
+            )
+        );
+        return new BxTemplStudioFormView($aForm);
+    }
+    
+    protected function _addJsCss()
+    {
+        parent::_addJsCss();
+        $this->_oTemplate->addJs(array('jquery.form.min.js', 'forms_categories.js'));
+
+        $oForm = new BxTemplStudioFormView(array());
+        $oForm->addCssJs();
+    }
+    
+    protected function canAdd()
+    {
+        return $this->sModule != '';
+    }
+    
+    protected function _getActionAdd($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
+    {
+        if(!$this->canAdd())
+            $isDisabled = true;
+
+        return parent::_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
+    }
+    
+    protected function _getFilterControls ()
+    {
+        parent::_getFilterControls();
+
+        $aInputModules = $this->getModulesSelectOneArray('getCategories', false, false);
+		foreach($aInputModules['values'] as $sKey => $sValue){
+			$bEnable = false;
+			$oModule = BxDolModule::getInstance($sKey);
+			if (isset($oModule->_oConfig)){
+				$CNF = $oModule->_oConfig->CNF;
+				if (isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] == true){
+					$bEnable = true;
+				}
+			}
+			if (!$bEnable)
+				unset($aInputModules['values'][$sKey]);
+		}
+		$aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
+		$oForm = new BxTemplStudioFormView(array());
+        $sContent = $oForm->genRow($aInputModules);
+        $oForm = new BxTemplStudioFormView(array());
+
+        $aInputSearch = array(
+            'type' => 'text',
+            'name' => 'keyword',
+            'attrs' => array(
+                'id' => 'bx-grid-search-' . $this->_sObject,
+            ),
+            'tr_attrs' => array(
+                'style' => 'display:none;'
+            )
+        );
+        $sContent .= $oForm->genRow($aInputSearch);
+
+        return  $sContent;
+    }
+    
+    function getJsObject()
+    {
+        return 'oBxDolStudioFormsCategories';
+    }
+    
+    function getCode($isDisplayHeader = true)
+    {
+        return $this->_oTemplate->parseHtmlByName('forms_categories.html', array(
+            'content' => parent::getCode($isDisplayHeader),
+            'js_object' => $this->getJsObject(),
+            'grid_object' => $this->_sObject,
+            'params_divider' => $this->sParamsDivider
+        ));
+    }
+}
+
+/** @} */

+ 10 - 6
studio/template/scripts/BxBaseStudioGrid.php

@@ -19,14 +19,12 @@ class BxBaseStudioGrid extends BxDolStudioGrid
     {
         return '';
     }
-
-    public function getModulesSelectOne($sGetItemsMethod, $bShowCustom = true, $bShowSystem = true)
+    
+    public function getModulesSelectOneArray($sGetItemsMethod, $bShowCustom = true, $bShowSystem = true)
     {
         if(empty($sGetItemsMethod))
             return '';
 
-        $oForm = new BxTemplStudioFormView(array());
-
         $aInputModules = array(
             'type' => 'select',
             'name' => 'module',
@@ -41,10 +39,16 @@ class BxBaseStudioGrid extends BxDolStudioGrid
         $aCounter = array();
         $this->oDb->$sGetItemsMethod(array('type' => 'counter_by_modules'), $aCounter, false);
         foreach($aInputModules['values'] as $sKey => $sValue)
-                $aInputModules['values'][$sKey] = $aInputModules['values'][$sKey] . " (" . (isset($aCounter[$sKey]) ? $aCounter[$sKey] : "0") . ")";
+            $aInputModules['values'][$sKey] = $aInputModules['values'][$sKey] . " (" . (isset($aCounter[$sKey]) ? $aCounter[$sKey] : "0") . ")";
+        
+        return $aInputModules;
+    }
 
+    public function getModulesSelectOne($sGetItemsMethod, $bShowCustom = true, $bShowSystem = true)
+    {
+        $aInputModules = $this->getModulesSelectOneArray($sGetItemsMethod, $bShowCustom, $bShowSystem);
         $aInputModules['values'] = array_merge(array('' => _t('_adm_txt_select_module')), $aInputModules['values']);
-
+        $oForm = new BxTemplStudioFormView(array());
         return $oForm->genRow($aInputModules);
     }