BxDolWiki.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * WIKI object.
  11. *
  12. * It's possble to create different WIKI object which will different URLs, Menus and permissions.
  13. * For example it's possible to create
  14. * http://example.com/wiki/somepageshere and http://example.com/cocs/anotherpageshere
  15. *
  16. * @section wiki_create Creating the WIKI object:
  17. *
  18. *
  19. * Add record to 'sys_objects_wiki' table:
  20. *
  21. * - object: name of the WIKI object, this name will be user in URL as well,
  22. * for example, 'wiki' will have URLs like this: http://example.com/wiki/somepageshere
  23. * - title: title of the WIKI, for example, documentation, help, tutorial
  24. * - module: module name WIKI object belongs to
  25. * - allow_add_for_levels: allow to add pages and blocks for this member levels
  26. * - allow_edit_for_levels: allow to edit block for this member levels
  27. * - allow_delete_for_levels: allow to delet pages and blocks for this member levels
  28. * - allow_translate_for_levels: allow to add translations for this member levels
  29. * - override_class_name: user defined class name
  30. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  31. *
  32. * Add record to 'sys_rewrite_rules' table:
  33. * - preg - regular expression which matches some URL
  34. * - service - service method to call if abbe regular expression matches
  35. * - active - active flag
  36. *
  37. * Add record to 'sys_permalinks' table:
  38. * - standard - how link should look like when permalinks are off
  39. * - permalink - how link should look like when permalinks are on,
  40. * some special server configuration may be required to make permalink to work,
  41. * such as 'mod_rewrite' and .htaccess file for Apache
  42. * - check - option name which enables/disables permalinks
  43. * - compare_by_prefix - compare by prefix
  44. */
  45. class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
  46. {
  47. protected $_sObject;
  48. protected $_aObject;
  49. protected $_sLangCateg = 'Wiki';
  50. /**
  51. * Constructor
  52. * @param $aObject array of WIKI options
  53. */
  54. protected function __construct($aObject)
  55. {
  56. parent::__construct();
  57. $this->_sObject = $aObject['object'];
  58. $this->_aObject = $aObject;
  59. $this->_oQuery = new BxDolWikiQuery($aObject);
  60. }
  61. /**
  62. * Get WIKI object instance by object URI
  63. * @param $sObject object name
  64. * @return object instance or false on error
  65. */
  66. static public function getObjectInstanceByUri($sUri, $oTemplate = false)
  67. {
  68. $aObject = BxDolWikiQuery::getWikiObjectByUri($sUri);
  69. if (!$aObject || !is_array($aObject))
  70. return false;
  71. return self::getObjectInstance($aObject['object'], $oTemplate);
  72. }
  73. /**
  74. * Get WIKI object instance by object name
  75. * @param $sObject object name
  76. * @return object instance or false on error
  77. */
  78. static public function getObjectInstance($sObject, $oTemplate = false)
  79. {
  80. if (isset($GLOBALS['bxDolClasses']['BxDolWiki!'.$sObject]))
  81. return $GLOBALS['bxDolClasses']['BxDolWiki!'.$sObject];
  82. $aObject = BxDolWikiQuery::getWikiObject($sObject);
  83. if (!$aObject || !is_array($aObject))
  84. $aObject = BxDolWikiQuery::getWikiObject('system');
  85. $sClass = empty($aObject['override_class_name']) ? 'BxDolWiki' : $aObject['override_class_name'];
  86. if (!empty($aObject['override_class_file']))
  87. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  88. $o = new $sClass($aObject, $oTemplate);
  89. return ($GLOBALS['bxDolClasses']['BxDolWiki!'.$sObject] = $o);
  90. }
  91. /**
  92. * Get object name
  93. */
  94. public function getObjectName ()
  95. {
  96. return $this->_sObject;
  97. }
  98. /**
  99. * Get wiki URi
  100. */
  101. public function getWikiUri ()
  102. {
  103. return $this->_aObject['uri'];
  104. }
  105. /**
  106. * Get WIKI block content
  107. * @param $iBlockId block ID
  108. * @param $sLang optional language name
  109. * @return block content
  110. */
  111. public function getBlockContent ($iBlockId, $sLang = false, $iRevision = false)
  112. {
  113. if (!$sLang)
  114. $sLang = bx_lang_name();
  115. $s = '';
  116. $sControls = '';
  117. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, $iRevision);
  118. $aWikiLatest = $this->_oQuery->getBlockContent ($iBlockId, $sLang);
  119. if ($aWikiVer) {
  120. require_once(BX_DIRECTORY_PATH_PLUGINS . "parsedown/Parsedown.php");
  121. $oParsedown = new Parsedown();
  122. $oParsedown->setSafeMode($aWikiVer['unsafe'] ? false : true);
  123. $s = $oParsedown->text($aWikiVer['content']);
  124. }
  125. if (!$aWikiVer && $aWikiLatest) {
  126. return _t('_sys_wiki_error_no_rev', $iRevision ? $iRevision : 0, $sLang);
  127. }
  128. if (!$aWikiVer && !$aWikiLatest && $this->isAllowed('edit')) {
  129. $s = _t('_sys_wiki_error_no_revs');
  130. }
  131. if ($aWikiVer || (!$aWikiVer && $this->isAllowed('edit'))) {
  132. $sControls = BxDolService::call('system', 'wiki_controls', array($this, $aWikiVer, $aWikiLatest, $iBlockId), 'TemplServiceWiki');
  133. }
  134. return $s . $sControls;
  135. }
  136. /**
  137. * Generate wiki contents - list of all wiki pages
  138. */
  139. public function getContents ($sAllExceptSpecified = '', $sOnlySpecified = '')
  140. {
  141. $aAllExceptSpecified = $sAllExceptSpecified ? explode(',', $sAllExceptSpecified) : array();
  142. $aOnlySpecified = $sOnlySpecified ? explode(',', $sOnlySpecified) : array();
  143. if (!($a = $this->_oQuery->getPages($aAllExceptSpecified, $aOnlySpecified)))
  144. return '';
  145. $aVars = array('bx_repeat:pages' => array());
  146. foreach ($a as $r) {
  147. $oPage = BxDolPage::getObjectInstance($r['object']);
  148. if (!$oPage->isVisiblePage())
  149. continue;
  150. $sUrl = $this->getPageUrl($r['uri']);
  151. $aVars['bx_repeat:pages'][] = array(
  152. 'url' => $sUrl,
  153. 'title' => _t($r['title']),
  154. );
  155. }
  156. usort($aVars['bx_repeat:pages'], function ($r1, $r2) {
  157. return strcmp($r1['title'], $r2['title']);
  158. });
  159. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_contents.html', $aVars);
  160. }
  161. /**
  162. * Generate list of wiki blocks with missing translations for the given language
  163. */
  164. public function getMissingTranslations ($sLang = false)
  165. {
  166. if (!$sLang)
  167. $sLang = bx_lang_name();
  168. if (!$this->isAllowed('edit') && !$this->isAllowed('translate') || !preg_match("/^[a-z]+$/", $sLang))
  169. return _t('_sys_txt_access_denied');
  170. if (!BxDolLanguages::getInstance()->getLangId($sLang))
  171. return _t('_sys_wiki_error_no_such_lang');
  172. $a = $this->_oQuery->getBlocksWithMissingTranslations($sLang);
  173. if (!$a)
  174. return _t('_sys_wiki_no_missing_translations', $sLang);
  175. $aVars = array(
  176. 'title_title' => _t('_sys_wiki_title_title'),
  177. 'title_lang' => _t('_sys_wiki_title_lang_missing'),
  178. 'title_author' => _t('_sys_wiki_title_author', _t('_sys_wiki_orig')),
  179. 'title_content' => _t('_sys_wiki_title_content', _t('_sys_wiki_orig')),
  180. 'title_time' => _t('_sys_wiki_title_time', _t('_sys_wiki_orig')),
  181. 'bx_repeat:blocks' => array()
  182. );
  183. foreach ($a as $iBlockId) {
  184. $aWikiVerMain = $this->_oQuery->getBlockContent ($iBlockId, false);
  185. $aPage = $this->_oQuery->getPageByBlockId($iBlockId);
  186. $sUrl = $this->getPageUrl($aPage['uri']) . '#bx-page-wiki-container-' . $iBlockId;
  187. $oProfile = BxDolProfile::getInstanceMagic($aWikiVerMain['profile_id']);
  188. $aVars['bx_repeat:blocks'][] = array(
  189. 'author_url' => $oProfile->getUrl(),
  190. 'author_name' => $oProfile->getDisplayName(),
  191. 'url' => $sUrl,
  192. 'lang' => $sLang,
  193. 'title' => _t($aPage['title']),
  194. 'content' => strmaxtextlen($aWikiVerMain['content']),
  195. 'timejs' => bx_time_js($aWikiVerMain['added']),
  196. );
  197. }
  198. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_translations_missing.html', $aVars);
  199. }
  200. /**
  201. * Generate list of wiki blocks with outdated translations for the given language
  202. */
  203. public function getOutdatedTranslations ($sLang = false)
  204. {
  205. if (!$sLang)
  206. $sLang = bx_lang_name();
  207. if (!$this->isAllowed('edit') && !$this->isAllowed('translate') || !preg_match("/^[a-z]+$/", $sLang))
  208. return _t('_sys_txt_access_denied');
  209. if (!BxDolLanguages::getInstance()->getLangId($sLang))
  210. return _t('_sys_wiki_error_no_such_lang');
  211. $aVars = array(
  212. 'title_title' => _t('_sys_wiki_title_title'),
  213. 'title_lang' => _t('_sys_wiki_title_lang_outated'),
  214. 'title_author' => _t('_sys_wiki_title_author', _t('_sys_wiki_orig')),
  215. 'title_content' => _t('_sys_wiki_title_content', _t('_sys_wiki_orig')),
  216. 'title_time_main' => _t('_sys_wiki_title_time', _t('_sys_wiki_orig')),
  217. 'title_time' => _t('_sys_wiki_title_time', '"'.$sLang.'"'),
  218. 'bx_repeat:blocks' => array()
  219. );
  220. $a = $this->_oQuery->getBlocksWithOutdatedTranslations($sLang);
  221. if (!$a)
  222. return _t('_sys_wiki_all_translations_up_to_date', $sLang);
  223. foreach ($a as $iBlockId) {
  224. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, false, false);
  225. $aWikiVerMain = $this->_oQuery->getBlockContent ($iBlockId, false);
  226. $aPage = $this->_oQuery->getPageByBlockId($iBlockId);
  227. $sUrl = $this->getPageUrl($aPage['uri']) . '#bx-page-wiki-container-' . $iBlockId;
  228. $oProfile = BxDolProfile::getInstanceMagic($aWikiVerMain['profile_id']);
  229. $aVars['bx_repeat:blocks'][] = array(
  230. 'author_url' => $oProfile->getUrl(),
  231. 'author_name' => $oProfile->getDisplayName(),
  232. 'url' => $sUrl,
  233. 'lang' => $sLang,
  234. 'title' => _t($aPage['title']),
  235. 'content' => strmaxtextlen($aWikiVerMain['content']),
  236. 'timejs_main' => bx_time_js($aWikiVerMain['added']),
  237. 'timejs' => bx_time_js($aWikiVer['added']),
  238. );
  239. }
  240. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_translations_outdated.html', $aVars);
  241. }
  242. /**
  243. * Check if partucular action is allowed
  244. * @param $sType action type: add, edit, delete, translate
  245. * @param $sProfileId profile to check, if not provided then current profile is used
  246. * @return true if action is allowed, false otherwise
  247. */
  248. public function isAllowed ($sType, $iProfileId = false)
  249. {
  250. if ('translate' == $sType) {
  251. $aLangs = BxDolLanguages::getInstance()->getLanguages(false, true);
  252. if (count($aLangs) < 2)
  253. return false;
  254. }
  255. if (isAdmin())
  256. return true;
  257. $aTypes = array(
  258. 'add' => 'allow_add_for_levels',
  259. 'add-page' => 'allow_add_for_levels',
  260. 'edit' => 'allow_edit_for_levels',
  261. 'translate' => 'allow_translate_for_levels',
  262. 'delete-version' => 'allow_delete_for_levels',
  263. 'delete-block' => 'allow_delete_for_levels',
  264. 'get-traaslation' => 'allow_translate_for_levels',
  265. 'history' => isLogged(),
  266. 'unsafe' => 'allow_unsafe_for_levels',
  267. );
  268. if (!isset($aTypes[$sType]))
  269. return false;
  270. if (true === $aTypes[$sType] || false === $aTypes[$sType])
  271. return $aTypes[$sType];
  272. if (!isset($this->_aObject[$aTypes[$sType]]))
  273. return false;
  274. $oAcl = BxDolAcl::getInstance();
  275. return $oAcl->isMemberLevelInSet($this->_aObject[$aTypes[$sType]], $iProfileId);
  276. }
  277. public function actionGetTranslation ()
  278. {
  279. $aWikiVer = $this->_oQuery->getBlockContent ((int)bx_get('block_id'), bx_get('language'), false, false);
  280. if (!$aWikiVer)
  281. return array('code' => 1, 'actions' => 'ShowMsg', 'msg' => 'no translation was found');
  282. else
  283. return array('code' => 0, 'language' => $aWikiVer['language'], 'content' => $aWikiVer['content'], 'block_id' => $aWikiVer['block_id']);
  284. }
  285. public function actionDeleteVersion ()
  286. {
  287. $iBlockId = (int)bx_get('block_id');
  288. $sLang = bx_lang_name();
  289. $oLang = BxDolLanguages::getInstance();
  290. $aVars = $this->getVarsForHistory($iBlockId, $sLang);
  291. $aVars['select_all'] = _t('_Select_all');
  292. if (!$aVars['bx_repeat:revisions'])
  293. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_msg.html', array(
  294. 'close' => _t('_sys_close'),
  295. 'msg' => _t('_sys_wiki_error_no_revs'),
  296. ));
  297. $aForm = array(
  298. 'form_attrs' => array(
  299. 'name' => 'bx-wiki-del-rev',
  300. 'method' => 'post',
  301. ),
  302. 'params' => array (
  303. 'db' => array(
  304. 'submit_name' => 'do_submit',
  305. ),
  306. ),
  307. 'inputs' => array(
  308. 'block_id' => array(
  309. 'type' => 'hidden',
  310. 'name' => 'block_id',
  311. 'value' => $iBlockId,
  312. ),
  313. 'language' => array(
  314. 'type' => 'hidden',
  315. 'name' => 'language',
  316. 'value' => $sLang,
  317. ),
  318. 'revision' => array(
  319. 'type' => 'custom',
  320. 'name' => 'revision',
  321. 'caption' => '',
  322. 'content' => BxDolTemplate::getInstance()->parseHtmlByName('wiki_delete_version.html', $aVars),
  323. ),
  324. 'buttons' => array(
  325. 'type' => 'input_set',
  326. array(
  327. 'type' => 'submit',
  328. 'name' => 'do_submit',
  329. 'value' => _t('_Submit'),
  330. ),
  331. array(
  332. 'type' => 'reset',
  333. 'name' => 'close',
  334. 'value' => _t('_sys_close'),
  335. 'attrs' => array(
  336. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide();",
  337. 'class' => "bx-def-margin-sec-left",
  338. ),
  339. ),
  340. ),
  341. ),
  342. );
  343. $oForm = new BxTemplFormView ($aForm);
  344. $oForm->initChecker();
  345. if ($oForm->isSubmittedAndValid ()) {
  346. $i = $this->_oQuery->deleteRevisions ($iBlockId, $sLang, bx_get('revision'));
  347. return array('code' => 0, 'actions' => array('Reload', 'ClosePopup', 'ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_revisions_deleted', $i));
  348. }
  349. else {
  350. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_form.html', array(
  351. 'form' => $oForm->getCode(),
  352. 'block_id' => $iBlockId,
  353. 'wiki_action_uri' => $this->getWikiUri(),
  354. 'action' => 'delete-version',
  355. ));
  356. }
  357. }
  358. public function actionAddPage ()
  359. {
  360. // validate page URI
  361. $sPageUri = strtolower(bx_get('page'));
  362. if (!preg_match("/^[a-z0-9_-]+$/", $sPageUri))
  363. return _t('_sys_wiki_error_incorrect_page_uri', bx_process_output($sPageUri));
  364. // check if such page exist
  365. $oPage1 = BxDolPage::getObjectInstanceByUri($sPageUri);
  366. $oPage2 = BxDolPage::getObjectInstance($this->_aObject['module'] . str_replace('-', '_', $sPageUri));
  367. if ($oPage1 || $oPage2)
  368. return _t('_sys_wiki_error_page_exists', bx_process_output($sPageUri));
  369. // get lang category where translations will be added
  370. $oLang = BxDolStudioLanguagesUtils::getInstance();
  371. $iLangCat = $oLang->getLanguageCategory($this->_sLangCateg);
  372. if (!$iLangCat)
  373. return _t('_sys_wiki_error_occured', 9);
  374. // set default values for the form
  375. $aValues = array();
  376. $aLangs = $oLang->getLanguages(false, true);
  377. foreach ($aLangs as $sKey => $sLang)
  378. $aValues[$sKey] = ucfirst($sPageUri);
  379. // init form
  380. $aForm = array(
  381. 'form_attrs' => array(
  382. 'name' => 'bx-wiki-create-page',
  383. 'method' => 'post',
  384. ),
  385. 'params' => array (
  386. 'db' => array(
  387. 'submit_name' => 'do_submit',
  388. ),
  389. ),
  390. 'inputs' => array(
  391. 'page' => array(
  392. 'type' => 'hidden',
  393. 'name' => 'page',
  394. 'value' => $sPageUri,
  395. ),
  396. 'title' => array(
  397. 'type' => 'text_translatable',
  398. 'name' => 'title',
  399. 'caption' => _t('_adm_bp_txt_page_title'),
  400. 'info' => _t('_adm_bp_dsc_page_title'),
  401. 'value' => '',
  402. 'values' => $aValues,
  403. 'required' => '1',
  404. 'db' => array (
  405. 'pass' => 'Xss',
  406. ),
  407. 'checker' => array (
  408. 'func' => 'LengthTranslatable',
  409. 'params' => array(3,100, 'title'),
  410. 'error' => _t('_adm_bp_err_page_title'),
  411. ),
  412. ),
  413. 'buttons' => array(
  414. 'type' => 'input_set',
  415. array(
  416. 'type' => 'submit',
  417. 'name' => 'do_submit',
  418. 'value' => _t('_Submit'),
  419. ),
  420. array(
  421. 'type' => 'reset',
  422. 'name' => 'close',
  423. 'value' => _t('_sys_close'),
  424. 'attrs' => array(
  425. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide();",
  426. 'class' => "bx-def-margin-sec-left",
  427. ),
  428. ),
  429. ),
  430. ),
  431. );
  432. $oForm = new BxTemplStudioFormView ($aForm);
  433. $oForm->initChecker();
  434. if ($oForm->isSubmittedAndValid ()) {
  435. // insert page
  436. $sLangKey = '_' . $this->_aObject['module'] . '_' . str_replace('-', '_', $sPageUri) . '_' . time();
  437. $sUrl = $this->getPageUrl($sPageUri, false, false);
  438. $iPageId = $this->_oQuery->insertPage ($sPageUri, $sUrl, $sLangKey);
  439. if (!$iPageId)
  440. return array('code' => 10, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 10));
  441. // insert translations
  442. $aLangs = $oLang->getLanguages(true, true);
  443. foreach ($aLangs as $iLangId => $sLangTitle) {
  444. $sLang = $oLang->getLangName($iLangId);
  445. if (!($sVal = $oForm->getCleanValue('title-' . $sLang)))
  446. continue;
  447. $oLang->addLanguageString($sLangKey, $sVal, $iLangId, $iLangCat);
  448. }
  449. return array('code' => 0, 'url' => BxDolPermalinks::getInstance()->permalink($sUrl));
  450. }
  451. else {
  452. // display form
  453. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_create_page_form.html', array(
  454. 'form' => $oForm->getCode(),
  455. 'wiki_action_uri' => $this->getWikiUri(),
  456. 'action' => 'add-page',
  457. ));
  458. }
  459. }
  460. public function actionAdd ()
  461. {
  462. $iCellId = (int)bx_get('cell_id');
  463. $sPage = bx_get('page');
  464. $oPage = BxDolPage::getObjectInstance($sPage);
  465. if (!$oPage)
  466. return array('code' => 4, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 4));
  467. if ($this->_aObject['module'] != $oPage->getModule())
  468. return array('code' => 5, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_txt_access_denied'));
  469. $iDesignBox = getParam($this->_aObject['module'] . '_design_box');
  470. $aBlock = array(
  471. 'object' => $sPage,
  472. 'cell_id' => $iCellId,
  473. 'module' => $this->_aObject['module'],
  474. 'title_system' => '',
  475. 'title' => '',
  476. 'designbox_id' => false === $iDesignBox ? 0 : $iDesignBox,
  477. 'visible_for_levels' => 2147483647,
  478. 'type' => 'wiki',
  479. 'deletable' => 1,
  480. 'copyable' => 0,
  481. 'active' => 1,
  482. );
  483. $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
  484. if (!($iBlockId = $oQueryPageBuilder->insertBlock($aBlock)))
  485. return array('code' => 6, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 6));
  486. return array('code' => 0, 'block_id' => $iBlockId, 'cell_id' => $iCellId);
  487. }
  488. public function actionDeleteBlock ()
  489. {
  490. $iBlockId = (int)bx_get('block_id');
  491. $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
  492. if (!$oQueryPageBuilder->deleteBlocks(array('type' => 'by_id', 'value' => $iBlockId)))
  493. return array('code' => 3, 'actions' => array('ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_error_occured', 3));
  494. $this->_oQuery->deleteAllRevisions($iBlockId);
  495. return array('code' => 0, 'actions' => array('DeleteBlock', 'ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_block_deleted'));
  496. }
  497. public function actionHistory ()
  498. {
  499. $iBlockId = (int)bx_get('block_id');
  500. $sLang = bx_lang_name();
  501. $oLang = BxDolLanguages::getInstance();
  502. $aVars = $this->getVarsForHistory($iBlockId, $sLang);
  503. $aVars['language'] = $oLang->getLangTitle($oLang->getLangId($sLang));
  504. $aVars['close'] = _t('_sys_close');
  505. $aVars['msg'] = $aVars['bx_repeat:revisions'] ? '' : _t('_sys_wiki_error_no_revs');
  506. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_history.html', $aVars);
  507. }
  508. public function actionTranslate ()
  509. {
  510. return $this->actionEdit (true);
  511. }
  512. public function actionEdit ($bTranslate = false)
  513. {
  514. $iBlockId = (int)bx_get('block_id');
  515. $sMainLangLabel = '';
  516. $aWikiVerMain = array();
  517. $sLangForTranslate = '';
  518. $aLangsForInput = $this->getLangsForInput ($iBlockId, $bTranslate, $sMainLangLabel, $aWikiVerMain, $sLangForTranslate);
  519. // don't allow to translate empty blocks
  520. if (!$aWikiVerMain && $bTranslate)
  521. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_msg.html', array(
  522. 'close' => _t('_sys_close'),
  523. 'msg' => _t('_sys_wiki_error_no_revs'),
  524. ));
  525. // get latest revision for block with current lang
  526. if ($bTranslate) {
  527. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLangForTranslate, false, false);
  528. }
  529. else {
  530. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, bx_lang_name());
  531. }
  532. unset($aWikiVer['notes']); // unset notes since we need this field empty in the form
  533. if (!$aWikiVer) { // check for new block, so initialize with default values
  534. $aWikiVer = array('block_id' => $iBlockId);
  535. if ($bTranslate)
  536. $aWikiVer['language'] = $sLangForTranslate;
  537. else
  538. $aWikiVer['language'] = bx_lang_name();
  539. }
  540. // init form object
  541. $oForm = BxDolForm::getObjectInstance('sys_wiki', $bTranslate ? 'sys_wiki_translate' : 'sys_wiki_edit');
  542. if (!$oForm)
  543. return _t('_sys_wiki_error_occured', 12);
  544. if (isset($oForm->aInputs['language']))
  545. $oForm->aInputs['language']['values'] = $aLangsForInput;
  546. if (isset($oForm->aInputs['content_main']) && $sMainLangLabel) {
  547. $oForm->aInputs['content_main']['caption'] = $sMainLangLabel;
  548. $oForm->aInputs['content_main']['content'] = $aWikiVerMain ? BxDolTemplate::getInstance()->parseHtmlByName('wiki_content.html', array('content' => $aWikiVerMain['content'])) : '';
  549. }
  550. $oForm->initChecker($aWikiVer);
  551. if (!$oForm->isSubmittedAndValid()) {
  552. // display form
  553. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_form.html', array(
  554. 'form' => $oForm->getCode(),
  555. 'block_id' => $iBlockId,
  556. 'wiki_action_uri' => $this->getWikiUri(),
  557. 'action' => $bTranslate ? 'translate' : 'edit',
  558. ));
  559. }
  560. else {
  561. $sLang = $oForm->getCleanValue('language');
  562. // get previous revision with priority for current language
  563. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang);
  564. $bMainLang = $this->getFieldMainLangFlag ($oForm, $sLang, $aWikiVer);
  565. $sRev = $this->getFieldRev ($oForm, $sLang, $aWikiVer);
  566. $bUnsafe = $this->getFieldUnsafeFlag ($oForm, $sLang, $aWikiVer);
  567. // insert new revision (main lang is NOT allowed for translations)
  568. $iTime = time();
  569. if ((!$bMainLang || ($bMainLang && $this->isAllowed('edit'))) && $this->isContentChanged($iBlockId, $sLang, $oForm->getCleanValue('content'))) {
  570. $sId = $oForm->insert(array(
  571. 'added' => $iTime,
  572. 'revision' => $sRev,
  573. 'main_lang' => $bMainLang,
  574. 'profile_id' => bx_get_logged_profile_id(),
  575. 'unsafe' => $bUnsafe,
  576. ));
  577. }
  578. // process translations if available
  579. if (($sTranslations = bx_get('translations')) && ($aTranslations = json_decode($sTranslations, true))) {
  580. foreach ($aTranslations as $sLang => $sContent) {
  581. if ($sLang == $oForm->getCleanValue('language'))
  582. continue;
  583. // get previous revision with priority for current language
  584. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang);
  585. $bMainLang = $this->getFieldMainLangFlag ($oForm, $sLang, $aWikiVer);
  586. $sRev = $this->getFieldRev ($oForm, $sLang, $aWikiVer);
  587. $bUnsafe = $this->getFieldUnsafeFlag ($oForm, $sLang, $aWikiVer);
  588. // insert new revision (main lang is NOT allowed for translations)
  589. if ((!$bMainLang || ($bMainLang && $this->isAllowed('edit'))) && $this->isContentChanged($iBlockId, $sLang, $sContent)) {
  590. $sId = $oForm->insert(array(
  591. 'added' => $iTime,
  592. 'revision' => $sRev,
  593. 'main_lang' => $bMainLang,
  594. 'profile_id' => bx_get_logged_profile_id(),
  595. 'unsafe' => $bUnsafe,
  596. 'language' => $sLang,
  597. 'content' => $sContent,
  598. ));
  599. }
  600. }
  601. }
  602. // update indexing data
  603. $this->updateBlockIndexingData($iBlockId);
  604. return array('code' => 0, 'actions' => array('Reload', 'ClosePopup'), 'block_id' => $iBlockId);
  605. }
  606. }
  607. protected function updateBlockIndexingData($iBlockId)
  608. {
  609. if (!($aLangs = $this->_oQuery->getBlockLangs ($iBlockId)))
  610. return false;
  611. $sText = '';
  612. foreach ($aLangs as $sLang) {
  613. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, false, false);
  614. $sText .= trim(strip_tags($aWikiVer['content'])) . ' ';
  615. }
  616. return $this->_oQuery->updateBlockIndexingData ($iBlockId, $sText);
  617. }
  618. protected function getPageUrl ($sPageUri, $bPermalink = true, $bAddRootUrl = true)
  619. {
  620. $sUrl = ($bAddRootUrl ? BX_DOL_URL_ROOT : '') . 'r.php?_q=' . $this->_aObject['uri'] . '/' . $sPageUri;
  621. return $bPermalink ? BxDolPermalinks::getInstance()->permalink($sUrl) : $sUrl;
  622. }
  623. protected function isContentChanged ($iBlockId, $sLang, $sContent)
  624. {
  625. if (!$sContent) // don't allow revisions with empty content
  626. return false;
  627. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, false, false);
  628. if (!$aWikiVer) // when no previous revision available
  629. return true;
  630. return $sContent != $aWikiVer['content'];
  631. }
  632. protected function getFieldRev ($oForm, $sLang, $aWikiVer) {
  633. // increase revision for particular language or start with first revision
  634. return $aWikiVer && $sLang == $aWikiVer['language'] ? $aWikiVer['revision'] + 1 : 1;
  635. }
  636. protected function getFieldMainLangFlag ($oForm, $sLang, $aWikiVer) {
  637. // detect main language flag
  638. if ($aWikiVer && $sLang == $aWikiVer['language'])
  639. return $aWikiVer['main_lang']; // when revision is increased (language matches) - copy this flag from previous revision
  640. elseif (!$aWikiVer)
  641. return 1; // when it's first revision and no translations - start as main language
  642. else
  643. return 0;
  644. }
  645. protected function getFieldUnsafeFlag ($oForm, $sLang, $aWikiVer) {
  646. if ($aWikiVer && $sLang == $aWikiVer['language'] && !$aWikiVer['unsafe'])
  647. return 0; // copy 'unsafe' from previous revision only when unsafe = 0
  648. else
  649. return $this->isAllowed('unsafe') ? 1 : 0; // in other cases - check permissions
  650. }
  651. protected function getLangsForInput ($iBlockId, $bTranslateForm, &$sMainLangLabel, &$aWikiVerMain, &$sLangForTranslate)
  652. {
  653. // get main lang update time
  654. $aWikiVerMain = $this->_oQuery->getBlockContent ($iBlockId, 'neverhood');
  655. $iUpdatedMainLang = $aWikiVerMain ? $aWikiVerMain['added'] : 0;
  656. // generate values for radio set
  657. $aLangs = BxDolLanguages::getInstance()->getLanguages(false, true);
  658. foreach ($aLangs as $sKey => $sLang) {
  659. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sKey, false, false);
  660. $sMainLang = $aWikiVer && $aWikiVer['main_lang'] ? '★' : '';
  661. $sComment = !$aWikiVer ? _t('_sys_wiki_lang_missing') : bx_time_js($aWikiVer['added']);
  662. if (!$aWikiVer || $iUpdatedMainLang > $aWikiVer['added'])
  663. $aLangs[$sKey] = _t('_sys_wiki_lang_mask_warn', $sLang, $sMainLang, $sComment);
  664. else
  665. $aLangs[$sKey] = _t('_sys_wiki_lang_mask', $sLang, $sMainLang, $sComment);
  666. if ($bTranslateForm && !$aWikiVer['main_lang'] && !$sLangForTranslate) {
  667. $sLangForTranslate = $sKey;
  668. }
  669. if ($bTranslateForm && $aWikiVer['main_lang']) {
  670. $sMainLangLabel = $aLangs[$sKey];
  671. unset($aLangs[$sKey]);
  672. continue;
  673. }
  674. }
  675. return $aLangs;
  676. }
  677. public function getVarsForHistory ($iBlockId, $sLang)
  678. {
  679. $a = $this->_oQuery->getBlockHistory($iBlockId, $sLang);
  680. $aVars = array(
  681. 'bx_repeat:revisions' => array()
  682. );
  683. foreach ($a as $r) {
  684. $oProfile = BxDolProfile::getInstanceMagic($r['profile_id']);
  685. list($sPageLink, $aPageParams) = bx_get_base_url_popup(array($r['block_id'].'rev' => $r['revision']));
  686. $r['author_url'] = $oProfile->getUrl();
  687. $r['author_name'] = $oProfile->getDisplayName();
  688. $r['timejs'] = bx_time_js($r['added']);
  689. $r['rev_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams));
  690. $aVars['bx_repeat:revisions'][] = $r;
  691. }
  692. return $aVars;
  693. }
  694. }
  695. /** @} */