Source for file admin.php

Documentation is available at admin.php

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