public function find_or_create($catPath, $label = false)
 {
     $Cat = $this->get_one_by('catPath', $catPath);
     if (is_object($Cat)) {
         return $Cat;
     }
     $path_parts = explode('/', $catPath);
     $setSlug = array_shift($path_parts);
     array_pop($path_parts);
     $catSlug = array_pop($path_parts);
     $Sets = new PerchCategories_Sets($this->api);
     $Set = $Sets->get_one_by('setSlug', $setSlug);
     if (is_object($Set)) {
         $data = array('setID' => $Set->id(), 'catTitle' => $label, 'catSlug' => $catSlug, 'catPath' => $catPath, 'catDisplayPath' => '', 'catParentID' => 0);
         $Cat = $this->create($data);
         return $Cat;
     }
     return false;
 }
 public function update_meta($fire_events = true)
 {
     $data = array();
     if ($this->catParentID() !== '0' && $this->catParentID() !== 'null') {
         $Categories = new PerchCategories_Categories();
         $ParentCat = $Categories->find($this->catParentID());
         $data['catPath'] = $ParentCat->catPath() . $this->catSlug() . '/';
         $data['catTreePosition'] = $ParentCat->catTreePosition() . '-' . str_pad($this->catOrder(), 3, '0', STR_PAD_LEFT);
     } else {
         $Sets = new PerchCategories_Sets();
         $Set = $Sets->find($this->setID());
         $data['catPath'] = $Set->setSlug() . '/' . $this->catSlug() . '/';
         $data['catTreePosition'] = str_pad($this->setID(), 3, '0', STR_PAD_LEFT) . '-' . str_pad($this->catOrder(), 3, '0', STR_PAD_LEFT);
     }
     $data['catDisplayPath'] = $this->get_display_path();
     if (count($data)) {
         parent::update($data);
         if ($fire_events) {
             $Perch = Perch::fetch();
             $Perch->event($this->event_prefix . '.update', $this);
         }
     }
 }
<?php

if (!PERCH_RUNWAY) {
    exit;
}
$Blogs = new PerchBlog_Blogs($API);
$CategorySets = new PerchCategories_Sets($API);
$category_sets = $CategorySets->all();
$HTML = $API->get('HTML');
$Form = $API->get('Form');
$message = false;
if (!$CurrentUser->has_priv('perch_blog.blogs.manage')) {
    PerchUtil::redirect($API->app_path());
}
if (isset($_GET['id']) && $_GET['id'] != '') {
    $blogID = (int) $_GET['id'];
    $Blog = $Blogs->find($blogID);
    $details = $Blog->to_array();
} else {
    $blogID = false;
    $Blog = false;
    $details = array();
}
$Template = $API->get('Template');
$Template->set('blog/blog.html', 'blog');
$Form->handle_empty_block_generation($Template);
$tags = $Template->find_all_tags_and_repeaters();
$Form->require_field('blogTitle', 'Required');
$Form->set_required_fields_from_template($Template, $details);
if ($Form->submitted()) {
    $postvars = array('blogTitle', 'setSlug', 'postTemplate');
<?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);
 $sql = "ALTER TABLE `" . PERCH_DB_PREFIX . "blog_posts` ADD `sectionID` INT(10)  UNSIGNED  NOT NULL  DEFAULT '1'  AFTER `authorID`";
 $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']);
             }
<?php

