Source for file admin.php

Documentation is available at admin.php

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