/**
 * Processes the check box bulk actions
 *
 */
function processZenpageBulkActions($type, &$reports)
{
    global $_zp_zenpage;
    if (isset($_POST['ids'])) {
        //echo "action for checked items:". $_POST['checkallaction'];
        $action = sanitize($_POST['checkallaction']);
        $links = $_POST['ids'];
        $total = count($links);
        $message = NULL;
        $sql = '';
        if ($action != 'noaction') {
            if ($total > 0) {
                if ($action == 'addtags' || $action == 'alltags') {
                    foreach ($_POST as $key => $value) {
                        $key = postIndexDecode($key);
                        if (substr($key, 0, 10) == 'mass_tags_') {
                            if ($value) {
                                $tags[] = substr($key, 10);
                            }
                        }
                    }
                    $tags = sanitize($tags, 3);
                }
                $n = 0;
                switch ($action) {
                    case 'deleteall':
                        $message = gettext('Selected items deleted');
                        break;
                    case 'showall':
                        $message = gettext('Selected items published');
                        break;
                    case 'hideall':
                        $message = gettext('Selected items unpublished');
                        break;
                    case 'commentson':
                        $message = gettext('Comments enabled for selected items');
                        break;
                    case 'commentsoff':
                        $message = gettext('Comments disabled for selected items');
                        break;
                    case 'resethitcounter':
                        $message = gettext('Hitcounter for selected items');
                        break;
                    case 'addtags':
                        $message = gettext('Tags added to selected items');
                        break;
                    case 'cleartags':
                        $message = gettext('Tags cleared from selected items');
                        break;
                    case 'alltags':
                        $message = gettext('Tags added to articles of selected items');
                        break;
                    case 'clearalltags':
                        $message = gettext('Tags cleared from articles of selected items');
                        break;
                }
                foreach ($links as $titlelink) {
                    $class = 'Zenpage' . $type;
                    $obj = new $class($titlelink);
                    switch ($action) {
                        case 'deleteall':
                            $obj->remove();
                            break;
                        case 'addtags':
                            $mytags = array_unique(array_merge($tags, $obj->getTags()));
                            $obj->setTags($mytags);
                            break;
                        case 'cleartags':
                            $obj->setTags(array());
                            break;
                        case 'alltags':
                            $allarticles = $obj->getArticles('', 'all', true);
                            foreach ($allarticles as $article) {
                                $newsobj = new ZenpageNews($article['titlelink']);
                                $mytags = array_unique(array_merge($tags, $newsobj->getTags()));
                                $newsobj->setTags($mytags);
                                $newsobj->save();
                            }
                            break;
                        case 'clearalltags':
                            $allarticles = $obj->getArticles('', 'all', true);
                            foreach ($allarticles as $article) {
                                $newsobj = new ZenpageNews($article['titlelink']);
                                $newsobj->setTags(array());
                                $newsobj->save();
                            }
                            break;
                        case 'showall':
                            $obj->set('show', 1);
                            break;
                        case 'hideall':
                            $obj->set('show', 0);
                            break;
                        case 'commentson':
                            $obj->set('commentson', 1);
                            break;
                        case 'commentsoff':
                            $obj->set('commentson', 0);
                            break;
                        case 'resethitcounter':
                            $obj->set('hitcounter', 0);
                            break;
                    }
                    $obj->save();
                }
                if (!is_null($message)) {
                    $reports[] = "<p class='messagebox fade-message'>" . $message . "</p>";
                }
            }
        }
    }
}
/**
 * Processes the check box bulk actions
 *
 */
function processZenpageBulkActions($type)
{
    global $_zp_zenpage;
    $action = false;
    if (isset($_POST['ids'])) {
        //echo "action for checked items:". $_POST['checkallaction'];
        $action = sanitize($_POST['checkallaction']);
        $links = sanitize($_POST['ids']);
        $total = count($links);
        $message = NULL;
        $sql = '';
        if ($action != 'noaction') {
            if ($total > 0) {
                if ($action == 'addtags' || $action == 'alltags') {
                    $tags = bulkTags();
                }
                if ($action == 'addcats') {
                    foreach ($_POST as $key => $value) {
                        $key = postIndexDecode($key);
                        if (substr($key, 0, 3) == 'cat') {
                            if ($value) {
                                $cats[] = substr($key, 3);
                            }
                        }
                    }
                    $cats = sanitize($cats, 3);
                }
                $n = 0;
                foreach ($links as $titlelink) {
                    $class = 'Zenpage' . $type;
                    $obj = new $class($titlelink);
                    switch ($action) {
                        case 'deleteall':
                            $obj->remove();
                            SearchEngine::clearSearchCache();
                            break;
                        case 'addtags':
                            $mytags = array_unique(array_merge($tags, $obj->getTags()));
                            $obj->setTags($mytags);
                            break;
                        case 'cleartags':
                            $obj->setTags(array());
                            break;
                        case 'alltags':
                            $allarticles = $obj->getArticles('', 'all', true);
                            foreach ($allarticles as $article) {
                                $newsobj = new ZenpageNews($article['titlelink']);
                                $mytags = array_unique(array_merge($tags, $newsobj->getTags()));
                                $newsobj->setTags($mytags);
                                $newsobj->save();
                            }
                            break;
                        case 'clearalltags':
                            $allarticles = $obj->getArticles('', 'all', true);
                            foreach ($allarticles as $article) {
                                $newsobj = new ZenpageNews($article['titlelink']);
                                $newsobj->setTags(array());
                                $newsobj->save();
                            }
                            break;
                        case 'addcats':
                            $catarray = array();
                            $allcats = $obj->getCategories();
                            foreach ($cats as $cat) {
                                $catitem = $_zp_zenpage->getCategory($cat);
                                $catarray[] = $catitem['titlelink'];
                                //to use the setCategories method we need an array with just the titlelinks!
                            }
                            $allcatsarray = array();
                            foreach ($allcats as $allcat) {
                                $allcatsarray[] = $allcat['titlelink'];
                                //same here!
                            }
                            $mycats = array_unique(array_merge($catarray, $allcatsarray));
                            $obj->setCategories($mycats);
                            break;
                        case 'clearcats':
                            $obj->setCategories(array());
                            break;
                        case 'showall':
                            $obj->set('show', 1);
                            break;
                        case 'hideall':
                            $obj->set('show', 0);
                            break;
                        case 'commentson':
                            $obj->set('commentson', 1);
                            break;
                        case 'commentsoff':
                            $obj->set('commentson', 0);
                            break;
                        case 'resethitcounter':
                            $obj->set('hitcounter', 0);
                            break;
                    }
                    $obj->save();
                }
            }
        }
    }
    return $action;
}