public function update_all_in_set()
 {
     $Categories = new PerchCategories_Categories();
     $categories = $Categories->get_by('setID', $this->id());
     if (PerchUtil::count($categories)) {
         foreach ($categories as $Cat) {
             $Cat->update_meta(false);
         }
     }
 }
Example #2
0
function perch_category($path, $opts = array(), $return = false)
{
    $path = rtrim(ltrim($path, '/'), '/') . '/';
    $opts = PerchUtil::extend(array('set' => false, 'skip-template' => false, 'template' => 'category.html', 'filter' => 'catPath', 'match' => 'eq', 'value' => $path), $opts);
    $Categories = new PerchCategories_Categories();
    $r = $Categories->get_custom($opts);
    if ($opts['skip-template']) {
        $return = true;
    }
    if ($return) {
        return $r;
    }
    echo $r;
}
 public function get_display_path($catID = false, $s = array())
 {
     if ($catID === false) {
         $Cat = $this;
     } else {
         $Categories = new PerchCategories_Categories();
         $Cat = $Categories->find($catID);
     }
     if (is_object($Cat)) {
         $s[] = $Cat->catTitle();
     }
     if (!is_object($Cat) || $Cat->catParentID() === '0') {
         $s = array_reverse($s);
         return implode(' › ', $s);
     }
     return $this->get_display_path($Cat->catParentID(), $s);
 }
Example #4
0
<?php

if (!$CurrentUser->has_priv('categories.delete')) {
    PerchUtil::redirect(PERCH_LOGINPATH . '/core/apps/categories/');
}
$Categories = new PerchCategories_Categories();
if (isset($_GET['id']) && is_numeric($_GET['id'])) {
    $catID = (int) $_GET['id'];
    $Category = $Categories->find($catID);
}
if (!$Category || !is_object($Category)) {
    PerchUtil::redirect(PERCH_LOGINPATH . '/core/apps/categories/');
}
/* --------- Delete Form ----------- */
$Form = new PerchForm('delete');
if ($Form->posted() && $Form->validate()) {
    $Category->delete();
    if ($Form->submitted_via_ajax) {
        echo PERCH_LOGINPATH . '/core/apps/categories/sets/?id=' . $Category->setID();
        exit;
    } else {
        PerchUtil::redirect(PERCH_LOGINPATH . '/core/apps/categories/sets/?id=' . $Category->setID());
    }
}
 public function get_categories()
 {
     $cats = $this->get_field('categories', false);
     if (PerchUtil::count($cats)) {
         $Categories = new PerchCategories_Categories();
         $out = array();
         foreach ($cats as $catID) {
             $out[] = $Categories->find((int) $catID);
         }
         return $out;
     }
     return false;
 }
<?php

