| 26 | | |
|---|
| | 49 | |
|---|
| | 50 | /** |
|---|
| | 51 | * Load the schemadb and other midcom.admin.folder specific stuff |
|---|
| | 52 | * |
|---|
| | 53 | * @access public |
|---|
| | 54 | */ |
|---|
| | 55 | function _on_initialize() |
|---|
| | 56 | { |
|---|
| | 57 | // Load the configuration |
|---|
| | 58 | $_MIDCOM->componentloader->load('midcom.admin.folder'); |
|---|
| | 59 | $this->_config =& $GLOBALS['midcom_component_data']['midcom.admin.folder']['config']; |
|---|
| | 60 | } |
|---|
| | 61 | |
|---|
| | 62 | /** |
|---|
| | 63 | * Load either a create controller or an edit (simple) controller or trigger an error message |
|---|
| | 64 | * |
|---|
| | 65 | * @access private |
|---|
| | 66 | */ |
|---|
| | 67 | function _load_controller() |
|---|
| | 68 | { |
|---|
| | 69 | // Get the configured schemas |
|---|
| | 70 | $schemadbs = $this->_config->get('schemadbs_folder'); |
|---|
| | 71 | |
|---|
| | 72 | // Check if a custom schema exists |
|---|
| | 73 | if (array_key_exists($this->_topic->component, $schemadbs)) |
|---|
| | 74 | { |
|---|
| | 75 | $schemadb = $schemadbs[$this->_topic->component]; |
|---|
| | 76 | } |
|---|
| | 77 | else |
|---|
| | 78 | { |
|---|
| | 79 | if (!array_key_exists('default', $schemadbs)) |
|---|
| | 80 | { |
|---|
| | 81 | $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Configuration error. No default schema for topic has been defined!'); |
|---|
| | 82 | // This will exit |
|---|
| | 83 | } |
|---|
| | 84 | |
|---|
| | 85 | $schemadb = $schemadbs['default']; |
|---|
| | 86 | } |
|---|
| | 87 | |
|---|
| | 88 | $GLOBALS['midcom_admin_folder_mode'] = $this->_handler_id; |
|---|
| | 89 | |
|---|
| | 90 | // Create the schema instance |
|---|
| | 91 | $this->_schemadb = midcom_helper_datamanager2_schema::load_database($schemadb); |
|---|
| | 92 | |
|---|
| | 93 | switch ($this->_handler_id) |
|---|
| | 94 | { |
|---|
| | 95 | case 'edit': |
|---|
| | 96 | $this->_controller =& midcom_helper_datamanager2_controller::create('simple'); |
|---|
| | 97 | $this->_controller->schemadb =& $this->_schemadb; |
|---|
| | 98 | $this->_controller->set_storage($this->_topic); |
|---|
| | 99 | break; |
|---|
| | 100 | |
|---|
| | 101 | case 'create': |
|---|
| | 102 | $this->_schemadb->default->fields['name']['required'] = 0; |
|---|
| | 103 | $this->_controller =& midcom_helper_datamanager2_controller::create('create'); |
|---|
| | 104 | $this->_controller->schemadb =& $this->_schemadb; |
|---|
| | 105 | $this->_controller->schemaname = 'default'; |
|---|
| | 106 | $this->_controller->callback_object =& $this; |
|---|
| | 107 | |
|---|
| | 108 | // Suggest to create the same type of a folder as the parent is |
|---|
| | 109 | $this->_controller->defaults = array |
|---|
| | 110 | ( |
|---|
| | 111 | 'component' => $this->_topic->component, |
|---|
| | 112 | ); |
|---|
| | 113 | break; |
|---|
| | 114 | |
|---|
| | 115 | default: |
|---|
| | 116 | $_MIDCOM->generate_error(MIDCOM_ERRCRIT, 'Unable to process the request, unknown handler id'); |
|---|
| | 117 | // This will exit |
|---|
| | 118 | } |
|---|
| | 119 | |
|---|
| | 120 | if (! $this->_controller->initialize()) |
|---|
| | 121 | { |
|---|
| | 122 | $_MIDCOM->generate_error(MIDCOM_ERRCRIT, "Failed to initialize a DM2 controller instance for article {$this->_event->id}."); |
|---|
| | 123 | // This will exit. |
|---|
| | 124 | } |
|---|
| | 125 | |
|---|
| | 126 | } |
|---|
| | 127 | |
|---|
| | 128 | /** |
|---|
| | 129 | * DM2 creation callback, binds to the current content topic. |
|---|
| | 130 | */ |
|---|
| | 131 | function & dm2_create_callback (&$controller) |
|---|
| | 132 | { |
|---|
| | 133 | $this->_new_topic = new midcom_db_topic(); |
|---|
| | 134 | $this->_new_topic->up = $this->_topic->id; |
|---|
| | 135 | |
|---|
| | 136 | if (! $this->_new_topic->create()) |
|---|
| | 137 | { |
|---|
| | 138 | debug_push_class(__CLASS__, __FUNCTION__); |
|---|
| | 139 | debug_print_r('We operated on this object:', $this->_new_topic); |
|---|
| | 140 | debug_pop(); |
|---|
| | 141 | $_MIDCOM->generate_error(MIDCOM_ERRCRIT, |
|---|
| | 142 | 'Failed to create a new article, cannot continue. Last Midgard error was: '. mgd_errstr()); |
|---|
| | 143 | // This will exit. |
|---|
| | 144 | } |
|---|
| | 145 | |
|---|
| | 146 | return $this->_new_topic; |
|---|
| | 147 | } |
|---|
| | 148 | |
|---|
| | 149 | |
|---|
| 38 | | if (array_key_exists('f_cancel', $_REQUEST)) |
|---|
| 39 | | { |
|---|
| 40 | | $_MIDCOM->relocate(''); |
|---|
| 41 | | // This will exit. |
|---|
| 42 | | } |
|---|
| 43 | | |
|---|
| 44 | | if (array_key_exists('f_submit', $_REQUEST)) |
|---|
| 45 | | { |
|---|
| 46 | | if ($this->_process_edit_form()) |
|---|
| 47 | | { |
|---|
| 48 | | // Relocate to the renamed topic |
|---|
| 49 | | $_MIDCOM->relocate($_MIDCOM->permalinks->create_permalink($this->_topic->guid)); |
|---|
| 50 | | // This will exit. |
|---|
| 51 | | } |
|---|
| | 161 | $this->_handler_id = str_replace('____ais-folder-', '', $handler_id); |
|---|
| | 162 | |
|---|
| | 163 | // Load the DM2 controller |
|---|
| | 164 | $this->_load_controller(); |
|---|
| | 165 | |
|---|
| | 166 | // Get the content topic prefix |
|---|
| | 167 | $prefix = $_MIDCOM->get_context_data(MIDCOM_CONTEXT_ANCHORPREFIX); |
|---|
| | 168 | |
|---|
| | 169 | // Store the old name before editing |
|---|
| | 170 | $old_name = $this->_topic->name; |
|---|
| | 171 | |
|---|
| | 172 | switch ($this->_controller->process_form()) |
|---|
| | 173 | { |
|---|
| | 174 | case 'cancel': |
|---|
| | 175 | $_MIDCOM->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('cancelled')); |
|---|
| | 176 | $_MIDCOM->relocate($prefix); |
|---|
| | 177 | break; |
|---|
| | 178 | |
|---|
| | 179 | case 'save': |
|---|
| | 180 | if ($this->_handler_id === 'edit') |
|---|
| | 181 | { |
|---|
| | 182 | $_MIDCOM->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder saved')); |
|---|
| | 183 | |
|---|
| | 184 | // Get the relocation url |
|---|
| | 185 | $url = preg_replace("/{$old_name}\/\$/", "{$this->_topic->name}/", $prefix); |
|---|
| | 186 | } |
|---|
| | 187 | else |
|---|
| | 188 | { |
|---|
| | 189 | $_MIDCOM->uimessages->add($this->_l10n->get('midcom.admin.folder'), $this->_l10n->get('folder created')); |
|---|
| | 190 | |
|---|
| | 191 | // Generate name if it is missing |
|---|
| | 192 | if (!$this->_new_topic->name) |
|---|
| | 193 | { |
|---|
| | 194 | $this->_new_topic->name = midcom_generate_urlname_from_string($this->_new_topic->extra); |
|---|
| | 195 | $this->_new_topic->update(); |
|---|
| | 196 | } |
|---|
| | 197 | |
|---|
| | 198 | // Get the relocation url |
|---|
| | 199 | $url = "{$prefix}/{$this->_new_topic->name}/"; |
|---|
| | 200 | } |
|---|
| | 201 | |
|---|
| | 202 | $_MIDCOM->relocate($url); |
|---|
| | 203 | // This will exit |
|---|
| 134 | | * Processes the _Edit folder_ page form and updates the folder. |
|---|
| 135 | | * |
|---|
| 136 | | * @access private |
|---|
| 137 | | * @return boolean Indicating success |
|---|
| 138 | | */ |
|---|
| 139 | | function _process_edit_form() |
|---|
| 140 | | { |
|---|
| 141 | | if (trim($_REQUEST['f_name']) == '') |
|---|
| 142 | | { |
|---|
| 143 | | $this->_processing_msg = $_MIDCOM->i18n->get_string('no url name specified', 'midcom.admin.folder'); |
|---|
| 144 | | $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('create folder', 'midcom.admin.folder'), $this->_processing_msg, 'error'); |
|---|
| 145 | | |
|---|
| 146 | | return false; |
|---|
| 147 | | } |
|---|
| 148 | | if (trim($_REQUEST['f_title']) == '') |
|---|
| 149 | | { |
|---|
| 150 | | $this->_processing_msg = $_MIDCOM->i18n->get_string('title is empty', 'midcom.admin.folder'); |
|---|
| 151 | | $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('create folder', 'midcom.admin.folder'), $this->_processing_msg, 'error'); |
|---|
| 152 | | return false; |
|---|
| 153 | | } |
|---|
| 154 | | |
|---|
| 155 | | if (mgd_get_topic_by_name($this->_topic->id, $_REQUEST['f_name'])) |
|---|
| 156 | | { |
|---|
| 157 | | $this->_processing_msg = sprintf($_MIDCOM->i18n->get_string('folder with name %s already exists', 'midcom.admin.folder'), $_REQUEST['f_name']); |
|---|
| 158 | | $_MIDCOM->uimessages->add($_MIDCOM->i18n->get_string('create folder', 'midcom.admin.folder'), $this->_processing_msg, 'error'); |
|---|
| 159 | | return false; |
|---|
| 160 | | } |
|---|
| 161 | | |
|---|
| 162 | | // store form data in topic object |
|---|
| 163 | | $this->_topic->name = midcom_generate_urlname_from_string($_REQUEST['f_name']); |
|---|
| 164 | | $this->_topic->extra = $_REQUEST['f_title']; |
|---|
| 165 | | //$this->_topic->score = $_REQUEST['f_score']; |
|---|
| 166 | | //$this->_topic->owner = $_REQUEST['f_owner']; |
|---|
| 167 | | |
|---|
| 168 | | if ($_REQUEST['f_style'] === '__create') |
|---|
| 169 | | { |
|---|
| 170 | | $this->_topic->require_do('midcom.admin.folder:template_management'); |
|---|
| 171 | | |
|---|
| 172 | | $this->_topic->style = $this->_create_style($this->_topic->name); |
|---|
| 173 | | |
|---|
| 174 | | // Failed to create the new style template |
|---|
| 175 | | if ($this->_topic->style === '') |
|---|
| 176 | | { |
|---|
| 177 | | return false; |
|---|
| 178 | | } |
|---|
| | 291 | * Shows the _Edit folder_ page. |
|---|
| | 292 | * |
|---|
| | 293 | * @access private |
|---|
| | 294 | */ |
|---|
| | 295 | function _show_edit($handler_id, &$data) |
|---|
| | 296 | { |
|---|
| | 297 | $styles_all = midcom_admin_folder_folder_management::list_styles(); |
|---|
| | 298 | |
|---|
| | 299 | // Show the style element |
|---|
| | 300 | if ($this->_handler_id === 'create') |
|---|
| | 301 | { |
|---|
| | 302 | $data['page_title'] = sprintf($this->_i18n->get_string("create folder", 'midcom.admin.folder')); |
|---|
| 182 | | $this->_topic->style = $_REQUEST['f_style']; |
|---|
| 183 | | } |
|---|
| 184 | | |
|---|
| 185 | | // TODO: Move to metadata |
|---|
| 186 | | $this->_topic->parameter('midcom.helper.nav', 'navorder', $_REQUEST['f_navorder']); |
|---|
| 187 | | |
|---|
| 188 | | if ( array_key_exists('f_style_inherit', $_REQUEST) |
|---|
| 189 | | && $_REQUEST['f_style_inherit'] == 'on') |
|---|
| 190 | | { |
|---|
| 191 | | $this->_topic->styleInherit = true; |
|---|
| 192 | | } |
|---|
| 193 | | else |
|---|
| 194 | | { |
|---|
| 195 | | $this->_topic->styleInherit = false; |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | if ( trim($_REQUEST['f_type']) !== $this->_topic->component |
|---|
| 199 | | && $_MIDCOM->auth->admin |
|---|
| 200 | | && $_REQUEST['f_type'] !== '') |
|---|
| 201 | | { |
|---|
| 202 | | $this->_topic->component = $_REQUEST['f_type']; |
|---|
| 203 | | } |
|---|
| 204 | | |
|---|
| 205 | | |
|---|
| 206 | | if (! $this->_topic->update()) |
|---|
| 207 | | { |
|---|
| 208 | | $this->_processing_msg = 'Could not save Folder: ' . mgd_errstr(); |
|---|
| 209 | | return false; |
|---|
| 210 | | } |
|---|
| 211 | | |
|---|
| 212 | | $_MIDCOM->cache->invalidate($this->_topic->guid()); |
|---|
| 213 | | |
|---|
| 214 | | return true; |
|---|
| 215 | | } |
|---|
| 216 | | |
|---|
| 217 | | /** |
|---|
| 218 | | * Shows the _Edit folder_ page. |
|---|
| 219 | | * |
|---|
| 220 | | * @access private |
|---|
| 221 | | */ |
|---|
| 222 | | function _show_edit($handler_id, &$data) |
|---|
| 223 | | { |
|---|
| 224 | | // Get parent component and navorder |
|---|
| 225 | | $data['parent_topic'] = $this->_topic->component; |
|---|
| 226 | | |
|---|
| 227 | | $data['view'] =& $this->_topic; |
|---|
| 228 | | $data['style_inherit'] = $this->_topic->styleInherit; |
|---|
| 229 | | $data['style'] = $this->_topic->style; |
|---|
| 230 | | $data['folder'] =& $this->_topic; |
|---|
| 231 | | |
|---|
| 232 | | // TODO: Move to metadata |
|---|
| 233 | | $data['navorder'] = $this->_topic->parameter('midcom.helper.nav', 'navorder'); |
|---|
| 234 | | |
|---|
| 235 | | $data['navorder_list'] = array |
|---|
| 236 | | ( |
|---|
| 237 | | MIDCOM_NAVORDER_DEFAULT => $_MIDCOM->i18n->get_string('default sort order', 'midcom.admin.folder'), |
|---|
| 238 | | MIDCOM_NAVORDER_TOPICSFIRST => $_MIDCOM->i18n->get_string('folders first', 'midcom.admin.folder'), |
|---|
| 239 | | MIDCOM_NAVORDER_ARTICLESFIRST => $_MIDCOM->i18n->get_string('pages first', 'midcom.admin.folder'), |
|---|
| 240 | | MIDCOM_NAVORDER_SCORE => $_MIDCOM->i18n->get_string('by score', 'midcom.admin.folder'), |
|---|
| 241 | | ); |
|---|
| 242 | | |
|---|
| 243 | | $style_default = array |
|---|
| 244 | | ( |
|---|
| 245 | | '' => $_MIDCOM->i18n->get_string('default', 'midcom.admin.folder'), |
|---|
| 246 | | ); |
|---|
| 247 | | |
|---|
| 248 | | if ($this->_topic->can_do('midcom.admin.folder:template_management')) |
|---|
| 249 | | { |
|---|
| 250 | | $style_default['__create'] = $_MIDCOM->i18n->get_string('new layout template', 'midcom.admin.folder'); |
|---|
| 251 | | } |
|---|
| 252 | | |
|---|
| 253 | | $styles_all = midcom_admin_folder_folder_management::list_styles(); |
|---|
| 254 | | |
|---|
| 255 | | $data['styles'] = array_merge($style_default, $styles_all); |
|---|
| 256 | | |
|---|
| 257 | | // Place $view as a super global for the style checker function |
|---|
| 258 | | // midcom_admin_content_list_styles_selector2 |
|---|
| 259 | | $GLOBALS['view'] =& $this->_topic; |
|---|
| 260 | | |
|---|
| 261 | | // Show the style element |
|---|
| 262 | | midcom_show_style('midcom-admin-show-edit-folder'); |
|---|
| | 306 | $data['page_title'] = sprintf($this->_i18n->get_string("{$this->_handler_id} folder %s", 'midcom.admin.folder'), $this->_topic->extra); |
|---|
| | 307 | } |
|---|
| | 308 | |
|---|
| | 309 | midcom_show_style('midcom-admin-show-folder-actions'); |
|---|