if (!defined('PERCH_DB_PREFIX')) {
    exit;
}
$sql = "\n    CREATE TABLE `__PREFIX__root_locator_addresses` (\n      `addressID` int(11) unsigned NOT NULL AUTO_INCREMENT,\n      `addressTitle` varchar(255) NOT NULL DEFAULT '',\n      `addressBuilding` varchar(255) DEFAULT '',\n      `addressStreet` varchar(255) DEFAULT '',\n      `addressTown` varchar(255) DEFAULT '',\n      `addressRegion` varchar(255) DEFAULT '',\n      `addressPostcode` varchar(15) DEFAULT '',\n      `addressCountry` varchar(3) DEFAULT '',\n      `addressLatitude` decimal(9,6),\n      `addressLongitude` decimal(9,6),\n      `addressError` varchar(255),\n      `addressDynamicFields` text,\n      `addressUpdated` datetime NOT NULL,\n      PRIMARY KEY (`addressID`),\n      FULLTEXT KEY `root_locator_search_index` (`addressTitle`,`addressBuilding`,`addressStreet`,`addressPostcode`)\n    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;\n    \n    CREATE TABLE `__PREFIX__root_locator_index` (\n      `indexID` int(10) NOT NULL AUTO_INCREMENT,\n      `itemKey` char(64) NOT NULL DEFAULT '-',\n      `itemID` int(10) NOT NULL DEFAULT '0',\n      `indexKey` char(64) NOT NULL DEFAULT '-',\n      `indexValue` char(255) NOT NULL DEFAULT '',\n      PRIMARY KEY (`indexID`),\n      KEY `idx_fk` (`itemKey`,`itemID`),\n      KEY `idx_key` (`indexKey`),\n      KEY `idx_key_val` (`indexKey`,`indexValue`),\n      KEY `idx_keys` (`itemKey`,`indexKey`)\n    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;\n    \n    CREATE TABLE `__PREFIX__root_locator_tasks` (\n      `taskID` int(11) unsigned NOT NULL AUTO_INCREMENT,\n      `taskKey` VARCHAR(255) NOT NULL,\n      `addressID` int(11) unsigned NOT NULL,\n      `taskAttempt` int(1) unsigned NOT NULL DEFAULT 1,\n      `taskStart` datetime NOT NULL,\n      PRIMARY KEY (`taskID`)\n    ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;\n";
$sql = str_replace('__PREFIX__', PERCH_DB_PREFIX, $sql);
// Install
$statements = explode(';', $sql);
foreach ($statements as $statement) {
    $statement = trim($statement);
    if ($statement != '') {
        $this->db->execute($statement);
    }
}
// Permissions
$API = new PerchAPI(1.0, 'root_locator');
$UserPrivileges = $API->get('UserPrivileges');
$UserPrivileges->create_privilege('root_locator', 'Access the locator app');
$UserPrivileges->create_privilege('root_locator.import', 'Mass import location data');
// Categories
$Core_CategorySets = new PerchCategories_Sets();
$Core_Categories = new PerchCategories_Categories();
$Set = $Core_CategorySets->get_by('setSlug', 'locator');
if (!$Set) {
    $Set = $Core_CategorySets->create(array('setTitle' => PerchLang::get('Locator'), 'setSlug' => 'locator', 'setTemplate' => '~/root_locator/templates/locator/category_set.html', 'setCatTemplate' => '~/root_locator/templates/locator/category.html', 'setDynamicFields' => '[]'));
}
// Installation check
$sql = 'SHOW TABLES LIKE "' . $this->table . '"';
$result = $this->db->get_value($sql);
return $result;
<?php

$Paging = new PerchPaging();
$Paging->set_per_page(24);
$API = new PerchAPI(1.0, 'categories');
$HTML = $API->get('HTML');
$Sets = new PerchCategories_Sets();
$Categories = new PerchCategories_Categories();
$sets = $Sets->all($Paging);
if (!file_exists(PERCH_TEMPLATE_PATH . '/categories')) {
    $Alert->set('notice', PerchLang::get('The category templates could not be found. Please copy them to %s', '<code>' . PERCH_TEMPLATE_PATH . '/categories' . '</code>'));
}
/*
		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;
<?php

$default_fields = '<perch:categories id="setTitle" type="smarttext" label="Title" required="true" />
					   <perch:categories id="setSlug" type="slug" for="setTitle" />';
$API = new PerchAPI(1.0, 'categories');
$HTML = $API->get('HTML');
$Sets = new PerchCategories_Sets();
$setID = false;
$Set = false;
$message = false;
$details = array();
$template = 'set.html';
if (PerchUtil::get('id')) {
    $setID = (int) PerchUtil::get('id');
    $Set = $Sets->find($setID);
    $template = $Set->setTemplate();
    $details = $Set->to_array();
}
if (!$CurrentUser->has_priv('categories.manage')) {
    PerchUtil::redirect(PERCH_LOGINPATH . '/core/apps/categories/');
}
$Template = $API->get('Template');
$Template->set('categories/' . $template, 'categories', $default_fields);
$Template->disable_feature('categories');
$Form = $API->get('Form');
$Form->handle_empty_block_generation($Template);
$Form->set_required_fields_from_template($Template, $details);
if ($Form->submitted()) {
    $fixed_fields = $Form->receive(array('setTemplate', 'setCatTemplate'));
    $data = $Form->get_posted_content($Template, $Sets, $Set);
    $data = array_merge($data, $fixed_fields);