Documentation is available at admin.php
- <?php
- class net_nemein_bookmarks_admin
- {
- var $_debug_prefix;
- var $_config;
- var $_config_dm;
- var $_topic; /* Data Topic */
- var $_config_topic; /* Config Topic */
- var $_view;
- var $_article;
- var $_attachment;
- var $_layout;
- var $_l10n;
- var $_l10n_midcom;
- var $errcode;
- var $errstr;
- var $_local_toolbar;
- var $_topic_toolbar;
- var $_tag;
- var $schemadb_index;
- function net_nemein_bookmarks_admin($topic, $config)
- {
- $this->_debug_prefix = "net.nemein.bookmarks admin::";
- $this->_config = $config;
- $this->_config_dm = null;
- $this->_config_topic = $topic;
- $this->_view = "";
- $this->_article = false;
- $this->_attachment = false;
- $this->_layout = false;
- $i18n =& $GLOBALS["midcom"]->get_service("i18n");
- $this->_l10n = $i18n->get_l10n("net.nemein.bookmarks");
- $this->_l10n_midcom = $i18n->get_l10n("midcom");
- $this->errcode = MIDCOM_ERROK;
- $this->errstr = "";
- $this->_check_for_content_topic();
- $this->_local_toolbar =& $GLOBALS['midcom_admin_content_toolbar_component'];
- $this->_topic_toolbar =& $GLOBALS['midcom_admin_content_toolbar_main'];
- $this->_schemadb_index = null;
- }
- function _check_for_content_topic()
- {
- $guid = $this->_config->get("symlink_topic");
- if (is_null($guid))
- {
- /* No Symlink Topic set */
- $this->_topic = $this->_config_topic;
- return;
- }
- $object = mgd_get_object_by_guid($guid);
- if (! $object || $object->__table__ != "topic")
- {
- debug_add("Failed to open symlink content topic, (might also be an invalid object) last Midgard Error: "
- . mgd_errstr(), MIDCOM_LOG_ERROR);
- debug_print_r("Retrieved object was:", $object, MIDCOM_LOG_INFO);
- $GLOBALS["midcom"]->generate_error("Failed to open symlink content topic.");
- }
- /* Check topic validity */
- $root = $GLOBALS["midcom"]->get_context_data(MIDCOM_CONTEXT_ROOTTOPIC);
- if ($object->parameter("midcom", "component") != "net.nemein.bookmarks")
- {
- debug_add("Content Topic is invalid, see LOG_INFO object dump", MIDCOM_LOG_ERROR);
- debug_print_r("Retrieved object was:", $object, MIDCOM_LOG_INFO);
- debug_print_r("ROOT topic object was:", $root, MIDCOM_LOG_INFO);
- $GLOBALS["midcom"]->generate_error("Failed to open symlink content topic.");
- }
- $this->_topic = $object;
- }
- function can_handle($argc, $argv)
- {
- global $net_nemein_bookmarks_layouts;
- debug_push ($this->_debug_prefix . "can_handle");
- // Load Schema Database
- debug_add("Loading Schema Database", MIDCOM_LOG_DEBUG);
- $path = $this->_config->get("schemadb");
- $data = midcom_get_snippet_content($path);
- eval("\$net_nemein_bookmarks_layouts = Array ( " . $data . ");");
- $view_layouts = array ();
- if (is_array($GLOBALS['net_nemein_bookmarks_layouts']))
- {
- foreach ($GLOBALS['net_nemein_bookmarks_layouts'] as $layout)
- {
- $view_layouts[$layout["name"]] = $layout["description"];
- }
- }
- $this->_schemadb_index = $view_layouts;
- if ($argc == 0)
- {
- return true;
- }
- switch ($argv[0])
- {
- case "config":
- return ($argc == 1);
- case "view":
- // Fall-through
- case "list":
- // Fall-through
- case "edit":
- // Fall-through
- case "delete":
- return ($argc == 2);
- case "create":
- return ($argc < 3);
- default:
- return false;
- }
- }
- function handle($argc, $argv)
- {
- debug_push($this->_debug_prefix . "handle");
- /* Add the topic configuration item */
- $this->_topic_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => 'config.html',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('component configuration'),
- MIDCOM_TOOLBAR_HELPTEXT => $this->_l10n_midcom->get('component configuration helptext'),
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_folder-properties.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- /* Add the new article links at the beginning*/
- // We need to reverse the array, as we are prepending, not appending
- $view_layouts = array_reverse($this->_schemadb_index, true);
- foreach ($view_layouts as $name => $desc)
- {
- $text = sprintf($this->_l10n_midcom->get('create %s'), $desc);
- $this->_topic_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => "create/{$name}.html",
- MIDCOM_TOOLBAR_LABEL => $text,
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_new.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ), 0);
- }
- /* Add the new article link at the beginning*/
- /*
- $this->_topic_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => '',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n->get('create article'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/stock_new.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ), 0);
- */
- if ($argc == 0)
- {
- $this->_view = "welcome";
- debug_add("viewport = welcome");
- debug_pop();
- return true;
- }
- switch ($argv[0])
- {
- case "view":
- $result = $this->_init_view($argv[1]);
- break;
- case "list":
- $result = $this->_init_list($argv[1]);
- break;
- case "edit":
- $result = $this->_init_edit($argv[1]);
- break;
- case "delete":
- $result = $this->_init_delete($argv[1]);
- break;
- case "create":
- $result = $this->_init_create(($argc==2) ? $argv[1] : null);
- break;
- case "config":
- $result = $this->_init_config();
- break;
- default:
- $result = false;
- break;
- }
- debug_pop();
- return $result;
- }
- function _generate_urlname($article=null)
- {
- if (!$article)
- {
- $article = $this->_article;
- }
- $updated = false;
- $tries = 0;
- $maxtries = 99;
- while(!$updated && $tries < $maxtries)
- {
- $article->name = midcom_generate_urlname_from_string($article->title);
- if ($tries > 0)
- {
- // Append an integer if articles with same name exist
- $article->name .= sprintf("-%03d", $tries);
- }
- $updated = $article->update();
- $tries++;
- }
- if (! $updated)
- {
- debug_print_r("Failed to update the Article with a new URL, last article state:", $article);
- $GLOBALS['midcom']->generate_error("Could not update the article's URL Name: " . mgd_errstr());
- // This will exit()
- }
- return $article;
- }
- function _init_list($name)
- {
- $this->_local_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => '',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- $this->_tag = $name;
- $GLOBALS['midcom_component_data']['net.nemein.bookmarks']['active_leaf'] = $name;
- return true;
- }
- function _init_view($id)
- {
- global $net_nemein_bookmarks_layouts;
- $article = mgd_get_article($id);
- if (!$article)
- {
- debug_add("Article $id could not be loaded: " . mgd_errstr(),
- MIDCOM_LOG_INFO);
- $this->errstr = "Article $id could not be loaded: " . mgd_errstr();
- $this->errcode = MIDCOM_ERRNOTFOUND;
- return false;
- }
- $this->_article = $article;
- $GLOBALS["net_nemein_bookmarks_nap_activeid"] = $id;
- /* Add the toolbar items */
- $this->_local_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => "edit/{$id}.html",
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('edit'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- $this->_local_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => "delete/{$id}.html",
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('delete'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- $this->_local_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => '',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- $this->_layout = new midcom_helper_datamanager($net_nemein_bookmarks_layouts);
- if (!$this->_layout)
- {
- $this->errstr = "Could not create layout, see Debug Log";
- $this->errcode = MIDCOM_ERRCRIT;
- debug_add($this->errstr, MIDCOM_LOG_CRIT);
- return false;
- }
- if (!$this->_layout->init($this->_article))
- {
- $this->errstr = "Could not initialize layout, see Debug Log";
- $this->errcode = MIDCOM_ERRCRIT;
- debug_add($this->errstr, MIDCOM_LOG_CRIT);
- return false;
- }
- $this->_view = "view";
- return true;
- }
- function _init_edit($id)
- {
- if (!$this->_init_view($id))
- {
- return false;
- }
- $this->_topic_toolbar->disable_item('config.html');
- foreach ($this->_schemadb_index as $name => $desc)
- {
- $this->_topic_toolbar->disable_item("create/{$name}.html");
- }
- $this->_local_toolbar->disable_item("edit/{$id}.html");
- $this->_local_toolbar->disable_item("delete/{$id}.html");
- $this->_local_toolbar->disable_view_page();
- switch ($this->_layout->process_form())
- {
- case MIDCOM_DATAMGR_EDITING:
- $this->_view = "edit";
- $GLOBALS['midcom_component_data']['net.nemein.bookmarks']['active_leaf'] = $id;
- //$GLOBALS["net_nemein_bookmarks_nap_activeid"] = $id;
- return true;
- case MIDCOM_DATAMGR_SAVED:
- if ($this->_article->name == "" && $this->_config->get("create_name_from_title"))
- {
- // Empty URL name, regenerate it
- $this->_article = $this->_generate_urlname($this->_article);
- }
- // Update the Index
- //$indexer =& $GLOBALS['midcom']->get_service('indexer');
- //$indexer->index($this->_datamanager);
- // Redirect to view page.
- $GLOBALS['midcom']->relocate("view/$id.html");
- // This will exit()
- // Redirect to view page.
- //$GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
- // . "view/$id.html");
- // This will exit()
- case MIDCOM_DATAMGR_CANCELLED:
- // Redirect to view page.
- $GLOBALS['midcom']->relocate("view/$id.html");
- // This will exit()
- /*
- // Redirect to view page.
- $GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
- . "view/$id.html");
- // This will exit()
- */
- case MIDCOM_DATAMGR_FAILED:
- $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
- $this->errcode = MIDCOM_ERRCRIT;
- return false;
- }
- }
- function _init_create ($schema) {
- debug_push($this->_debug_prefix . "_init_create");
- debug_add("Entering creation mode with schema {$schema}.");
- /* Disable the View Page Link */
- $this->_local_toolbar->disable_view_page();
- // Initialize sessioning and the datamanager
- $session = new midcom_service_session();
- $this->_datamanager = new midcom_helper_datamanager($GLOBALS['net_nemein_bookmarks_layouts']);
- if (! $this->_datamanager)
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- 'Failed to create a datamanager instance, see debug log for details.');
- // This will exit.
- }
- // Check wether we already have a content object, or not.
- // Depending on this we'll set up a datamanaer in either create
- // or standard-editing mode.
- // The NAP active leaf will only be set if we already have a
- // content object.
- if (! $session->exists('admin_create_id'))
- {
- debug_add('We do not currently have a content object, entering creation-start mode.');
- $this->_article = null;
- if (! $this->_datamanager->init_creation_mode($schema, $this))
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- "Failied to initialize the datamanger in creation mode for schema '{$schema}'.");
- // This will exit
- }
- $create = true;
- }
- else
- {
- $id = $session->get('admin_create_id');
- debug_add("We have id {$id}, loading object.");
- $this->_article = mgd_get_article($id);
- if (! $this->_article)
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- 'Could not load created article, error was: ' . mgd_errstr());
- // This will exit
- }
- if (! $this->_datamanager->init($this->_article))
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- "Failied to initialize the datamanger to article ID '{$id}'.");
- // This will exit
- }
- $GLOBALS['midcom_component_data']['net.nemein.bookmarks']['active_leaf'] = $this->_article->id;
- $create = false;
- }
- switch ($this->_datamanager->process_form())
- {
- case MIDCOM_DATAMGR_CREATING:
- if (! $create)
- {
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = 'Method MIDCOM_DATAMANAGER_CREATING unknown for non-creation mode.';
- debug_pop();
- return false;
- }
- else
- {
- debug_add('First call within creation mode');
- $this->_view = 'create';
- break;
- }
- case MIDCOM_DATAMGR_EDITING:
- if ($create)
- {
- $id = $this->_article->id;
- debug_add("First time submit, the DM has created an object, adding ID {$id} to session data");
- $session->set('admin_create_id', $id);
- }
- else
- {
- debug_add('Subsequent submit, we already have an id in the session space.');
- }
- $this->_view = 'create';
- break;
- case MIDCOM_DATAMGR_SAVED:
- debug_add('Datamanger has saved, relocating to view.');
- $session->remove('admin_create_id');
- if ($this->_article->name == '' && $this->_config->get("create_name_from_title"))
- {
- // Since this is the first editing round generate the URL name from title
- // Don't touch the URL names after this to preserve links
- // But do it only if no url name has been supplied.
- $this->_article = $this->_generate_urlname($this->_article);
- }
- // index the article
- $indexer =& $GLOBALS['midcom']->get_service('indexer');
- $indexer->index($this->_datamanager);
- // Redirect to view page.
- $GLOBALS['midcom']->relocate("view/{$this->_article->id}.html");
- // This will exit
- case MIDCOM_DATAMGR_CANCELLED_NONECREATED:
- if (! $create) {
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = 'Method MIDCOM_DATAMGR_CANCELLED_NONECREATED unknown for non-creation mode.';
- debug_pop();
- return false;
- } else {
- debug_add('Cancel without anything being created, redirecting to the welcome screen.');
- $GLOBALS['midcom']->relocate('');
- // This will exit
- }
- case MIDCOM_DATAMGR_CANCELLED:
- if ($create) {
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = 'Method MIDCOM_DATAMGR_CANCELLED unknown for creation mode.';
- debug_pop();
- return false;
- } else {
- debug_add('Cancel with a temporary object, deleting it and redirecting to the welcome screen.');
- if (! mgd_delete_extensions($this->_article) || ! $this->_article->delete())
- {
- $GLOBALS['midcom']->generate_error(MIDCOM_ERRCRIT,
- 'Failed to remove temporary article or its dependants.');
- // This will exit
- }
- $session->remove('admin_create_id');
- $GLOBALS['midcom']->relocate('');
- // This will exit
- }
- case MIDCOM_DATAMGR_FAILED:
- case MIDCOM_DATAMGR_CREATEFAILED:
- debug_add('The DM failed critically, see above.');
- $this->errstr = 'The Datamanger failed to process the request, see the Debug Log for details';
- $this->errcode = MIDCOM_ERRCRIT;
- debug_pop();
- return false;
- default:
- $this->errcode = MIDCOM_ERRCRIT;
- $this->errstr = 'Method unknown';
- debug_pop();
- return false;
- }
- debug_pop();
- return true;
- }
- function _dm_create_callback(&$datamanager) {
- $result = Array (
- "success" => true,
- "storage" => null,
- );
- $midgard = $GLOBALS["midcom"]->get_midgard();
- $this->_article = mgd_get_article();
- $this->_article->topic = $this->_topic->id;
- $this->_article->author = $midgard->user;
- $id = $this->_article->create();
- if (! $id) {
- debug_add ("net.nemein.bookmarks::admin::_dm_create_callback Could not create article: " . mgd_errstr());
- return null;
- }
- $this->_article = mgd_get_article($id);
- $result["storage"] =& $this->_article;
- return $result;
- }
- function _init_delete($id)
- {
- if (!$this->_init_view($id))
- {
- return false;
- }
- if (array_key_exists("net_nemein_bookmarks_deleteok", $_REQUEST))
- {
- return $this->_delete_record($id);
- // This will redirect to the welcome page on success.
- }
- else
- {
- if (array_key_exists("net_nemein_bookmarks_deletecancel", $_REQUEST))
- {
- // Redirect to view page.
- $GLOBALS['midcom']->relocate($GLOBALS['midcom']->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX)
- . "view/$id.html");
- // This will exit()
- }
- else
- {
- $GLOBALS["net_nemein_bookmarks_nap_activeid"] = $id;
- $this->_view = "deletecheck";
- }
- }
- return true;
- }
- function _init_config()
- {
- debug_push($this->_debug_prefix . "_init_config");
- $this->_prepare_config_dm();
- /* Add the toolbar items */
- $this->_local_toolbar->add_item(Array(
- MIDCOM_TOOLBAR_URL => '',
- MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('back to index'),
- MIDCOM_TOOLBAR_HELPTEXT => null,
- MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/folder.png',
- MIDCOM_TOOLBAR_ENABLED => true
- ));
- switch ($this->_config_dm->process_form())
- {
- case MIDCOM_DATAMGR_SAVED:
- case MIDCOM_DATAMGR_EDITING:
- case MIDCOM_DATAMGR_CANCELLED:
- // Do nothing here, the datamanager will invalidate the cache.
- // Apart from that, let the user edit the configuration as long
- // as he likes.
- break;
- case MIDCOM_DATAMGR_FAILED:
- $this->errstr = "Datamanager: " . $GLOBALS["midcom_errstr"];
- $this->errcode = MIDCOM_ERRCRIT;
- debug_pop();
- return false;
- }
- $this->_view = "config";
- debug_pop();
- return true;
- }
- function show()
- {
- $GLOBALS["view_l10n"] = $this->_l10n;
- $GLOBALS["view_l10n_midcom"] = $this->_l10n_midcom;
- $GLOBALS["view_topic"] =& $this->_topic;
- $GLOBALS["view_config_topic"] =& $this->_config_topic;
- $GLOBALS["view_title"] = $this->_config_topic->extra;
- $GLOBALS["view_msg"] = "";
- eval("\$result = \$this->_show_$this->_view();");
- return $result;
- }
- function _show_welcome()
- {
- global $net_nemein_bookmarks_layouts;
- global $view_layouts;
- global $view;
- global $config;
- global $unification_errors_bookmarks;
- global $unification_errors_delicious;
- $unification = false;
- $config = $this->_config