Example #1
0
/**
 * save the rank depending on given categories order
 *
 * The list of ordered categories id is supposed to be in the same parent
 * category
 *
 * @param array categories
 * @return void
 */
function save_categories_order($categories)
{
    $current_rank_for_id_uppercat = array();
    $current_rank = 0;
    $datas = array();
    foreach ($categories as $category) {
        if (is_array($category)) {
            $id = $category['id'];
            $id_uppercat = $category['id_uppercat'];
            if (!isset($current_rank_for_id_uppercat[$id_uppercat])) {
                $current_rank_for_id_uppercat[$id_uppercat] = 0;
            }
            $current_rank = ++$current_rank_for_id_uppercat[$id_uppercat];
        } else {
            $id = $category;
            $current_rank++;
        }
        $datas[] = array('id' => $id, 'rank' => $current_rank);
    }
    $fields = array('primary' => array('id'), 'update' => array('rank'));
    mass_updates(CATEGORIES_TABLE, $fields, $datas);
    update_global_rank();
}
Example #2
0
            delete_elements($to_delete_elements);
        }
        $counts['del_elements'] = count($to_delete_elements);
    }
    $template->append('footer_elements', '<!-- scanning files : ' . get_elapsed_time($start_files, get_moment()) . ' -->');
}
// +-----------------------------------------------------------------------+
// |                          synchronize files                            |
// +-----------------------------------------------------------------------+
if (isset($_POST['submit']) and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') and !$general_failure) {
    if (!$simulate) {
        $start = get_moment();
        update_category('all');
        $template->append('footer_elements', '<!-- update_category(all) : ' . get_elapsed_time($start, get_moment()) . ' -->');
        $start = get_moment();
        update_global_rank();
        $template->append('footer_elements', '<!-- ordering categories : ' . get_elapsed_time($start, get_moment()) . ' -->');
    }
    if ($_POST['sync'] == 'files') {
        $start = get_moment();
        $opts['category_id'] = '';
        $opts['recursive'] = true;
        if (isset($_POST['cat'])) {
            $opts['category_id'] = $_POST['cat'];
            if (!isset($_POST['subcats-included']) or $_POST['subcats-included'] != 1) {
                $opts['recursive'] = false;
            }
        }
        $files = get_filelist($opts['category_id'], $site_id, $opts['recursive'], false);
        $template->append('footer_elements', '<!-- get_filelist : ' . get_elapsed_time($start, get_moment()) . ' -->');
        $start = get_moment();
/**
 * API method
 * Deletes a category
 * @param mixed[] $params
 *    @option string|int[] category_id
 *    @option string photo_deletion_mode
 *    @option string pwg_token
 */
function ws_categories_delete($params, &$service)
{
    if (get_pwg_token() != $params['pwg_token']) {
        return new PwgError(403, 'Invalid security token');
    }
    $modes = array('no_delete', 'delete_orphans', 'force_delete');
    if (!in_array($params['photo_deletion_mode'], $modes)) {
        return new PwgError(500, '[ws_categories_delete]' . ' invalid parameter photo_deletion_mode "' . $params['photo_deletion_mode'] . '"' . ', possible values are {' . implode(', ', $modes) . '}.');
    }
    if (!is_array($params['category_id'])) {
        $params['category_id'] = preg_split('/[\\s,;\\|]/', $params['category_id'], -1, PREG_SPLIT_NO_EMPTY);
    }
    $params['category_id'] = array_map('intval', $params['category_id']);
    $category_ids = array();
    foreach ($params['category_id'] as $category_id) {
        if ($category_id > 0) {
            $category_ids[] = $category_id;
        }
    }
    if (count($category_ids) == 0) {
        return;
    }
    $query = '
SELECT id
  FROM ' . CATEGORIES_TABLE . '
  WHERE id IN (' . implode(',', $category_ids) . ')
;';
    $category_ids = array_from_query($query, 'id');
    if (count($category_ids) == 0) {
        return;
    }
    include_once PHPWG_ROOT_PATH . 'admin/include/functions.php';
    delete_categories($category_ids, $params['photo_deletion_mode']);
    update_global_rank();
}
Example #4
0
/**
 * Create a virtual category.
 *
 * @param string $category_name
 * @param int $parent_id
 * @param array $options
 *    - boolean commentable
 *    - boolean visible
 *    - string status
 *    - string comment
 *    - boolean inherit
 * @return array ('info', 'id') or ('error')
 */
function create_virtual_category($category_name, $parent_id = null, $options = array())
{
    global $conf, $user;
    // is the given category name only containing blank spaces ?
    if (preg_match('/^\\s*$/', $category_name)) {
        return array('error' => l10n('The name of an album must not be empty'));
    }
    $insert = array('name' => $category_name, 'rank' => 0, 'global_rank' => 0);
    // is the album commentable?
    if (isset($options['commentable']) and is_bool($options['commentable'])) {
        $insert['commentable'] = $options['commentable'];
    } else {
        $insert['commentable'] = $conf['newcat_default_commentable'];
    }
    $insert['commentable'] = boolean_to_string($insert['commentable']);
    // is the album temporarily locked? (only visible by administrators,
    // whatever permissions) (may be overwritten if parent album is not
    // visible)
    if (isset($options['visible']) and is_bool($options['visible'])) {
        $insert['visible'] = $options['visible'];
    } else {
        $insert['visible'] = $conf['newcat_default_visible'];
    }
    $insert['visible'] = boolean_to_string($insert['visible']);
    // is the album private? (may be overwritten if parent album is private)
    if (isset($options['status']) and 'private' == $options['status']) {
        $insert['status'] = 'private';
    } else {
        $insert['status'] = $conf['newcat_default_status'];
    }
    // any description for this album?
    if (isset($options['comment'])) {
        $insert['comment'] = $conf['allow_html_descriptions'] ? $options['comment'] : strip_tags($options['comment']);
    }
    if (!empty($parent_id) and is_numeric($parent_id)) {
        $query = '
SELECT id, uppercats, global_rank, visible, status
  FROM ' . CATEGORIES_TABLE . '
  WHERE id = ' . $parent_id . '
;';
        $parent = pwg_db_fetch_assoc(pwg_query($query));
        $insert['id_uppercat'] = $parent['id'];
        $insert['global_rank'] = $parent['global_rank'] . '.' . $insert['rank'];
        // at creation, must a category be visible or not ? Warning : if the
        // parent category is invisible, the category is automatically create
        // invisible. (invisible = locked)
        if ('false' == $parent['visible']) {
            $insert['visible'] = 'false';
        }
        // at creation, must a category be public or private ? Warning : if the
        // parent category is private, the category is automatically create
        // private.
        if ('private' == $parent['status']) {
            $insert['status'] = 'private';
        }
        $uppercats_prefix = $parent['uppercats'] . ',';
    } else {
        $uppercats_prefix = '';
    }
    // we have then to add the virtual category
    single_insert(CATEGORIES_TABLE, $insert);
    $inserted_id = pwg_db_insert_id(CATEGORIES_TABLE);
    single_update(CATEGORIES_TABLE, array('uppercats' => $uppercats_prefix . $inserted_id), array('id' => $inserted_id));
    update_global_rank();
    if ('private' == $insert['status'] and !empty($insert['id_uppercat']) and (isset($options['inherit']) and $options['inherit'] or $conf['inheritance_by_default'])) {
        $query = '
      SELECT group_id
      FROM ' . GROUP_ACCESS_TABLE . '
      WHERE cat_id = ' . $insert['id_uppercat'] . '
    ;';
        $granted_grps = query2array($query, null, 'group_id');
        $inserts = array();
        foreach ($granted_grps as $granted_grp) {
            $inserts[] = array('group_id' => $granted_grp, 'cat_id' => $inserted_id);
        }
        mass_inserts(GROUP_ACCESS_TABLE, array('group_id', 'cat_id'), $inserts);
        $query = '
      SELECT user_id
      FROM ' . USER_ACCESS_TABLE . '
      WHERE cat_id = ' . $insert['id_uppercat'] . '
    ;';
        $granted_users = query2array($query, null, 'user_id');
        add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id']), $granted_users)));
    } elseif ('private' == $insert['status']) {
        add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id']))));
    }
    return array('info' => l10n('Virtual album added'), 'id' => $inserted_id);
}