/** * Check whether the main category and the extra categories are valid * in a Blog's context and try to fix errors. * * @author Tilman BLUMENBACH / Tblue * * @param integer The main category to check (by reference). * @param object The Blog to which the category is supposed to belong to (by reference). * @param array Extra categories for the post (by reference). * * @return boolean False on error (use xmlrpcs_resperror() to return it), true on success. */ function xmlrpcs_check_cats(&$maincat, &$Blog, &$extracats) { global $xmlrpcs_errcode, $xmlrpcs_errmsg, $xmlrpcerruser; // Trim $maincat and $extracats (qtm sends whitespace before the cat IDs): $maincat = trim($maincat); $extracats = array_map('trim', $extracats); $ChapterCache =& get_ChapterCache(); // ---- CHECK MAIN CATEGORY ---- if ($ChapterCache->get_by_ID($maincat, false) === false) { // Category does not exist! // Remove old category from extra cats: if (($key = array_search($maincat, $extracats)) !== false) { unset($extracats[$key]); } // Set new category (blog default): $maincat = $Blog->get_default_cat_ID(); logIO('Invalid main cat ID - new ID: ' . $maincat); } else { if (get_allow_cross_posting() < 2 && get_catblog($maincat) != $Blog->ID) { // We cannot use a maincat of another blog than the current one: $xmlrpcs_errcode = $xmlrpcerruser + 11; $xmlrpcs_errmsg = 'Current crossposting setting does not allow moving posts to a different blog.'; return false; } } // ---- CHECK EXTRA CATEGORIES ---- foreach ($extracats as $ecat) { if ($ecat == $maincat) { // We already checked the maincat above (or reset it): continue; } logIO('Checking extra cat: ' . $ecat); if ($ChapterCache->get_by_ID($ecat, false) === false) { // Extra cat does not exist: $xmlrpcs_errcode = $xmlrpcerruser + 11; $xmlrpcs_errmsg = 'Extra category ' . (int) $ecat . ' not found in requested blog.'; return false; } } if (!in_array($maincat, $extracats)) { logIO('$maincat was not found in $extracats array - adding.'); $extracats[] = $maincat; } return true; }
/** * * Check if new category needs to be created or not (after post editing). * If the new category radio is checked creates the new category and set it to post category * If the new category checkbox is checked creates the new category and set it to post extracat * * Function is called during post creation or post update * * @param Object Post category (by reference). * @param Array Post extra categories (by reference). * @return boolean true - if there is no new category, or new category created succesfull; false if new category creation failed. */ function check_categories(&$post_category, &$post_extracats) { $post_category = param('post_category', 'integer', -1); $post_extracats = param('post_extracats', 'array/integer', array()); global $Messages, $Blog, $blog; load_class('chapters/model/_chaptercache.class.php', 'ChapterCache'); $GenericCategoryCache =& get_ChapterCache(); if ($post_category == -1) { // no main cat select if (count($post_extracats) == 0) { // no extra cat select $post_category = $Blog->get_default_cat_ID(); } else { // first extracat become main_cat if (get_allow_cross_posting() >= 2) { // allow moving posts between different blogs is enabled, set first selected cat as main cat $post_category = $post_extracats[0]; } else { // allow moving posts between different blogs is disabled - we need a main cat from $blog foreach ($post_extracats as $cat) { if (get_catblog($cat) != $blog) { // this cat is not from $blog continue; } // set first cat from $blog as main cat $post_category = $cat; break; } if ($post_category == -1) { // wasn't cat selected from $blog select a default as main cat $post_category = $Blog->get_default_cat_ID(); } } } if ($post_category) { // If main cat is not a new category, and has been autoselected $GenericCategory =& $GenericCategoryCache->get_by_ID($post_category); $post_category_Blog = $GenericCategory->get_Blog(); $Messages->add(sprintf(T_('The main category for this post has been automatically set to "%s" (Blog "%s")'), $GenericCategory->get_name(), $post_category_Blog->get('name')), 'warning'); } } if (!$post_category || in_array(0, $post_extracats)) { global $current_User; if (!$current_User->check_perm('blog_cats', '', false, $Blog->ID)) { // Current user cannot add a categories for this blog check_categories_nosave($post_category, $post_extracats); // set up the category parameters $Messages->add(T_('You are not allowed to create a new category.'), 'error'); return false; } $category_name = param('category_name', 'string', true); if ($category_name == '') { $show_error = !$post_category; // new main category without name => error message check_categories_nosave($post_category, $post_extracats); // set up the category parameters if ($show_error) { // new main category without name $Messages->add(T_('Please provide a name for new category.'), 'error'); return false; } return true; } $new_GenericCategory =& $GenericCategoryCache->new_obj(NULL, $blog); // create new category object $new_GenericCategory->set('name', $category_name); if ($new_GenericCategory->dbinsert() !== false) { $Messages->add(T_('New category created.'), 'success'); if (!$post_category) { $post_category = $new_GenericCategory->ID; // set the new ID } if (($extracat_key = array_search('0', $post_extracats)) || $post_extracats[0] == '0') { if ($extracat_key) { unset($post_extracats[$extracat_key]); } else { unset($post_extracats[0]); } $post_extracats[] = $new_GenericCategory->ID; } $GenericCategoryCache->add($new_GenericCategory); } else { $Messages->add(T_('New category creation failed.'), 'error'); return false; } } if (get_allow_cross_posting() == 2) { // Extra cats in different blogs is disabled, check selected extra cats $post_category_blog = get_catblog($post_category); $ignored_cats = ''; foreach ($post_extracats as $key => $cat) { if (get_catblog($cat) != $post_category_blog) { // this cat is not from main category blog, it has to be ingnored $GenericCategory =& $GenericCategoryCache->get_by_ID($cat); $ignored_cats = $ignored_cats . $GenericCategory->get_name() . ', '; unset($post_extracats[$key]); } } $ingnored_length = strlen($ignored_cats); if ($ingnored_length > 2) { // ingnore list is not empty global $current_User, $admin_url; if ($current_User->check_perm('options', 'view', false)) { $cross_posting_text = '<a href="' . $admin_url . '?ctrl=features">' . T_('cross-posting is disabled') . '</a>'; } else { $cross_posting_text = T_('cross-posting is disabled'); } $ignored_cats = substr($ignored_cats, 0, $ingnored_length - 2); $Messages->add(sprintf(T_('The category selection "%s" was ignored since %s'), $ignored_cats, $cross_posting_text), 'warning'); } } // make sure main cat is in extracat list and there are no duplicates $post_extracats[] = $post_category; $post_extracats = array_unique($post_extracats); return true; }
// This is somewhat in between new and edit... // Check permission based on DB status: $current_User->check_perm('item_post!CURSTATUS', 'edit', true, $edited_Item); $edited_Item->status = param('post_status', 'string', NULL); // 'published' or 'draft' or ... // We know we can use at least one status, // but we need to make sure the requested/default one is ok: $edited_Item->status = $Blog->get_allowed_item_status($edited_Item->status); // We use the request variables to fill the edit form, because we need to be able to pass those values // from tab to tab via javascript when the editor wants to switch views... $edited_Item->load_from_Request(true); // needs Blog set // Check if new category was started to create. If yes then set up parameters for next page check_categories_nosave($post_category, $post_extracats); $edited_Item->set('main_cat_ID', $post_category); if ($edited_Item->main_cat_ID && get_allow_cross_posting() < 2 && $edited_Item->get_blog_ID() != $blog) { // the main cat is not in the list of categories; this happens, if the user switches blogs during editing: $edited_Item->set('main_cat_ID', $Blog->get_default_cat_ID()); } $post_extracats = param('post_extracats', 'array/integer', $post_extracats); param('item_tags', 'string', ''); // Trackback addresses (never saved into item) param('trackback_url', 'string', ''); // Page title: $AdminUI->title_titlearea = sprintf(T_('Editing post #%d: %s'), $edited_Item->ID, $Blog->get('name')); $AdminUI->breadcrumbpath_add(sprintf(T_('Post #%s'), $edited_Item->ID), '?ctrl=items&blog=' . $Blog->ID . '&p=' . $edited_Item->ID); $AdminUI->breadcrumbpath_add(T_('Edit'), '?ctrl=items&action=edit&blog=' . $Blog->ID . '&p=' . $edited_Item->ID); // Params we need for tab switching: $tab_switch_params = 'p=' . $edited_Item->ID; break; case 'history':
function import_cat_select_before_each($cat_ID, $level) { // callback to display sublist element global $current_blog_ID, $blog, $cat, $postdata, $default_main_cat, $action, $tabindex; echo '<li>'; if (get_allow_cross_posting() >= 1) { // We allow cross posting, display checkbox: echo '<input type="checkbox" name="post_extracats[]" class="checkbox" title="', T_('Select as an additionnal category'), '" value="', $cat_ID, '"'; echo ' />'; } // Radio for main cat: if ($current_blog_ID == $blog) { if ($default_main_cat == 0 && $action == 'post') { // Assign default cat for new post $default_main_cat = $cat_ID; } echo ' <input type="radio" name="post_category" class="checkbox" title="', T_('Select as MAIN category'), '" value="', $cat_ID, '"'; if ($cat_ID == $postdata["Category"] || $cat_ID == $default_main_cat) { echo ' checked="checked"'; } echo ' />'; } echo ' ' . htmlspecialchars(get_catname($cat_ID)); }