BxDolWiki.php 32 KB

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