Alex Trofimov 6 лет назад
Родитель
Сommit
4c33bf040e

+ 118 - 4
inc/classes/BxDolWiki.php

@@ -47,6 +47,7 @@ class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
 {
     protected $_sObject;
     protected $_aObject;
+    protected $_sLangCateg = 'Wiki';
 
     /**
      * Constructor
@@ -170,6 +171,7 @@ class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
 
         $aTypes = array(
             'add' => 'allow_add_for_levels',
+            'add-page' => 'allow_add_for_levels',
             'edit' => 'allow_edit_for_levels',
             'translate' => 'allow_translate_for_levels',
             'delete-version' => 'allow_delete_for_levels',
@@ -279,13 +281,125 @@ class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
         }
     }
 
+    public function actionAddPage ()
+    {
+        // validate page URI
+        $sPageUri = strtolower(bx_get('page'));
+        if (!preg_match("/^[a-z0-9_-]+$/", $sPageUri))
+            return _t('_sys_wiki_error_incorrect_page_uri', bx_process_output($sPageUri));
+
+        // check if such page exist
+        $oPage1 = BxDolPage::getObjectInstanceByUri($sPageUri);
+        $oPage2 = BxDolPage::getObjectInstance($this->_aObject['module'] . str_replace('-', '_', $sPageUri));
+        if ($oPage1 || $oPage2)
+            return _t('_sys_wiki_error_page_exists', bx_process_output($sPageUri));
+
+        // get lang category where translations will be added
+        $oLang = BxDolStudioLanguagesUtils::getInstance();
+        $iLangCat = $oLang->getLanguageCategory($this->_sLangCateg);
+        if (!$iLangCat)
+            return _t('_sys_wiki_error_occured', 9);
+
+        // set default values for the form
+        $aValues = array();
+        $aLangs = $oLang->getLanguages(false, true);
+        foreach ($aLangs as $sKey => $sLang)
+            $aValues[$sKey] = ucfirst($sPageUri);
+
+        // init form
+        $aForm = array(
+            'form_attrs' => array(
+                'name' => 'bx-wiki-create-page',
+                'method' => 'post',
+            ),
+            'params' => array (
+                'db' => array(
+                    'submit_name' => 'do_submit',
+                ),
+            ),
+            'inputs' => array(
+                'page' => array(
+                    'type' => 'hidden',
+                    'name' => 'page',
+                    'value' => $sPageUri,
+                ),
+                'title' => array(
+                    'type' => 'text_translatable',
+                    'name' => 'title',
+                    'caption' => _t('_adm_bp_txt_page_title'),
+                    'info' => _t('_adm_bp_dsc_page_title'),
+                    'value' => '',
+                    'values' => $aValues,
+                    'required' => '1',
+                    'db' => array (
+                        'pass' => 'Xss',
+                    ),
+                    'checker' => array (
+                        'func' => 'LengthTranslatable',
+                        'params' => array(3,100, 'title'),
+                        'error' => _t('_adm_bp_err_page_title'),
+                    ),
+                ),
+                'buttons' => array(
+                    'type' => 'input_set',
+                    array(
+                        'type' => 'submit',
+                        'name' => 'do_submit',
+                        'value' => _t('_Submit'),
+                    ),
+                    array(
+                        'type' => 'reset',
+                        'name' => 'close',
+                        'value' => _t('_sys_close'),
+                        'attrs' => array(
+                            'onclick' => "$('.bx-popup-applied:visible').dolPopupHide();",
+                            'class' => "bx-def-margin-sec-left",
+                        ),
+                    ),
+                ),
+            ),
+        );
+
+        $oForm = new BxTemplStudioFormView ($aForm);
+        $oForm->initChecker();
+
+        if ($oForm->isSubmittedAndValid ()) {
+
+            // insert page
+            $sLangKey = '_' . $this->_aObject['module'] . '_' . str_replace('-', '_', $sPageUri) . '_' . time();
+            $sUrl = 'r.php?_q=' . $this->_aObject['uri'] . '/' . $sPageUri;
+            $iPageId = $this->_oQuery->insertPage ($sPageUri, $sUrl, $sLangKey);
+            if (!$iPageId)
+                return array('code' => 10, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 10));
+
+            // insert translations
+            $aLangs = $oLang->getLanguages(true, true);
+            foreach ($aLangs as $iLangId => $sLangTitle) {
+                $sLang = $oLang->getLangName($iLangId);
+                if (!($sVal = $oForm->getCleanValue('title-' . $sLang)))
+                    continue;
+                $oLang->addLanguageString($sLangKey, $sVal, $iLangId, $iLangCat);
+            }
+
+            return array('code' => 0, 'url' => BxDolPermalinks::getInstance()->permalink($sUrl));
+        }
+        else {
+            // display form
+            return BxDolTemplate::getInstance()->parseHtmlByName('wiki_create_page_form.html', array(
+                'form' => $oForm->getCode(),
+                'wiki_action_uri' => $this->getWikiUri(),
+                'action' => 'add-page',
+            ));
+        }
+    }
+
     public function actionAdd ()
     {
         $iCellId = (int)bx_get('cell_id');
         $sPage = bx_get('page');
         $oPage = BxDolPage::getObjectInstance($sPage);
         if (!$oPage)
-            return array('code' => 4, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_txt_error_occured'));
+            return array('code' => 4, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 4));
 
         if ($this->_aObject['module'] != $oPage->getModule())
             return array('code' => 5, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_txt_access_denied'));
@@ -306,7 +420,7 @@ class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
         );
         $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
         if (!($iBlockId = $oQueryPageBuilder->insertBlock($aBlock)))
-            return array('code' => 6, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_txt_error_occured'));
+            return array('code' => 6, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 6));
 
         return array('code' => 0, 'block_id' => $iBlockId, 'cell_id' => $iCellId);
     }
@@ -317,7 +431,7 @@ class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
 
         $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
         if (!$oQueryPageBuilder->deleteBlocks(array('type' => 'by_id', 'value' => $iBlockId)))
-            return array('code' => 3, 'actions' => array('ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_txt_error_occured'));
+            return array('code' => 3, 'actions' => array('ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_error_occured', 3));
 
         $this->_oQuery->deleteAllRevisions($iBlockId);
 
@@ -376,7 +490,7 @@ class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
         // init form object
         $oForm = BxDolForm::getObjectInstance('sys_wiki', $bTranslate ? 'sys_wiki_translate' : 'sys_wiki_edit');
         if (!$oForm)
-            return _t('_sys_txt_error_occured');
+            return _t('_sys_wiki_error_occured', 12);
 
         if (isset($oForm->aInputs['language']))
             $oForm->aInputs['language']['values'] = $aLangsForInput;

+ 35 - 0
inc/classes/BxDolWikiQuery.php

@@ -92,6 +92,41 @@ class BxDolWikiQuery extends BxDolDb
         return $i;
     }
 
+    public function insertPage ($sUri, $sUrl, $sTitleLangKey, $iType = 1, $iLayoutId = 5, $iVisibleForLevels = 2147483647, $sClass = 'BxTemplPageWiki')
+    {
+        $b = $this->query('INSERT INTO `sys_objects_page` SET
+            `object` = :obj,
+            `uri` = :uri,
+            `title` = :title,
+            `module` = :module,
+            `cover` = :cover,
+            `type_id` = :type,
+            `layout_id` = :layout,
+            `visible_for_levels` = :levels, 
+            `visible_for_levels_editable` = 1,
+            `url` = :url,
+            `cache_lifetime` = 0,
+            `cache_editable` = 1,
+            `deletable` = 1,
+            `override_class_name` = :class
+        ', array(
+            'obj' => $this->_aObject['module'] . '_' . str_replace('-', '_', $sUri),
+            'uri' => $sUri,
+            'title' => $sTitleLangKey,
+            'module' => $this->_aObject['module'],
+            'cover' => 0,
+            'type' => $iType,
+            'layout' => $iLayoutId,
+            'levels' => $iVisibleForLevels,
+            'url' => $sUrl,
+            'class' => $sClass,
+        ));
+        if (!$b)
+            return false;
+
+        return $this->lastId();
+    }
+
     public static function deleteAllRevisions ($mixedBlockIds)
     {
         $oDb = BxDolDb::getInstance();

+ 4 - 1
modules/boonex/english/data/langs/system/en.xml

@@ -3116,6 +3116,8 @@ If it is not enabled then please consider implement this optimization, since it
 	<string name="_sys_wiki_error_no_revs"><![CDATA[There are no revisions available for this block]]></string>
 	<string name="_sys_wiki_error_missing_wiki_object"><![CDATA[Wiki object ({0}) wasn't found]]></string>
     <string name="_sys_wiki_error_action_not_allowed"><![CDATA[Wiki object ({1}) action ({0}) isn't allowed]]></string>
+    <string name="_sys_wiki_error_incorrect_page_uri"><![CDATA["{0}" isn't correct page URI]]></string>
+    <string name="_sys_wiki_error_occured"><![CDATA[Error occured (code:{0})]]></string>
     <string name="_sys_wiki_view_rev"><![CDATA[Version <b>{0}</b> by <a href="{1}">{2}</a>, {3}]]></string>
     <string name="_sys_wiki_lang_mask"><![CDATA[{0} {1} <span class="bx-def-font-grayed bx-def-font-small">{2}</span>]]></string>
     <string name="_sys_wiki_lang_mask_warn"><![CDATA[{0} {1} <span class="bx-def-font-small" style="color:red;">{2}</span>]]></string>
@@ -3124,5 +3126,6 @@ If it is not enabled then please consider implement this optimization, since it
     <string name="_sys_wiki_confirm_block_deletion"><![CDATA[Are you sure you want to permanently delete this block with all revisions and translations?]]></string>
     <string name="_sys_wiki_block_deleted"><![CDATA[Block was successfully deleted]]></string>
     <string name="_sys_wiki_add_block"><![CDATA[Add Block]]></string>
-	
+	<string name="_sys_wiki_add_page"><![CDATA[Add Page]]></string>
+	<string name="_sys_wiki_add_page_text"><![CDATA[Such wiki page doesn't exists.]]></string>
 </resources>

+ 4 - 1
modules/boonex/russian/data/langs/system/ru.xml

@@ -3096,6 +3096,8 @@
     <string name="_sys_wiki_error_no_revs"><![CDATA[Нет ревизий для данного блока]]></string>
     <string name="_sys_wiki_error_missing_wiki_object"><![CDATA[Вики объект ({0}) не найден]]></string>
     <string name="_sys_wiki_error_action_not_allowed"><![CDATA[Не разрешенное действие ({0}) для вики объекта ({1})]]></string>
+    <string name="_sys_wiki_error_incorrect_page_uri"><![CDATA["{0}" - неправильная ссылка на страницу]]></string>
+    <string name="_sys_wiki_error_occured"><![CDATA[Ошибка (код:{0})]]></string>
     <string name="_sys_wiki_view_rev"><![CDATA[Версия <b>{0}</b> от <a href="{1}">{2}</a>, {3}]]></string>
     <string name="_sys_wiki_lang_mask"><![CDATA[{0} {1} <span class="bx-def-font-grayed bx-def-font-small">{2}</span>]]></string>
     <string name="_sys_wiki_lang_mask_warn"><![CDATA[{0} {1} <span class="bx-def-font-small" style="color:red;">{2}</span>]]></string>
@@ -3104,5 +3106,6 @@
     <string name="_sys_wiki_confirm_block_deletion"><![CDATA[Подтвердите удаление блока со всей историей и переводами?]]></string>
     <string name="_sys_wiki_block_deleted"><![CDATA[Блок был успешно удален]]></string>
     <string name="_sys_wiki_add_block"><![CDATA[Добавить блок]]></string>
-
+	<string name="_sys_wiki_add_page"><![CDATA[Добавить страницу]]></string>
+	<string name="_sys_wiki_add_page_text"><![CDATA[Такой вики страницы не существует.]]></string>
 </resources>

+ 7 - 0
template/css/wiki.css

@@ -43,3 +43,10 @@
     border:1px dashed #aaa; 
     text-align:center;
 }
+.bx-wiki-create-page {
+    text-align:center;
+}
+.bx-wiki-create-page button {
+    float:none;
+    display:inline-block;
+}

+ 16 - 5
template/scripts/BxBaseServiceWiki.php

@@ -49,11 +49,21 @@ class BxBaseServiceWiki extends BxDol
 
             if ($oWiki->isAllowed('add')) {
                 $oPage = BxDolPage::getObjectInstanceByURI($sUri);
-                if ($oPage) {
-                    $oTemplate = BxDolTemplate::getInstance();
+                $oTemplate = BxDolTemplate::getInstance();
+                if ($oPage) {                    
                     $oTemplate->displayErrorOccured(_t("_sys_wiki_error_page_exists", bx_process_output($sUri)));
                 } else {
-                    echo "TODO: wiki - suggest to create page with specified URI. Display form where user can enter title(text input), text(plain textrea), languale (selectbox with currect site languages with current one pre-selected), revision comments (text input), title - will be page and first block title, text - will be content for first block. Default layout is page with one column and block without borders and title.";
+                    $this->_addCssJs (true);
+                    $s = $oTemplate->parseHtmlByName('wiki_create_page.html', array(
+                        'page_uri' => bx_process_output($sUri),
+                        'action_uri' => $oWiki->getWikiUri(),
+                        'create_page' => _t('_sys_wiki_add_page'),
+                        'text' => _t('_sys_wiki_add_page_text'),
+                    ));
+                    $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
+                    $oTemplate->setPageHeader (_t('_sys_wiki_add_page'));
+                    $oTemplate->setPageContent ('page_main_code', $s);
+                    $oTemplate->getPageCode();
                 }
             } 
             else {
@@ -137,7 +147,6 @@ class BxBaseServiceWiki extends BxDol
             'obj' => $oWikiObject->getObjectName(),
             'block_id' => $sBlockId,
             'menu' => $oMenu->getCode(),
-            // 'TODO: On right - Last modified time and List of missing and outdated translations. History and Last modified time should be controlled by regular menu privacy, while other actions should have custom privacy for particular wiki object',
             'info' => $sInfo,
             'options' => json_encode(array(
                 'block_id' => $sBlockId,
@@ -202,7 +211,7 @@ class BxBaseServiceWiki extends BxDol
         return $a;
     }
 
-    protected function _addCssJs ()
+    protected function _addCssJs ($bAddPage = false)
     {
         if ($this->_bJsCssAdded)
             return false;
@@ -210,6 +219,8 @@ class BxBaseServiceWiki extends BxDol
         $o = BxDolTemplate::getInstance();
         $o->addCss('wiki.css');
         $o->addJs('BxDolWiki.js');
+        if ($bAddPage)
+            $o->addJs('studio/js/|forms.js');
         $this->_bJsCssAdded = true;
     }
 }

+ 19 - 0
template/wiki_create_page.html

@@ -0,0 +1,19 @@
+<div class="bx-wiki-create-page bx-def-margin-top">
+    __text__
+    <br />
+    <button class="bx-btn bx-def-margin-topbottom" onclick="bx_wiki_add_page(this, '__page_uri__', '__action_uri__')">__create_page__</button>
+</div>
+<script>
+function bx_wiki_add_page(e, sPageUri, sActionUri) {
+
+    var sActionUrl = bx_append_url_params('r.php?_q=' + sActionUri + '-action/' + 'add-page', {
+        page: encodeURIComponent(sPageUri.replace(/[\s]+/, '-')),
+    });
+
+    // remove previous popup
+    $(".bx-wiki-create-page-form").parents(".bx-popup-wrapper").remove();
+
+    // show new popup
+    $(window).dolPopupAjax({url: sUrlRoot + sActionUrl, closeOnOuterClick: false});
+}
+</script>

+ 28 - 0
template/wiki_create_page_form.html

@@ -0,0 +1,28 @@
+<div id="bx-wiki-create-page-form" class="bx-wiki-create-page-form">
+    __form__
+</div>
+<script>
+    $(document).ready(function () {
+        $("#bx-wiki-create-page-form form").ajaxForm({
+            url: sUrlRoot + 'r.php?_q=__wiki_action_uri__-action/__action__',
+            beforeSubmit: function (formData, $form, options) {
+                bx_loading($("#bx-wiki-create-page-form form"), true);
+            },
+            success: function (data) {
+                if ('{' == data[0]) {
+                    var oResponce = JSON.parse(data);
+                    if (oResponce.code) {
+                        $('.bx-popup-applied:visible').dolPopupHide();
+                        bx_alert(oResponce.msg);
+                    } 
+                    else {
+                        document.location.href = sUrlRoot + oResponce.url;
+                    }
+                }
+                else {
+                    $("#bx-wiki-create-page-form").parent().html(data).bxProcessHtml();
+                }
+            }
+        });
+    });
+</script>