$HTML = $API->get('HTML');
// Try to update
$Settings = $API->get('Settings');
if ($Settings->get('perch_blog_update')->val() != '5.0') {
    PerchUtil::redirect($API->app_path() . '/update/');
}
$Blog = new PerchBlog_Posts($API);
$Paging = $API->get('Paging');
$Paging->set_per_page(15);
$Categories = new PerchCategories_Categories();
$categories = $Categories->get_for_set('blog');
$Sections = new PerchBlog_Sections($API);
$sections = $Sections->all();
$Lang = $API->get('Lang');
$posts = array();
$filter = 'all';
if (isset($_GET['category']) && $_GET['category'] != '') {
    $filter = 'category';
    $category = $_GET['category'];
}
if (isset($_GET['section']) && $_GET['section'] != '') {
    $filter = 'section';
    $section = $_GET['section'];
}
if (isset($_GET['status']) && $_GET['status'] != '') {
    $filter = 'status';
    $status = $_GET['status'];
}
switch ($filter) {
Example #7
0
<?php

$API = new PerchAPI('categories', 1.0);
$HTML = $API->get('HTML');
$Sets = new PerchCategories_Sets();
$Categories = new PerchCategories_Categories();
$setID = false;
$Set = false;
if (isset($_GET['id']) && $_GET['id'] != '') {
    $setID = (int) $_GET['id'];
    $Set = $Sets->find($setID);
}
if ($setID == false) {
    PerchUtil::redirect(PERCH_LOGINPATH . '/core/apps/categories/');
}
$cats = $Categories->get_tree($setID);
 public function update_category_counts()
 {
     $sql = 'SELECT COUNT(*) AS qty, c.catID
             FROM ' . PERCH_DB_PREFIX . $this->index_table . ' i, ' . PERCH_DB_PREFIX . 'categories c, ' . $this->table . ' p 
             WHERE i.indexValue=c.catPath AND i.indexKey=\'_category\' AND i.itemKey=\'postID\' AND i.itemID=p.postID
                 AND p.postStatus=\'Published\' AND p.postDateTime<=' . $this->db->pdb(date('Y-m-d H:i:00')) . ' 
             GROUP BY i.indexValue
             ';
     $rows = $this->db->get_rows($sql);
     $sql = 'DELETE FROM ' . PERCH_DB_PREFIX . 'category_counts WHERE countType=' . $this->db->pdb('blog.post');
     $this->db->execute($sql);
     if (PerchUtil::count($rows)) {
         $Categories = new PerchCategories_Categories();
         foreach ($rows as $row) {
             $Category = $Categories->find((int) $row['catID']);
             if ($Category) {
                 $Category->update_count('blog.post', $row['qty']);
             }
         }
     }
 }
 $db->execute($sql);
 $sql = "ALTER TABLE `" . PERCH_DB_PREFIX . "blog_posts` ADD INDEX `idx_status` (`postStatus`)`";
 $db->execute($sql);
 $sql = "ALTER TABLE `" . PERCH_DB_PREFIX . "blog_posts` ADD INDEX `idx_section` (`sectionID`)`";
 $db->execute($sql);
 $sql = "CREATE TABLE `" . PERCH_DB_PREFIX . "blog_sections` (\n\t          `sectionID` int(11) NOT NULL AUTO_INCREMENT,\n\t          `sectionTitle` varchar(255) NOT NULL DEFAULT '',\n\t          `sectionSlug` varchar(255) NOT NULL DEFAULT '',\n\t          `sectionPostCount` int(10) unsigned NOT NULL DEFAULT '0',\n\t          `sectionDynamicFields` text,\n\t          PRIMARY KEY (`sectionID`),\n\t          KEY `idx_slug` (`sectionSlug`)\n\t        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC";
 $db->execute($sql);
 $sql = "INSERT INTO `" . PERCH_DB_PREFIX . "blog_sections` (sectionID, sectionTitle, sectionSlug, sectionPostCount, sectionDynamicFields) VALUES ('1', 'Posts', 'posts', 0, '')";
 $db->execute($sql);
 // 5.0
 $sql = "CREATE TABLE IF NOT EXISTS `" . PERCH_DB_PREFIX . "blog_index` (\n\t\t      `indexID` int(10) NOT NULL AUTO_INCREMENT,\n\t\t      `itemKey` char(64) NOT NULL DEFAULT '-',\n\t\t      `itemID` int(10) NOT NULL DEFAULT '0',\n\t\t      `indexKey` char(64) NOT NULL DEFAULT '-',\n\t\t      `indexValue` char(255) NOT NULL DEFAULT '',\n\t\t      PRIMARY KEY (`indexID`),\n\t\t      KEY `idx_fk` (`itemKey`,`itemID`),\n\t\t      KEY `idx_key` (`indexKey`),\n\t\t      KEY `idx_key_val` (`indexKey`,`indexValue`),\n\t\t      KEY `idx_keys` (`itemKey`,`indexKey`)\n\t\t    ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
 $db->execute($sql);
 $sql = "ALTER TABLE `" . PERCH_DB_PREFIX . "blog_categories` ADD `categoryCoreID` INT(10)  NULL  DEFAULT NULL  AFTER `categoryDynamicFields`";
 $db->execute($sql);
 $Core_CategorySets = new PerchCategories_Sets();
 $Core_Categories = new PerchCategories_Categories();
 $Set = $Core_CategorySets->get_by('setSlug', 'blog');
 if (!$Set) {
     $Set = $Core_CategorySets->create(array('setTitle' => PerchLang::get('Blog'), 'setSlug' => 'blog', 'setTemplate' => '~/perch_blog/templates/blog/category_set.html', 'setCatTemplate' => '~/perch_blog/templates/blog/category.html', 'setDynamicFields' => '[]'));
     $cats = $db->get_rows('SELECT * FROM ' . PERCH_DB_PREFIX . 'blog_categories');
     if (PerchUtil::count($cats)) {
         foreach ($cats as $cat) {
             $dynfields = '[]';
             if ($cat['categoryDynamicFields']) {
                 $dynfields = $cat['categoryDynamicFields'];
             }
             $NewCat = $Core_Categories->create(array('setID' => $Set->id(), 'catParentID' => 0, 'catTitle' => $cat['categoryTitle'], 'catSlug' => $cat['categorySlug'], 'catPath' => '/blog/' . $cat['categorySlug'] . '/', 'catDynamicFields' => $dynfields));
             if (is_object($NewCat)) {
                 $db->update(PERCH_DB_PREFIX . 'blog_categories', array('categoryCoreID' => $NewCat->id()), 'categoryID', $cat['categoryID']);
             }
         }
 public function get_index($raw = false)
 {
     if ($raw === false) {
         $raw = $this->get_raw();
     }
     $id = $this->Tag->id();
     $out = array();
     if (is_array($raw)) {
         $Categories = new PerchCategories_Categories();
         foreach ($raw as $key => $val) {
             if (!is_array($val)) {
                 $Cat = $Categories->find((int) $val);
                 if (is_object($Cat)) {
                     $out[] = array('key' => '_category', 'value' => $Cat->catPath());
                 }
             }
         }
     }
     return $out;
 }
Example #11
0
/*
		DEFAULT FIELDS
		This is a string of template code that gets appended to the end of the template.
		It's used to ensure that any required static fields end up in the form even if the user removes them.
		Useful for things like titles and slugs that the app won't work without.
*/
$default_fields = '<perch:category id="catTitle" type="smarttext" label="Title" required="true" />
					   <perch:category id="catSlug" type="slug" for="catTitle" />';
/*
		SET UP
		Load up an instance of the API, and any classes we need.
*/
$API = new PerchAPI(1.0, 'categories');
$HTML = $API->get('HTML');
$Sets = new PerchCategories_Sets();
$Categories = new PerchCategories_Categories($API);
/*
		Initialising variables we need to set the default for our different edit states.
		This page edits in the following states:

			1) Edit - a category ID is passed in ?id=
			2) New in set - a set ID is passed in ?sid=
			3) New subcat - a parent category ID is passed in ?pid=
*/
$catID = false;
$Category = false;
$Set = false;
$ParentCat = false;
// Success or failure message
$message = false;
$details = array();
Example #12
0
/**
 * Gets the title of a category from its slug
 *
 * @param string $categorySlug
 * @param string $return
 * @return void
 * @author Drew McLellan
 */
function perch_blog_category($categorySlug, $blog = false, $return = false)
{
    $set = 'blog';
    $categorySlug = rtrim($categorySlug, '/');
    $categoryPath = 'blog/' . $categorySlug . '/';
    if (strpos($categorySlug, '/')) {
        $categoryPath = $categorySlug;
    } else {
        if (is_bool($blog)) {
            $return = $blog;
            $set = 'blog';
        } else {
            $API = new PerchAPI(1.0, 'perch_blog');
            $Blogs = new PerchBlog_Blogs($API);
            $Blog = $Blogs->get_one_by('blogSlug', $blog);
            if ($Blog) {
                $set = $Blog->setSlug();
            }
        }
        $categoryPath = $set . '/' . $categorySlug . '/';
    }
    $cache_key = 'perch_blog_category' . md5($categorySlug);
    $cache = PerchBlog_Cache::get_static($cache_key, 10);
    if ($cache) {
        if ($return) {
            return $cache;
        }
        echo PerchUtil::html($cache);
        return '';
    }
    $Categories = new PerchCategories_Categories();
    $Category = $Categories->get_one_by('catPath', $categoryPath);
    if (is_object($Category)) {
        $r = $Category->catTitle();
        if ($r != '') {
            PerchBlog_Cache::save_static($cache_key, $r);
        }
        if ($return) {
            return $r;
        }
        echo PerchUtil::html($r);
    }
    return false;
}
 public function import_from_wp($wordpress_file, $format = "textile", $callback = false, $sectionID = 1)
 {
     $out = array();
     // LOAD XML
     $xml = simplexml_load_file(PerchUtil::file_path(PERCH_PATH . '/addons/apps/' . $this->api->app_id . '/import_data/' . $wordpress_file));
     // AUTHORS
     $Authors = new PerchBlog_Authors($this->api);
     foreach ($xml->channel->children('wp', true) as $tag) {
         if ($tag->getName() == 'author') {
             $data = array();
             $data['authorEmail'] = (string) $tag->author_email;
             $data['authorSlug'] = PerchUtil::urlify((string) $tag->author_display_name);
             $data['authorGivenName'] = (string) $tag->author_first_name;
             $data['authorFamilyName'] = (string) $tag->author_last_name;
             $data['authorImportRef'] = (string) $tag->author_login;
             if ($data['authorGivenName'] == '') {
                 $data['authorGivenName'] = (string) $tag->author_login;
             }
             $Author = $Authors->find_or_create_by_email((string) $tag->author_email, $data);
             if ($Author) {
                 $out[] = array('type' => 'success', 'messages' => array('Author ' . (string) $tag->author_display_name, 'Successfully imported'));
             }
         }
     }
     // POSTS
     $Posts = new PerchBlog_Posts($this->api);
     $Template = $this->api->get('Template');
     $Template->set('blog/post.html', 'blog');
     foreach ($xml->channel->item as $item) {
         $post = array();
         $post['postTitle'] = (string) $item->title;
         $post['postTags'] = '';
         $post['postLegacyURL'] = parse_url((string) $item->link, PHP_URL_PATH);
         $post_type = false;
         foreach ($item->children('wp', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'post_id':
                     $post['postImportID'] = (string) $tag;
                     break;
                 case 'post_type':
                     $post_type = (string) $tag;
                     break;
                 case 'post_date_gmt':
                     $val = strtotime((string) $tag);
                     if ($val) {
                         $post['postDateTime'] = date('Y-m-d H:i:s', $val);
                     }
                     break;
                 case 'post_date':
                     $val = strtotime((string) $tag);
                     if ($val) {
                         $post['postDateTime'] = date('Y-m-d H:i:s', $val);
                     }
                     break;
                 case 'comment_status':
                     $val = (string) $tag;
                     if ($val == 'open') {
                         $post['postAllowComments'] = '1';
                     } else {
                         $post['postAllowComments'] = '0';
                     }
                     break;
                 case 'post_name':
                     $post['postSlug'] = (string) $tag;
                     break;
                 case 'status':
                     $val = (string) $tag;
                     $post['postStatus'] = 'Draft';
                     if ($val == 'publish') {
                         $post['postStatus'] = 'Published';
                     }
                     break;
             }
         }
         // if it's not of type 'post', skip.
         if ($post_type != 'post') {
             continue;
         }
         // At this point, check we don't already have the post (as we know have the postImportID to identify it)
         if (isset($post['postImportID'])) {
             $Post = $Posts->find_by_importID($post['postImportID']);
             if (is_object($Post)) {
                 $out[] = array('type' => 'success', 'messages' => array('Post ' . $Post->postTitle(), 'Already imported'));
                 continue;
             }
         }
         foreach ($item->children('dc', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'creator':
                     $val = (string) $tag;
                     $Author = $Authors->get_one_by('authorImportRef', $val);
                     if (is_object($Author)) {
                         $post['authorID'] = $Author->id();
                     }
                     break;
             }
         }
         foreach ($item->children('content', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'encoded':
                     $raw = (string) $tag;
                     if ($format == 'textile') {
                         $html = PerchUtil::text_to_html($raw);
                         $post['postDescRaw'] = $raw;
                         $post['postDescHTML'] = $html;
                     } else {
                         $post['postDescRaw'] = $raw;
                         $post['postDescHTML'] = $raw;
                     }
                     break;
             }
         }
         foreach ($item->children('excerpt', true) as $tag) {
             $tagName = $tag->getName();
             switch ($tagName) {
                 case 'encoded':
                     $raw = (string) $tag;
                     $html = PerchUtil::text_to_html($raw);
                     $fields = array();
                     $fields['excerpt'] = array();
                     if ($format == 'textile') {
                         $fields['excerpt']['raw'] = $raw;
                         $fields['excerpt']['processed'] = $html;
                     } else {
                         $fields['excerpt']['raw'] = $html;
                         $fields['excerpt']['processed'] = $html;
                     }
                     $post['postDynamicFields'] = PerchUtil::json_safe_encode($fields);
                     break;
             }
         }
         // Callbacks
         if ($callback) {
             $post = call_user_func($callback, $post, $Template);
         }
         // Section
         $post['sectionID'] = $sectionID;
         // Create the post
         $Post = $Posts->create($post, $Template);
         if (is_object($Post)) {
             $out[] = array('type' => 'success', 'messages' => array('Post ' . $Post->postTitle(), 'Successfully imported'));
             // CATEGORIES AND TAGS
             $Categories = new PerchCategories_Categories();
             $Tags = new PerchBlog_Tags($this->api);
             $postTags = array();
             $cat_ids = array();
             foreach ($item->category as $category) {
                 $attributes = $category->attributes();
                 $slug = (string) $attributes['nicename'];
                 $label = (string) $category;
                 switch ((string) $attributes['domain']) {
                     case 'post_tag':
                         $Tag = $Tags->find_or_create($slug, $label);
                         if (is_object($Tag)) {
                             $postTags[] = $Tag->tagSlug();
                             $out[] = array('type' => 'success', 'messages' => array('Tag ' . $Tag->tagSlug(), 'Successfully imported'));
                         }
                         break;
                     case 'category':
                         PerchUtil::debug("Find or create {$slug}", 'notice');
                         $Category = $Categories->find_or_create('blog/' . $slug . '/', $label);
                         if (is_object($Category)) {
                             $cat_ids[] = $Category->id();
                             $out[] = array('type' => 'success', 'messages' => array('Category ' . $label, 'Successfully imported'));
                         }
                         break;
                 }
             }
             if (PerchUtil::count($postTags)) {
                 $post['postTags'] = implode(', ', $postTags);
             }
             if (PerchUtil::count($cat_ids)) {
                 $fields['categories'] = $cat_ids;
                 $post['postDynamicFields'] = PerchUtil::json_safe_encode($fields);
             }
             $Post->Template = $Template;
             $Post->update($post);
             $Post->index($Template);
             // COMMENTS
             $Comments = new PerchBlog_Comments($this->api);
             foreach ($item->children('wp', true) as $tag) {
                 $tagName = $tag->getName();
                 if ($tagName == 'comment') {
                     if ((string) $tag->comment_type == 'pingback') {
                         continue;
                         // this is a pingback, so skip it.
                     }
                     $html = PerchUtil::text_to_html((string) $tag->comment_content);
                     $comment = array();
                     $comment['postID'] = $Post->id();
                     $comment['commentName'] = (string) $tag->comment_author;
                     $comment['commentEmail'] = (string) $tag->comment_author_email;
                     $comment['commentURL'] = (string) $tag->comment_author_url;
                     $comment['commentIP'] = ip2long((string) $tag->comment_author_IP);
                     $comment['commentDateTime'] = date('Y-m-d H:i:s', strtotime((string) $tag->comment_date_gmt));
                     $comment['commentHTML'] = $html;
                     $comment['commentSpamData'] = '';
                     $comment['commentDynamicFields'] = '';
                     if ((string) $tag->comment_approved == '1') {
                         $comment['commentStatus'] = 'LIVE';
                         $Comment = $Comments->create($comment);
                         $out[] = array('type' => 'success', 'messages' => array('Comment from ' . $comment['commentName'], 'Successfully imported'));
                     }
                 }
             }
             $Post->update_comment_count();
         }
     }
     return $out;
 }
Example #14
0
if (!PerchUtil::count($blogs)) {
    $Posts->attempt_install();
    $blogs = $Blogs->all();
}
$Paging = $API->get('Paging');
$Paging->set_per_page(15);
$Blog = false;
if (PERCH_RUNWAY) {
    if (PerchUtil::get('blog')) {
        $Blog = $Blogs->get_one_by('blogSlug', PerchUtil::get('blog'));
    }
}
if (!$Blog) {
    $Blog = $Blogs->find(1);
}
$Categories = new PerchCategories_Categories();
$categories = $Categories->get_for_set($Blog->setSlug());
$Sections = new PerchBlog_Sections($API);
$sections = $Sections->get_by('blogID', (int) $Blog->id());
$Lang = $API->get('Lang');
$posts = array();
$filter = 'all';
if (isset($_GET['category']) && $_GET['category'] != '') {
    $filter = 'category';
    $category = $_GET['category'];
}
if (isset($_GET['section']) && $_GET['section'] != '') {
    $filter = 'section';
    $section = $_GET['section'];
}
if (isset($_GET['status']) && $_GET['status'] != '') {
/**
 * Gets the title of a category from its slug
 *
 * @param string $categorySlug 
 * @param string $return 
 * @return void
 * @author Drew McLellan
 */
function perch_blog_category($categorySlug, $return = false)
{
    $categorySlug = rtrim($categorySlug, '/');
    $cache_key = 'perch_blog_category' . md5($categorySlug);
    $cache = PerchBlog_Cache::get_static($cache_key, 10);
    if ($cache) {
        if ($return) {
            return $cache;
        }
        echo PerchUtil::html($cache);
        return '';
    }
    $Categories = new PerchCategories_Categories();
    $Category = $Categories->get_one_by('catPath', 'blog/' . $categorySlug . '/');
    if (is_object($Category)) {
        $r = $Category->catTitle();
        if ($r != '') {
            PerchBlog_Cache::save_static($cache_key, $r);
        }
        if ($return) {
            return $r;
        }
        echo PerchUtil::html($r);
    }
    return false;
}