1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 
<?php

/**
 * This file contains the functions to add, modify, remove, collapse and expand categories.
 *
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC1
 */

if (!defined('SMF'))
    die('No direct access...');

/**
 * Edit the position and properties of a category.
 * general function to modify the settings and position of a category.
 * used by ManageBoards.php to change the settings of a category.
 *
 * @param int $category_id The ID of the category
 * @param array $catOptions An array containing data and options related to the category
 */
function modifyCategory($category_id, $catOptions)
{
    global $sourcedir, $smcFunc;

    $catUpdates = array();
    $catParameters = array();

    $cat_id = $category_id;
    call_integration_hook('integrate_pre_modify_category', array($cat_id, &$catOptions));

    // Wanna change the categories position?
    if (isset($catOptions['move_after']))
    {
        // Store all categories in the proper order.
        $cats = array();
        $cat_order = array();

        // Setting 'move_after' to '0' moves the category to the top.
        if ($catOptions['move_after'] == 0)
            $cats[] = $category_id;

        // Grab the categories sorted by cat_order.
        $request = $smcFunc['db_query']('', '
            SELECT id_cat, cat_order
            FROM {db_prefix}categories
            ORDER BY cat_order',
            array(
            )
        );
        while ($row = $smcFunc['db_fetch_assoc']($request))
        {
            if ($row['id_cat'] != $category_id)
                $cats[] = $row['id_cat'];
            if ($row['id_cat'] == $catOptions['move_after'])
                $cats[] = $category_id;
            $cat_order[$row['id_cat']] = $row['cat_order'];
        }
        $smcFunc['db_free_result']($request);

        // Set the new order for the categories.
        foreach ($cats as $index => $cat)
            if ($index != $cat_order[$cat])
                $smcFunc['db_query']('', '
                    UPDATE {db_prefix}categories
                    SET cat_order = {int:new_order}
                    WHERE id_cat = {int:current_category}',
                    array(
                        'new_order' => $index,
                        'current_category' => $cat,
                    )
                );

        // If the category order changed, so did the board order.
        require_once($sourcedir . '/Subs-Boards.php');
        reorderBoards();
    }

    if (isset($catOptions['cat_name']))
    {
        $catUpdates[] = 'name = {string:cat_name}';
        $catParameters['cat_name'] = $catOptions['cat_name'];
    }

    if (isset($catOptions['cat_desc']))
    {
        $catUpdates[] = 'description = {string:cat_desc}';
        $catParameters['cat_desc'] = $catOptions['cat_desc'];
    }

    // Can a user collapse this category or is it too important?
    if (isset($catOptions['is_collapsible']))
    {
        $catUpdates[] = 'can_collapse = {int:is_collapsible}';
        $catParameters['is_collapsible'] = $catOptions['is_collapsible'] ? 1 : 0;
    }

    $cat_id = $category_id;
    call_integration_hook('integrate_modify_category', array($cat_id, &$catUpdates, &$catParameters));

    // Do the updates (if any).
    if (!empty($catUpdates))
    {
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}categories
            SET
                ' . implode(',
                ', $catUpdates) . '
            WHERE id_cat = {int:current_category}',
            array_merge($catParameters, array(
                'current_category' => $category_id,
            ))
        );

        if (empty($catOptions['dont_log']))
            logAction('edit_cat', array('catname' => isset($catOptions['cat_name']) ? $catOptions['cat_name'] : $category_id), 'admin');
    }
}

/**
 * Create a new category.
 * general function to create a new category and set its position.
 * allows (almost) the same options as the modifyCat() function.
 * returns the ID of the newly created category.
 *
 * @param array $catOptions An array of data and settings related to the new category. Should have at least 'cat_name' and can also have 'cat_desc', 'move_after' and 'is_collapsable'
 */
function createCategory($catOptions)
{
    global $smcFunc;

    // Check required values.
    if (!isset($catOptions['cat_name']) || trim($catOptions['cat_name']) == '')
        trigger_error('createCategory(): A category name is required', E_USER_ERROR);

    // Set default values.
    if (!isset($catOptions['cat_desc']))
        $catOptions['cat_desc'] = '';
    if (!isset($catOptions['move_after']))
        $catOptions['move_after'] = 0;
    if (!isset($catOptions['is_collapsible']))
        $catOptions['is_collapsible'] = true;
    // Don't log an edit right after.
    $catOptions['dont_log'] = true;

    $cat_columns = array(
        'name' => 'string-48',
        'description' => 'string',
    );
    $cat_parameters = array(
        $catOptions['cat_name'],
        $catOptions['cat_desc'],
    );

    call_integration_hook('integrate_create_category', array(&$catOptions, &$cat_columns, &$cat_parameters));

    // Add the category to the database.
    $category_id = $smcFunc['db_insert']('',
        '{db_prefix}categories',
        $cat_columns,
        $cat_parameters,
        array('id_cat'),
        1
    );

    // Set the given properties to the newly created category.
    modifyCategory($category_id, $catOptions);

    logAction('add_cat', array('catname' => $catOptions['cat_name']), 'admin');

    // Return the database ID of the category.
    return $category_id;
}

/**
 * Remove one or more categories.
 * general function to delete one or more categories.
 * allows to move all boards in the categories to a different category before deleting them.
 * if moveChildrenTo is set to null, all boards inside the given categories will be deleted.
 * deletes all information that's associated with the given categories.
 * updates the statistics to reflect the new situation.
 *
 * @param array $categories The IDs of the categories to delete
 * @param int $moveBoardsTo The ID of the category to move any boards to or null to delete the boards
 */
function deleteCategories($categories, $moveBoardsTo = null)
{
    global $sourcedir, $smcFunc, $cat_tree;

    require_once($sourcedir . '/Subs-Boards.php');

    getBoardTree();

    call_integration_hook('integrate_delete_category', array($categories, &$moveBoardsTo));

    // With no category set to move the boards to, delete them all.
    if ($moveBoardsTo === null)
    {
        $request = $smcFunc['db_query']('', '
            SELECT id_board
            FROM {db_prefix}boards
            WHERE id_cat IN ({array_int:category_list})',
            array(
                'category_list' => $categories,
            )
        );
        $boards_inside = array();
        while ($row = $smcFunc['db_fetch_assoc']($request))
            $boards_inside[] = $row['id_board'];
        $smcFunc['db_free_result']($request);

        if (!empty($boards_inside))
            deleteBoards($boards_inside, null);
    }

    // Make sure the safe category is really safe.
    elseif (in_array($moveBoardsTo, $categories))
        trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR);

    // Move the boards inside the categories to a safe category.
    else
        $smcFunc['db_query']('', '
            UPDATE {db_prefix}boards
            SET id_cat = {int:new_parent_cat}
            WHERE id_cat IN ({array_int:category_list})',
            array(
                'category_list' => $categories,
                'new_parent_cat' => $moveBoardsTo,
            )
        );

    // Do the deletion of the category itself
    $smcFunc['db_query']('', '
        DELETE FROM {db_prefix}categories
        WHERE id_cat IN ({array_int:category_list})',
        array(
            'category_list' => $categories,
        )
    );

    // Log what we've done.
    foreach ($categories as $category)
        logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin');

    // Get all boards back into the right order.
    reorderBoards();
}

?>