Source for file admin.php

Documentation is available at admin.php

  1. <?php
  2.  
  3. class net_nemein_bookmarks_admin
  4. {
  5. var $_debug_prefix;
  6. var $_config;
  7. var $_config_dm;
  8. var $_topic; /* Data Topic */
  9. var $_config_topic; /* Config Topic */
  10. var $_view;
  11. var $_article;
  12. var $_attachment;
  13. var $_layout;
  14. var $_l10n;
  15. var $_l10n_midcom;
  16. var $errcode;
  17. var $errstr;
  18. var $_local_toolbar;
  19. var $_topic_toolbar;
  20. var $_tag;
  21. var $schemadb_index;
  22.  
  23. function net_nemein_bookmarks_admin($topic, $config)
  24. {
  25. $this->_debug_prefix = "net.nemein.bookmarks admin::";
  26. $this->_config = $config;
  27. $this->_config_dm = null;
  28. $this->_config_topic = $topic;
  29. $this->_view = "";
  30. $this->_article = false;
  31. $this->_attachment = false;
  32. $this->_layout = false;
  33. $i18n =& $GLOBALS["midcom"]->get_service("i18n");
  34. $this->_l10n = $i18n->get_l10n("net.nemein.bookmarks");
  35. $this->_l10n_midcom = $i18n->get_l10n("midcom");
  36. $this->errcode = MIDCOM_ERROK;
  37. $this->errstr = "";
  38. $this->_check_for_content_topic();
  39. $this->_local_toolbar =& $GLOBALS['midcom_admin_content_toolbar_component'];
  40. $this->_topic_toolbar =& $GLOBALS['midcom_admin_content_toolbar_main'];
  41. $this->_schemadb_index = null;
  42. }
  43. function _check_for_content_topic()
  44. {
  45. $guid = $this->_config->get("symlink_topic");
  46. if (is_null($guid))
  47. {
  48. /* No Symlink Topic set */
  49. $this->_topic = $this->_config_topic;
  50. return;
  51. }
  52. $object = mgd_get_object_by_guid($guid);
  53. if (! $object || $object->__table__ != "topic")
  54. {
  55. debug_add("Failed to open symlink content topic, (might also be an invalid object) last Midgard Error: "
  56. . mgd_errstr(), MIDCOM_LOG_ERROR);
  57. debug_print_r("Retrieved object was:", $object, MIDCOM_LOG_INFO);
  58. $GLOBALS["midcom"]->generate_error("Failed to open symlink content topic.");
  59. }
  60. /* Check topic validity */
  61. $root = $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_ROOTTOPIC);
  62. if ($object->parameter("midcom", "component") != "net.nemein.bookmarks")
  63. {
  64. debug_add("Content Topic is invalid, see LOG_INFO object dump", MIDCOM_LOG_ERROR);
  65. debug_print_r("Retrieved object was:", $object, MIDCOM_LOG_INFO);
  66. debug_print_r("ROOT topic object was:", $root, MIDCOM_LOG_INFO);
  67. $GLOBALS["midcom"]->generate_error("Failed to open symlink content topic.");
  68. }
  69. $this->_topic = $object;
  70. }
  71. function can_handle($argc, $argv)
  72. {
  73. global $net_nemein_bookmarks_layouts;
  74. debug_push ($this->_debug_prefix . "can_handle");
  75.  
  76. // Load Schema Database
  77. debug_add("Loading Schema Database", MIDCOM_LOG_DEBUG);
  78. $path = $this->_config->get("schemadb");
  79. $data = midcom_get_snippet_content($path);
  80. eval("\$net_nemein_bookmarks_layouts = Array ( " . $data . ");");
  81. $view_layouts = array ();
  82. if (is_array($GLOBALS['net_nemein_bookmarks_layouts']))
  83. {
  84. foreach ($GLOBALS['net_nemein_bookmarks_layouts'] as $layout)
  85. {
  86. $view_layouts[$layout["name"]] = $layout["description"];
  87. }
  88. }
  89. $this->_schemadb_index = $view_layouts;
  90. if ($argc == 0)
  91. {
  92. return true;
  93. }
  94.  
  95. switch ($argv[0])
  96. {
  97. case "config":
  98. return ($argc == 1);
  99. case "view":
  100. // Fall-through
  101. case "list":
  102. // Fall-through
  103. case "edit":
  104. // Fall-through
  105. case "delete":
  106. return ($argc == 2);
  107. case "create":
  108. return ($argc < 3);
  109. default:
  110. return false;
  111. }
  112. }
  113.  
  114.  
  115. function handle($argc, $argv)
  116. {
  117. debug_push($this->_debug_prefix . "handle");
  118. /* Add the topic configuration item */
  119. $this->_topic_toolbar->add_item(Array(
  120. MIDCOM_TOOLBAR_URL => 'config.html',
  121. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
  122. MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
  123. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png',
  124. MIDCOM_TOOLBAR_ENABLED => true
  125. ));
  126. /* Add the new article links at the beginning*/
  127. // We need to reverse the array, as we are prepending, not appending
  128. $view_layouts = array_reverse($this->_schemadb_index, true);
  129. foreach ($view_layouts as $name => $desc)
  130. {
  131. $text = sprintf($this->_l10n_midcom->get('create %s'), $desc);
  132. $this->_topic_toolbar->add_item(Array(
  133. MIDCOM_TOOLBAR_URL => "create/{$name}.html",
  134. MIDCOM_TOOLBAR_LABEL => $text,
  135. MIDCOM_TOOLBAR_HELPTEXT => null,
  136. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_new.png',
  137. MIDCOM_TOOLBAR_ENABLED => true
  138. ), 0);
  139. }
  140.  
  141. /* Add the new article link at the beginning*/
  142. /*
  143. $this->_topic_toolbar->add_item(Array(
  144. MIDCOM_TOOLBAR_URL => '',
  145. MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('create article'),
  146. MIDCOM_TOOLBAR_HELPTEXT => null,
  147. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_new.png',
  148. MIDCOM_TOOLBAR_ENABLED => true
  149. ), 0);
  150. */
  151. if ($argc == 0)
  152. {
  153. $this->_view = "welcome";
  154. debug_add("viewport = welcome");
  155. debug_pop();
  156. return true;
  157. }
  158.  
  159. switch ($argv[0])
  160. {
  161. case "view":
  162. $result = $this->_init_view($argv[1]);
  163. break;
  164. case "list":
  165. $result = $this->_init_list($argv[1]);
  166. break;
  167. case "edit":
  168. $result = $this->_init_edit($argv[1]);
  169. break;
  170. case "delete":
  171. $result = $this->_init_delete($argv[1]);
  172. break;
  173. case "create":
  174. $result = $this->_init_create(($argc==2) ? $argv[1] : null);
  175. break;
  176. case "config":
  177. $result = $this->_init_config();
  178. break;
  179. default:
  180. $result = false;
  181. break;
  182. }
  183. debug_pop();
  184. return $result;
  185. }
  186.  
  187. function _generate_urlname($article=null)
  188. {
  189. if (!$article)
  190. {
  191. $article = $this->_article;
  192. }
  193.  
  194. $updated = false;
  195. $tries = 0;
  196. $maxtries = 99;
  197. while(!$updated && $tries < $maxtries)
  198. {
  199. $article->name = midcom_generate_urlname_from_string($article->title);
  200. if ($tries > 0)
  201. {
  202. // Append an integer if articles with same name exist
  203. $article->name .= sprintf("-%03d", $tries);
  204. }
  205. $updated = $article->update();
  206. $tries++;
  207. }
  208. if (! $updated)
  209. {
  210. debug_print_r("Failed to update the Article with a new URL, last article state:", $article);
  211. $GLOBALS['midcom']->generate_error("Could not update the article's URL Name: " . mgd_errstr());
  212. // This will exit()
  213. }
  214. return $article;
  215. }
  216. function _init_list($name)
  217. {
  218. $this->_local_toolbar->add_item(Array(
  219. MIDCOM_TOOLBAR_URL => '',
  220. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
  221. MIDCOM_TOOLBAR_HELPTEXT => null,
  222. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
  223. MIDCOM_TOOLBAR_ENABLED => true
  224. ));
  225. $this->_tag = $name;
  226. $GLOBALS['midcom_component_data']['net.nemein.bookmarks']['active_leaf'] = $name;
  227. return true;
  228. }
  229. function _init_view($id)
  230. {
  231. global $net_nemein_bookmarks_layouts;
  232.  
  233. $article = mgd_get_article($id);
  234. if (!$article)
  235. {
  236. debug_add("Article $id could not be loaded: " . mgd_errstr(),
  237. MIDCOM_LOG_INFO);
  238. $this->errstr = "Article $id could not be loaded: " . mgd_errstr();
  239. $this->errcode = MIDCOM_ERRNOTFOUND;
  240. return false;
  241. }
  242. $this->_article = $article;
  243. $GLOBALS["net_nemein_bookmarks_nap_activeid"] = $id;
  244. /* Add the toolbar items */
  245. $this->_local_toolbar->add_item(Array(
  246. MIDCOM_TOOLBAR_URL => "edit/{$id}.html",
  247. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('edit'),
  248. MIDCOM_TOOLBAR_HELPTEXT => null,
  249. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
  250. MIDCOM_TOOLBAR_ENABLED => true
  251. ));
  252. $this->_local_toolbar->add_item(Array(
  253. MIDCOM_TOOLBAR_URL => "delete/{$id}.html",
  254. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('delete'),
  255. MIDCOM_TOOLBAR_HELPTEXT => null,
  256. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png',
  257. MIDCOM_TOOLBAR_ENABLED => true
  258. ));
  259. $this->_local_toolbar->add_item(Array(
  260. MIDCOM_TOOLBAR_URL => '',
  261. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
  262. MIDCOM_TOOLBAR_HELPTEXT => null,
  263. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
  264. MIDCOM_TOOLBAR_ENABLED => true
  265. ));
  266. $this->_layout = new midcom_helper_datamanager($net_nemein_bookmarks_layouts);
  267. if (!$this->_layout)
  268. {
  269. $this->errstr = "Could not create layout, see Debug Log";
  270. $this->errcode = MIDCOM_ERRCRIT;
  271. debug_add($this->errstr, MIDCOM_LOG_CRIT);
  272. return false;
  273. }
  274.  
  275. if (!$this->_layout->init($this->_article))
  276. {
  277. $this->errstr = "Could not initialize layout, see Debug Log";
  278. $this->errcode = MIDCOM_ERRCRIT;
  279. debug_add($this->errstr, MIDCOM_LOG_CRIT);
  280. return false;
  281. }
  282. $this->_view = "view";
  283. return true;
  284. }
  285.  
  286. function _init_edit($id)
  287. {
  288. if (!$this->_init_view($id))
  289. {
  290. return false;
  291. }
  292. $this->_topic_toolbar->disable_item('config.html');
  293. foreach ($this->_schemadb_index as $name => $desc)
  294. {
  295. $this->_topic_toolbar->disable_item("create/{$name}.html");
  296. }
  297. $this->_local_toolbar->disable_item("edit/{$id}.html");
  298. $this->_local_toolbar->disable_item("delete/{$id}.html");
  299. $this->_local_toolbar->disable_view_page();
  300. switch ($this->_layout->process_form())
  301. {
  302. case MIDCOM_DATAMGR_EDITING:
  303. $this->_view = "edit";
  304. $GLOBALS['midcom_component_data']['net.nemein.bookmarks']['active_leaf'] = $id;
  305. //$GLOBALS["net_nemein_bookmarks_nap_activeid"] = $id;
  306. return true;
  307.  
  308. case MIDCOM_DATAMGR_SAVED:
  309. if ($this->_article->name == "" && $this->_config->get("create_name_from_title"))
  310. {
  311. // Empty URL name, regenerate it
  312. $this->_article = $this->_generate_urlname($this->_article);
  313. }
  314. // Update the Index
  315. //$indexer =& $GLOBALS['midcom']->get_service('indexer');
  316. //$indexer->index($this->_datamanager);
  317.  
  318. // Redirect to view page.
  319. $GLOBALS['midcom']->relocate("view/$id.html");
  320. // This will exit()
  321. // Redirect to view page.
  322. //$GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
  323. // . "view/$id.html");
  324. // This will exit()
  325. case MIDCOM_DATAMGR_CANCELLED:
  326. // Redirect to view page.
  327. $GLOBALS['midcom']->relocate("view/$id.html");
  328. // This will exit()
  329. /*
  330. // Redirect to view page.
  331. $GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
  332. . "view/$id.html");
  333. // This will exit()
  334. */
  335. case MIDCOM_DATAMGR_FAILED:
  336. $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
  337. $this->errcode = MIDCOM_ERRCRIT;
  338. return false;
  339. }
  340. }
  341.  
  342. function _init_create ($schema) {
  343. debug_push($this->_debug_prefix . "_init_create");
  344. debug_add("Entering creation mode with schema {$schema}.");
  345. /* Disable the View Page Link */
  346. $this->_local_toolbar->disable_view_page();
  347.  
  348. // Initialize sessioning and the datamanager
  349. $session = new midcom_service_session();
  350. $this->_datamanager = new midcom_helper_datamanager($GLOBALS['net_nemein_bookmarks_layouts']);
  351. if (! $this->_datamanager)
  352. {
  353. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  354. 'Failed to create a datamanager instance, see debug log for details.');
  355. // This will exit.
  356. }
  357. // Check wether we already have a content object, or not.
  358. // Depending on this we'll set up a datamanaer in either create
  359. // or standard-editing mode.
  360. // The NAP active leaf will only be set if we already have a
  361. // content object.
  362. if (! $session->exists('admin_create_id'))
  363. {
  364. debug_add('We do not currently have a content object, entering creation-start mode.');
  365. $this->_article = null;
  366. if (! $this->_datamanager->init_creation_mode($schema, $this))
  367. {
  368. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  369. "Failied to initialize the datamanger in creation mode for schema '{$schema}'.");
  370. // This will exit
  371. }
  372. $create = true;
  373. }
  374. else
  375. {
  376. $id = $session->get('admin_create_id');
  377. debug_add("We have id {$id}, loading object.");
  378. $this->_article = mgd_get_article($id);
  379. if (! $this->_article)
  380. {
  381. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  382. 'Could not load created article, error was: ' . mgd_errstr());
  383. // This will exit
  384. }
  385. if (! $this->_datamanager->init($this->_article))
  386. {
  387. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  388. "Failied to initialize the datamanger to article ID '{$id}'.");
  389. // This will exit
  390. }
  391. $GLOBALS['midcom_component_data']['net.nemein.bookmarks']['active_leaf'] = $this->_article->id;
  392. $create = false;
  393. }
  394. switch ($this->_datamanager->process_form())
  395. {
  396. case MIDCOM_DATAMGR_CREATING:
  397. if (! $create)
  398. {
  399. $this->errcode = MIDCOM_ERRCRIT;
  400. $this->errstr = 'Method MIDCOM_DATAMANAGER_CREATING unknown for non-creation mode.';
  401. debug_pop();
  402. return false;
  403. }
  404. else
  405. {
  406. debug_add('First call within creation mode');
  407. $this->_view = 'create';
  408. break;
  409. }
  410. case MIDCOM_DATAMGR_EDITING:
  411. if ($create)
  412. {
  413. $id = $this->_article->id;
  414. debug_add("First time submit, the DM has created an object, adding ID {$id} to session data");
  415. $session->set('admin_create_id', $id);
  416. }
  417. else
  418. {
  419. debug_add('Subsequent submit, we already have an id in the session space.');
  420. }
  421. $this->_view = 'create';
  422. break;
  423. case MIDCOM_DATAMGR_SAVED:
  424. debug_add('Datamanger has saved, relocating to view.');
  425. $session->remove('admin_create_id');
  426. if ($this->_article->name == '' && $this->_config->get("create_name_from_title"))
  427. {
  428. // Since this is the first editing round generate the URL name from title
  429. // Don't touch the URL names after this to preserve links
  430. // But do it only if no url name has been supplied.
  431. $this->_article = $this->_generate_urlname($this->_article);
  432. }
  433. // index the article
  434. $indexer =& $GLOBALS['midcom']->get_service('indexer');
  435. $indexer->index($this->_datamanager);
  436. // Redirect to view page.
  437. $GLOBALS['midcom']->relocate("view/{$this->_article->id}.html");
  438. // This will exit
  439. case MIDCOM_DATAMGR_CANCELLED_NONECREATED:
  440. if (! $create) {
  441. $this->errcode = MIDCOM_ERRCRIT;
  442. $this->errstr = 'Method MIDCOM_DATAMGR_CANCELLED_NONECREATED unknown for non-creation mode.';
  443. debug_pop();
  444. return false;
  445. } else {
  446. debug_add('Cancel without anything being created, redirecting to the welcome screen.');
  447. $GLOBALS['midcom']->relocate('');
  448. // This will exit
  449.  
  450. }
  451. case MIDCOM_DATAMGR_CANCELLED:
  452. if ($create) {
  453. $this->errcode = MIDCOM_ERRCRIT;
  454. $this->errstr = 'Method MIDCOM_DATAMGR_CANCELLED unknown for creation mode.';
  455. debug_pop();
  456. return false;
  457. } else {
  458. debug_add('Cancel with a temporary object, deleting it and redirecting to the welcome screen.');
  459. if (! mgd_delete_extensions($this->_article) || ! $this->_article->delete())
  460. {
  461. $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
  462. 'Failed to remove temporary article or its dependants.');
  463. // This will exit
  464. }
  465. $session->remove('admin_create_id');
  466. $GLOBALS['midcom']->relocate('');
  467. // This will exit
  468. }
  469. case MIDCOM_DATAMGR_FAILED:
  470. case MIDCOM_DATAMGR_CREATEFAILED:
  471. debug_add('The DM failed critically, see above.');
  472. $this->errstr = 'The Datamanger failed to process the request, see the Debug Log for details';
  473. $this->errcode = MIDCOM_ERRCRIT;
  474. debug_pop();
  475. return false;
  476. default:
  477. $this->errcode = MIDCOM_ERRCRIT;
  478. $this->errstr = 'Method unknown';
  479. debug_pop();
  480. return false;
  481. }
  482. debug_pop();
  483. return true;
  484. }
  485.  
  486. function _dm_create_callback(&$datamanager) {
  487. $result = Array (
  488. "success" => true,
  489. "storage" => null,
  490. );
  491. $midgard = $GLOBALS["midcom"]->get_midgard();
  492. $this->_article = mgd_get_article();
  493. $this->_article->topic = $this->_topic->id;
  494. $this->_article->author = $midgard->user;
  495. $id = $this->_article->create();
  496. if (! $id) {
  497. debug_add ("net.nemein.bookmarks::admin::_dm_create_callback Could not create article: " . mgd_errstr());
  498. return null;
  499. }
  500. $this->_article = mgd_get_article($id);
  501. $result["storage"] =& $this->_article;
  502. return $result;
  503. }
  504.  
  505. function _init_delete($id)
  506. {
  507. if (!$this->_init_view($id))
  508. {
  509. return false;
  510. }
  511. if (array_key_exists("net_nemein_bookmarks_deleteok", $_REQUEST))
  512. {
  513. return $this->_delete_record($id);
  514. // This will redirect to the welcome page on success.
  515. }
  516. else
  517. {
  518. if (array_key_exists("net_nemein_bookmarks_deletecancel", $_REQUEST))
  519. {
  520. // Redirect to view page.
  521. $GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
  522. . "view/$id.html");
  523. // This will exit()
  524. }
  525. else
  526. {
  527. $GLOBALS["net_nemein_bookmarks_nap_activeid"] = $id;
  528. $this->_view = "deletecheck";
  529. }
  530. }
  531. return true;
  532. }
  533. function _init_config()
  534. {
  535. debug_push($this->_debug_prefix . "_init_config");
  536. $this->_prepare_config_dm();
  537. /* Add the toolbar items */
  538. $this->_local_toolbar->add_item(Array(
  539. MIDCOM_TOOLBAR_URL => '',
  540. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
  541. MIDCOM_TOOLBAR_HELPTEXT => null,
  542. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
  543. MIDCOM_TOOLBAR_ENABLED => true
  544. ));
  545. switch ($this->_config_dm->process_form())
  546. {
  547. case MIDCOM_DATAMGR_SAVED:
  548. case MIDCOM_DATAMGR_EDITING:
  549. case MIDCOM_DATAMGR_CANCELLED:
  550. // Do nothing here, the datamanager will invalidate the cache.
  551. // Apart from that, let the user edit the configuration as long
  552. // as he likes.
  553. break;
  554.  
  555. case MIDCOM_DATAMGR_FAILED:
  556. $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
  557. $this->errcode = MIDCOM_ERRCRIT;
  558. debug_pop();
  559. return false;
  560. }
  561. $this->_view = "config";
  562. debug_pop();
  563. return true;
  564. }
  565.  
  566. function show()
  567. {
  568. $GLOBALS["view_l10n"] = $this->_l10n;
  569. $GLOBALS["view_l10n_midcom"] = $this->_l10n_midcom;
  570. $GLOBALS["view_topic"] =& $this->_topic;
  571. $GLOBALS["view_config_topic"] =& $this->_config_topic;
  572. $GLOBALS["view_title"] = $this->_config_topic->extra;
  573. $GLOBALS["view_msg"] = "";
  574. eval("\$result = \$this->_show_$this->_view();");
  575. return $result;
  576. }
  577.  
  578. function _show_welcome()
  579. {
  580. global $net_nemein_bookmarks_layouts;
  581. global $view_layouts;
  582. global $view;
  583. global $config;
  584. global $unification_errors_bookmarks;
  585. global $unification_errors_delicious;
  586. $unification = false;
  587. $config = $this->_config