BxDolWiki.php 32 KB

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