Source for file _cmdtopic.php

Documentation is available at _cmdtopic.php

  1. <?php
  2.  
  3. class midcom_admin_content__cmdtopic {
  4. var $_argv;
  5. var $_contentadm;
  6. var $_topic;
  7. var $_view;
  8. var $_redirect;
  9. var $_config;
  10. var $_adminuser;
  11. var $_poweruser;
  12. var $_topicowner;
  13.  
  14. function midcom_admin_content__cmdtopic ($argv, &$contentadm) {
  15. $this->_argv = $argv;
  16. $this->_contentadm = &$contentadm;
  17. $this->_topic = $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_CONTENTTOPIC);
  18. $this->_view = "index";
  19. $this->_redirect = ""; // This one is _relative_ to the MidCOM Page !!! (e.g. 150/data)
  20. $this->_config = $GLOBALS["midcom_admin_content_topicadmin_config"];
  21. $midgard = $GLOBALS["midcom"]->get_midgard();
  22. $data =& $GLOBALS["view_data"];
  23. $this->_adminuser = false;
  24. $this->_poweruser = false;
  25. $this->_topicowner = false;
  26. if ($midgard->admin) {
  27. $this->_adminuser = true;
  28. } else {
  29. $user = mgd_get_person($midgard->user);
  30. if ($user !== false) {
  31. // This parameter is not set for power users
  32. if ($user->parameter("Interface","Power_User") != "NO")
  33. $this->_poweruser = true;
  34. if (mgd_is_topic_owner($this->_topic->id))
  35. $this->_topicowner = true;
  36. }
  37. }
  38. }
  39.  
  40. function execute () {
  41. debug_push("Content Admin, Topic Command Execute");
  42. $return = true;
  43. if (count($this->_argv) == 0) {
  44. $this->_view = "index";
  45. $return = true;
  46. } else {
  47. switch($this->_argv[0]) {
  48. case "create":
  49. if (! ( $this->_adminuser
  50. || $this->_topicowner && ($this->_poweruser || ! $this->_config["restrict_create"])
  51. )
  52. )
  53. {
  54. $this->_contentadm->errstr = "Permission denied.";
  55. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  56. debug_add("Permission denied in execute/create.", MIDCOM_LOG_INFO);
  57. debug_pop();
  58. return false;
  59. }
  60. debug_pop();
  61. $this->_view = "create";
  62. break;
  63. case "createok":
  64. debug_pop();
  65. $return = $this->_admin_topic_create();
  66. break;
  67. case "delete":
  68. if (! ( $this->_adminuser
  69. || $this->_topicowner && ($this->_poweruser || ! $this->_config["restrict_delete"])
  70. )
  71. )
  72. {
  73. $this->_contentadm->errstr = "Permission denied.";
  74. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  75. debug_add("Permission denied in execute/delete.", MIDCOM_LOG_INFO);
  76. debug_pop();
  77. return false;
  78. }
  79. debug_pop();
  80. $this->_view = "delete";
  81. break;
  82. case "deleteok":
  83. debug_pop();
  84. $return = $this->_admin_topic_delete();
  85. break;
  86. case "edit":
  87. if (! $this->_adminuser && ! $this->_topicowner)
  88. {
  89. $this->_contentadm->errstr = "Permission denied.";
  90. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  91. debug_add("Permission denied in execute/edit.", MIDCOM_LOG_INFO);
  92. debug_pop();
  93. return false;
  94. }
  95. debug_pop();
  96. $this->_view = "edit";
  97. break;
  98. case "editok":
  99. debug_pop();
  100. $return = $this->_admin_topic_edit();
  101. break;
  102. case "score":
  103. if (! $this->_adminuser && ! $this->_topicowner)
  104. {
  105. $this->_contentadm->errstr = "Permission denied.";
  106. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  107. debug_add("Permission denied in execute/score.", MIDCOM_LOG_INFO);
  108. debug_pop();
  109. return false;
  110. }
  111. debug_pop();
  112. $this->_view = "score";
  113. break;
  114. case "scoreok":
  115. debug_pop();
  116. $return = $this->_admin_topic_score();
  117.  
  118. break;
  119. default:
  120. $this->_contentadm->errstr = "Unkown Command in Execute Handler, this should not happen.";
  121. $this->_contentadm->errcode = MIDCOM_ERRCRIT;
  122. debug_add($this->_contentadm->errstr, MIDCOM_LOG_ERROR);
  123. debug_pop();
  124. return false;
  125. }
  126. }
  127. if ($this->_view == "redirect") {
  128. $this->_admin_redirect();
  129. }
  130.  
  131. debug_pop();
  132. return $return;
  133. }
  134. function _admin_redirect() {
  135. $GLOBALS["midcom"]->relocate($this->_contentadm->viewdata["adminprefix"] . $this->_redirect);
  136. // This will exit.
  137. }
  138. function _admin_topic_create() {
  139. debug_push("Data Manager, Topic Command Create");
  140. if (! ( $this->_adminuser
  141. || $this->_topicowner && ($this->_poweruser || ! $this->_config["restrict_create"])
  142. )
  143. )
  144. {
  145. $this->_contentadm->errstr = "Permission denied.";
  146. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  147. debug_add("Permission denied in _admin_topic_create.", MIDCOM_LOG_INFO);
  148. debug_pop();
  149. return false;
  150. }
  151. debug_print_r("We have to create a topic under Topic $this->_topic->id, _REQUEST is:", $_REQUEST, MIDCOM_LOG_DEBUG);
  152.  
  153. $this->_view = "create";
  154.  
  155. if (array_key_exists("f_cancel", $_REQUEST)) {
  156. debug_pop();
  157. $this->_view = "redirect";
  158. $this->_redirect = $this->_topic->id . "/data";
  159. return true;
  160. }
  161.  
  162.  
  163. if (!array_key_exists("f_submit", $_REQUEST)) {
  164. $this->_contentadm->errstr = "The submit button was not in the request data.";
  165. $this->_contentadm->errcode = MIDCOM_ERRCRIT;
  166. debug_add($this->_contentadm->errstr, MIDCOM_LOG_ERROR);
  167. debug_pop();
  168. return false;
  169. }
  170.  
  171. if (trim($_REQUEST["f_title"]) == "") {
  172. $this->_contentadm->msg = "Error: Title was empty";
  173. debug_pop();
  174. debug_add("Cancelled topic creation, Title was empty.", MIDCOM_LOG_INFO);
  175. return true;
  176. }
  177. if (mgd_get_topic_by_name($this->_topic->id, $_REQUEST["f_name"])) {
  178. $this->_contentadm->msg = "Error: a Topic with this name already exists.";
  179. debug_add("A topic with this name already exists.", MIDCOM_LOG_INFO);
  180. debug_pop();
  181. return true;
  182. }
  183.  
  184. if (trim($_REQUEST["f_name"]) == "")
  185. {
  186. // No URL name given, generate from title
  187. $name = midcom_generate_urlname_from_string($_REQUEST['f_title']);
  188. } else {
  189. $name = midcom_generate_urlname_from_string($_REQUEST['f_name']);
  190. }
  191. $newid = mgd_create_topic ($this->_topic->id, $name, "", $_REQUEST["f_title"], $_REQUEST["f_owner"], "");
  192. if (!$newid) {
  193. $this->_contentadm->msg = "Could not create Topic: " . mgd_errstr();
  194. debug_add("Could not create topic: " . mgd_errstr(), MIDCOM_LOG_WARN);
  195. debug_pop();
  196. return true;
  197. }
  198. $topic = mgd_get_topic ($newid);
  199. $topic->parameter("midcom","component",$_REQUEST["f_type"]);
  200. if (!is_null($this->_config))
  201. {
  202. $topic->parameter("midcom","style",$this->_config["components"][$_REQUEST["f_type"]]["default style"]);
  203. }
  204. $topic->parameter("midcom.helper.nav","navorder",$_REQUEST["f_navorder"]);
  205. $this->_view = "redirect";
  206. $this->_redirect = $newid . "/data";
  207. // We have to invalidate the current topic (so that it can be reread with the correct
  208. // childs), *not* the newly created topic (which won't be in any cache anyway, as it
  209. // has just been created with a new GUID...
  210. $GLOBALS['midcom']->cache->invalidate($this->_topic->guid());
  211.  
  212. debug_pop();
  213. return true;
  214. }
  215.  
  216. function _admin_topic_delete ()
  217. {
  218. debug_push_class(__CLASS__, __FUNCTION__);
  219.  
  220. if (! ( $this->_adminuser
  221. || $this->_topicowner
  222. && (
  223. $this->_poweruser
  224. || ! $this->_config["restrict_delete"])))
  225. {
  226. $this->_contentadm->errstr = "Permission denied.";
  227. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  228. debug_add("Permission denied in _admin_topic_delete.", MIDCOM_LOG_INFO);
  229. debug_pop();
  230. return false;
  231. }
  232. $this->_view = "delete";
  233.  
  234. if (array_key_exists("f_cancel", $_REQUEST))
  235. {
  236. debug_pop();
  237. $this->_view = "redirect";
  238. $this->_redirect = $this->_topic->id . "/data";
  239. return true;
  240. }
  241.  
  242. if (!array_key_exists("f_submit", $_REQUEST))
  243. {
  244. $this->_contentadm->errstr = "The submit button was not in the request data.";
  245. $this->_contentadm->errcode = MIDCOM_ERRCRIT;
  246. debug_add($this->_contentadm->errstr, MIDCOM_LOG_ERROR);
  247. debug_pop();
  248. return false;
  249. }
  250. debug_add("We have to delete topic {$this->_topic->name} [{$this->_topic->id}]");
  251. $this->_delete_topic_update_index();
  252. $articles = mgd_list_topic_articles($this->_topic->id);
  253. if ($articles)
  254. {
  255. while ($articles->fetch())
  256. {
  257. $article = mgd_get_article($articles->id);
  258. if (!mgd_delete_extensions($article))
  259. {
  260. debug_add("Could not delete Article {$article->id} Extensions: " . mgd_errstr(), MIDCOM_LOG_ERROR);
  261. $this->_contentadm->msg = "Error: Could not delete Topic contents: " . mgd_errstr();
  262. return false;
  263. }
  264. if (!$article->delete())
  265. {
  266. debug_add("Could not delete Article {$article->id}:" . mgd_errstr(), MIDCOM_LOG_ERROR);
  267. $this->_contentadm->msg = "Error: Could not delete Topic contents: " . mgd_errstr();
  268. return false;
  269. }
  270. }
  271. }
  272. if (!mgd_delete_extensions($this->_topic))
  273. {
  274. debug_add("Could not delete Topic {$this->_topic->id} Extensions: " . mgd_errstr(), MIDCOM_LOG_ERROR);
  275. $this->_contentadm->msg = "Error: Could not delete Topic contents: " . mgd_errstr();
  276. return false;
  277. }
  278. $newtopic = $this->_topic->up;
  279. $guid = $this->_topic->guid();
  280. if (!$this->_topic->delete())
  281. {
  282. debug_add("Could not delete Topic {$this->_topic->id}: " . mgd_errstr(), MIDCOM_LOG_ERROR);
  283. $this->_contentadm->msg = "Error: Could not delete Topic contents: " . mgd_errstr();
  284. return false;
  285. }
  286.  
  287. $this->_view = "redirect";
  288. $this->_redirect = $newtopic . "/data";
  289. // Invalidate everything since we operate recursive here.
  290. $GLOBALS['midcom']->cache->invalidate_all();
  291. debug_pop();
  292. return true;
  293. }
  294.  
  295. function _delete_topic_update_index()
  296. {
  297. if ($GLOBALS['midcom_config']['indexer_backend'] === false)
  298. {
  299. // Indexer is not configured.
  300. return;
  301. }
  302.  
  303. debug_push_class(__CLASS__, __FUNCTION__);
  304. debug_add("Dropping all NAP registered objects from the index.");
  305. // First we collect everything we have to delete, this might take a while
  306. // so we keep an eye on the script timeout.
  307. $guids = Array();
  308. $nap = new midcom_helper_nav();
  309. $node_list = Array($nap->get_current_node());
  310. while (count($node_list) > 0)
  311. {
  312. set_time_limit(30);
  313. // Add the node being processed.
  314. $nodeid = array_shift($node_list);
  315. debug_add("Processing node {$nodeid}");
  316. $node = $nap->get_node($nodeid);
  317. $guids[] = $node[MIDCOM_NAV_GUID];
  318. debug_add("Processing leaves of node {$nodeid}");
  319. $leaves = $nap->list_leaves($nodeid, true);
  320. debug_add("Got " . count($leaves) . " leaves.");
  321. foreach ($leaves as $leafid)
  322. {
  323. $leaf = $nap->get_leaf($leafid);
  324. $guids[] = $leaf[MIDCOM_NAV_GUID];
  325. }
  326. debug_add("Loading subnodes");
  327. $node_list = array_merge($node_list, $nap->list_nodes($nodeid, true));
  328. debug_print_r("Remaining node queue", $node_list);
  329. }
  330. debug_add("We have to delete " . count($guids) . " objects from the index.");
  331. // Now we go over the entire index and delete the corresponding objects.
  332. // We load all attachments of the corresponding objects as well, to have
  333. // them deleted too.
  334. //
  335. // Again we keep an eye on the script timeout.
  336. $indexer =& $GLOBALS['midcom']->get_service('indexer');
  337. foreach ($guids as $guid)
  338. {
  339. set_time_limit(60);
  340. $object = mgd_get_object_by_guid($guid);
  341. $atts = $object->listattachments();
  342. if ($atts)
  343. {
  344. while ($atts->fetch())
  345. {
  346. debug_add("Deleting attachment {$atts->id} from the index.");
  347. $indexer->delete($atts->guid());
  348. }
  349. }
  350. debug_add("Deleting guid {$guid} from the index.");
  351. $indexer->delete($guid);
  352. }
  353. debug_pop();
  354. }
  355. function _admin_topic_edit() {
  356. debug_push("Data Manager, Topic Command Edit");
  357. if (! $this->_adminuser && ! $this->_topicowner)
  358. {
  359. $this->_contentadm->errstr = "Permission denied.";
  360. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  361. debug_add("Permission denied in _admin_topic_edit.", MIDCOM_LOG_INFO);
  362. debug_pop();
  363. return false;
  364. }
  365. debug_print_r("We have to edit Topic $this->_topic->id, _REQUEST is:", $_REQUEST, MIDCOM_LOG_DEBUG);
  366. $this->_view = "edit";
  367. // do some validation
  368. if (array_key_exists("f_cancel", $_REQUEST)) {
  369. debug_pop();
  370. $this->_view = "redirect";
  371. $this->_redirect = $this->_topic->id . "/data";
  372. return true;
  373. }
  374. if (!array_key_exists("f_submit", $_REQUEST)) {
  375. $this->_contentadm->errstr = "The submit button was not in the request data.";
  376. $this->_contentadm->errcode = MIDCOM_ERRCRIT;
  377. debug_add($this->_contentadm->errstr);
  378. debug_pop();
  379. return false;
  380. }
  381. if (trim($_REQUEST["f_name"]) == "") {
  382. $this->_contentadm->msg = "Error: URL name was empty.";
  383. debug_add("Cancelled topic creation, Name was empty.", MIDCOM_LOG_INFO);
  384. debug_pop();
  385. return true;
  386. }
  387. if (trim($_REQUEST["f_title"]) == "") {
  388. $this->_contentadm->msg = "Error: Title was empty.";
  389. debug_pop();
  390. debug_add("Cancelled topic creation, Title was empty.", MIDCOM_LOG_INFO);
  391. return true;
  392. }
  393. if (mgd_get_topic_by_name($this->_topic->id, $_REQUEST["f_name"])) {
  394. $this->_contentadm->msg = "Error: a Topic with this name already exists.";
  395. debug_add("A topic with this name already exists.", MIDCOM_LOG_INFO);
  396. debug_pop();
  397. return true;
  398. }
  399. // store form data in topic object
  400. // FIXME: nap hidden
  401. $this->_topic->name = midcom_generate_urlname_from_string($_REQUEST["f_name"]); // name
  402. $this->_topic->extra = $_REQUEST["f_title"]; // title
  403. $this->_topic->score = $_REQUEST["f_score"]; // Score
  404. $this->_topic->owner = $_REQUEST["f_owner"]; // Owner
  405. $this->_topic->parameter("midcom", "style", $_REQUEST["f_style"]); // style
  406. $this->_topic->parameter("midcom.helper.nav", "navorder", $_REQUEST["f_navorder"]); // sort
  407. if (array_key_exists("f_style_inherit", $_REQUEST) && $_REQUEST["f_style_inherit"] == "on")
  408. $this->_topic->parameter("midcom", "style_inherit", "true");
  409. else
  410. $this->_topic->parameter("midcom", "style_inherit", "");
  411. /* Viewer Groups: First clear everything, then store the new selection */
  412. $params = $this->_topic->listparameters("ViewerGroups");
  413. if ($params)
  414. while ($params->fetch())
  415. $this->_topic->parameter("ViewerGroups", $params->name, "");
  416.  
  417. if ( ! in_array("all", $_REQUEST["f_viewer_groups"])
  418. && count ($_REQUEST["f_viewer_groups"]) > 0)
  419. {
  420. foreach ($_REQUEST["f_viewer_groups"] as $guid)
  421. $this->_topic->parameter("ViewerGroups", $guid, $guid);
  422. }
  423. if (! $this->_topic->update()) {
  424. $this->_contentadm->msg = "Could not save Topic: " . mgd_errstr();
  425. debug_add("Could not update topic: " . mgd_errstr(), MIDCOM_LOG_WARN);
  426. debug_pop();
  427. return true;
  428. }
  429.  
  430. $this->_view = "redirect";
  431. $this->_redirect = $this->_topic->id . "/data";
  432. $GLOBALS['midcom']->cache->invalidate($this->_topic->guid());
  433.  
  434. debug_pop();
  435. return true;
  436. }
  437.  
  438. function _admin_topic_score() {
  439. debug_push("Data Manager, Topic Command Score");
  440. /*
  441. if (! $this->_adminuser && ! $this->_topicowner)
  442. {
  443. $this->_contentadm->errstr = "Permission denied.";
  444. $this->_contentadm->errcode = MIDCOM_ERRFORBIDDEN;
  445. debug_add("Permission denied in _admin_topic_score.", MIDCOM_LOG_INFO);
  446. debug_pop();
  447. return false;
  448. }
  449. */
  450. debug_print_r("We have to order Topic $this->_topic->id, _REQUEST is:", $_REQUEST, MIDCOM_LOG_DEBUG);
  451. $this->_view = "score";
  452.  
  453. // done, show leave score admin, show content admin
  454. if (array_key_exists("f_finish_nosave", $_REQUEST)) {
  455. $this->_view = "redirect";
  456. $this->_redirect = $this->_topic->id . "/data";
  457. debug_pop();
  458. return true;
  459. } elseif ( array_key_exists("f_finish_save", $_REQUEST)) {
  460. $this->_view = "redirect";
  461. $this->_redirect = $this->_topic->id . "/data";
  462. }
  463. $items = explode('|', $_REQUEST['topicresult']); // teh resultset is like this: <guid>|<guid>|||
  464. for ($i = 0; $i < (count($items)-1 ); $i++) {
  465. if ($items[$i] != '') { /* this tests for empty || in the resultset. This may occur. */
  466. $obj = mgd_get_object_by_guid($items[$i]);
  467. if ($obj) {
  468. $obj->setscore($i);
  469. $GLOBALS['midcom']->cache->invalidate($items[$i]);
  470. }
  471. }
  472. }
  473.  
  474. $items = explode('|', $_REQUEST['articleresult']);
  475. for ($i = 0; $i < (count($items)-1 ); $i++) {
  476. if ($items[$i] != '') {
  477. $obj = mgd_get_object_by_guid($items[$i]);
  478. if ($obj) {
  479. $obj->setscore($i);
  480. $GLOBALS['midcom']->cache->invalidate($items[$i]);
  481. }
  482. }
  483. }
  484. debug_pop();
  485. return true;
  486. }
  487.  
  488. function show () {
  489. eval("\$this->_show_$this->_view();");
  490. }