File: D:/HostingSpaces/PvdBoogaard/indoorski.nl/backup/oude-site/cms/api/class.categories.php
<?php
/**
* This file contains the iwp_categories package class
*
* @package IWP
* @subpackage IWP_API
**/
/**
* iwp_categories
* This class extends the iwp_engine abstract class.
* This class handles all operations pertaining to the categories
*
* @package IWP
* @subpackage IWP_API
**/
class iwp_categories extends iwp_engine {
/**
* primaryKey
* This is the name of the field that is the primary key for this table. It is a private and final variable
*
* @var primaryKey
**/
protected $primaryKey = 'categoryid';
/**
* baseTableName
* This is the name of the table without its prefix. This is a final private class variable and cannot be changed.
*
* @var baseTableName
**/
protected $baseTableName = 'categories';
/**
* _columns
* This is the final structure of the table in use. It can not be changed after this declaration.
*
* @var _columns
*
* @see ValidColumn
**/
protected $_columns = array(
'parentid' => 0,
'name' => '',
'description' => '',
'metadesc' => '',
'metakeywords' => '',
'metatitle' => '',
'visits' => 0,
'sortorder' => 0,
'caticon' => '',
'contentcount' => 0,
'sortcache'=>'',
'generateurl'=>1,
'hidefrommenu'=>0,
'haslayout'=>0
);
/**
* data
* The data array of the current row held in the class memory. It should be the same structure as _columns.
*
* @var data
*
* @see Save
* @see Load
* @see LoadMulti
**/
protected $data = array();
/**
* validation
* The array of validation functions to apply to the data values whenever we save to the database.
*
* @var validation
*
* @see Save
* @see iwp_validation
**/
protected $validation = array(
);
/**
* Instance
* This static variable holds the current instance of this object being loaded.
* So using the getInstance function anywhere will return the very same instance.
*
* @var object Instance
*/
public static $Instance;
/**
* This static variable contains permissions that are available to be chosen for setting User Groups permissions.
*
* @var Array
*/
public static $PermissionOptions;
/**
* getInstance
* This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
*
* @return iwp_categories Returns the instantiated object
**/
public static function getInstance(){
if(!isset(self::$Instance)){
self::$Instance = new self();
}
return self::$Instance;
}
public static function GetPermissionOptions () {
return self::$PermissionOptions;
}
/**
* __construct
* The class contructor. Call the parent constructor then reset the data columns
*
* @return void
**/
function __construct(){
parent::__construct();
$this->ResetColumns();
}
/**
* An override to the usual iwp_engine load to automatically decode siteURL placeholders
*
* @param int $id Optional. Id of row to load.
* @return boolean
* @see iwp_engine::Load
*/
public function Load ($id = null) {
$success = parent::Load($id);
if ($success) {
$this->data['description'] = iwp_content::getInstance()->DecodeSiteURLs($this->data['description']);
}
return $success;
}
/**
* An override to the usual iwp_engine loadbyfield to automatically decode siteURL placeholders.
*
* @param $fieldname string The name of the field to load the data by
* @param $fieldvalue string The value of the field to load the data by
* @return void
**/
public function LoadByField ($fieldname, $fieldvalue, $multi = false) {
parent::LoadByField($fieldname, $fieldvalue, $multi);
if (isset($this->data['description'])) {
$this->data['description'] = iwp_content::getInstance()->DecodeSiteURLs($this->data['description']);
}
}
public function getCategoriesWithLayouts () {
$sql = sprintf("SELECT categoryid, name FROM %s where haslayout=1 ORDER BY sortcache", IWP_TABLE_CATEGORIES);
$result = $this->db->Query($sql);
$return = array();
while ($row = $this->db->Fetch($result)) {
$return[] = $row;
}
return $return;
}
/**
* This returns an array of categories using a parentid to start from.
*
* @param integer $parentid The ID number to use as the base parent. Optional, leave blank if you want all categories
* @param integer $level Used in recursion to adjust the indenting
* @return array An array of the categories and children (if any)
*/
function GetCategoriesList ($parentid=0, $level=0) {
if (iwp_IsId($parentid)) {
$sql = sprintf("SELECT categoryid, name, sortcache FROM %s WHERE LEFT(sortcache, CHAR_LENGTH((SELECT sortcache FROM %s WHERE categoryid = %d))) = (SELECT sortcache FROM %s WHERE categoryid = %d) ORDER BY sortcache", IWP_TABLE_CATEGORIES, IWP_TABLE_CATEGORIES, IWP_TABLE_CATEGORIES, $parentid, $parentid);
} else {
$sql = sprintf("SELECT categoryid, name, sortcache FROM %s ORDER BY sortcache", IWP_TABLE_CATEGORIES);
}
$result = $this->db->Query($sql);
$options = array();
while ($row = $this->db->Fetch($result)) {
$level = substr_count($row['sortcache'], '.');
$name = str_repeat(' ', $level * 4) . $row['name']; // 4 spaces per level
$options[$row['categoryid']] = $name;
}
return $options;
}
/**
* This returns an array of categories rss feeds using a parentid to start from.
*
* @param integer $parentid The ID number to use as the base parent. Optional, leave blank if you want all categories
* @param integer $level Used in recursion to adjust the indenting
* @return array An array of the categories and children (if any)
*/
function GetCategoriesRSSFeedList ($parentid=0, $level=0) {
if (iwp_IsId($parentid)) {
$sql = sprintf("SELECT categoryid, name, sortcache FROM %s WHERE LEFT(sortcache, CHAR_LENGTH((SELECT sortcache FROM %s WHERE categoryid = %d))) = (SELECT sortcache FROM %s WHERE categoryid = %d) ORDER BY sortcache", IWP_TABLE_CATEGORIES, IWP_TABLE_CATEGORIES, IWP_TABLE_CATEGORIES, $parentid, $parentid);
} else {
$sql = sprintf("SELECT categoryid, name, sortcache FROM %s ORDER BY sortcache", IWP_TABLE_CATEGORIES);
}
$result = $this->db->Query($sql);
$options = array();
while ($row = $this->db->Fetch($result)) {
$level = substr_count($row['sortcache'], '/');
$url = $this->urls->GetStaticUrl('viewcategoryrss', array('name'=>$row['name'],'idnumber'=>$row['categoryid']));
$options[$row['categoryid']] = array('name'=>$row['name'], 'level'=>$level, 'url'=>$url);
}
return $options;
}
/**
* This returns an associative array of parent categories from the current ID up to the root with the category id as the array key and category name as the value.
*
* @param integer $catid The ID number of the current category to get the parents for
*
* @return array An array of the parent categories
*/
function GetParentsArray ($catid) {
if (!iwp_IsId($catid)) {
return array();
}
$return = array();
$sql = sprintf("SELECT /* iwp_categories::GetParentsArray */ categoryid, name FROM %s WHERE categoryid <> %d AND sortcache = LEFT((SELECT sortcache FROM %s WHERE categoryid = %d), CHAR_LENGTH(sortcache)) ORDER BY sortcache", IWP_TABLE_CATEGORIES, $catid, IWP_TABLE_CATEGORIES, $catid);
$result = $this->db->Query($sql);
while ($row = $this->db->Fetch($result)) {
$return[$row['categoryid']] = $row['name'];
}
return $return;
}
/**
* Returns a granularity list for this class.
*
* @param Integer $total Total will be populated with number of rows found in query (by reference)
* @param String $filter Filter string, optional
* @param Integer $page Page number of records to return, optional
* @return Array List of value/text pairs
*/
public static function getGranularityList (&$total, &$page, $filter = '')
{
$limitStart = ($page * IWP_PERMISSIONGRANULARITEMS_PER_PAGE) - IWP_PERMISSIONGRANULARITEMS_PER_PAGE;
$where = '';
if ($filter) {
$filter = '%'. self::getInstance()->db->Quote($filter) .'%';
$where = sprintf("WHERE (`name` LIKE '%s')", $filter);
}
$result = self::getInstance()->db->Query(sprintf("SELECT SQL_CALC_FOUND_ROWS categoryid AS `value`, `name` AS `text` FROM %s %s ORDER BY `name` LIMIT %d, %d", IWP_TABLE_CATEGORIES, $where, $limitStart, IWP_PERMISSIONGRANULARITEMS_PER_PAGE));
$total = self::getInstance()->db->FetchOne('SELECT found_rows()');
$list = array();
if ($result) {
while ($row = self::getInstance()->db->Fetch($result)) {
$row['value'] = (int)$row['value'];
array_push($list, $row);
}
self::getInstance()->db->FreeResult($result);
}
return $list;
}
/**
* Returns a list of category titles for the supplied ids
*
* @param Array $ids
* @return Array
*/
public static function getTitleList (&$ids)
{
$list = array();
if (count($ids)) {
$me = self::getInstance();
$sql = sprintf("SELECT categoryid as `value`, `name` as `text` FROM %s WHERE categoryid IN (%s) ORDER BY `name`", IWP_TABLE_CATEGORIES, implode(',', $ids));
$result = $me->db->Query($sql);
while ($row = $me->db->Fetch($result)) {
$row['value'] = (int)$row['value'];
array_push($list, $row);
}
}
return $list;
}
/**
* Internal static cache used for category id => name/url resolution.
*
* @var array
*/
public static $categoryNameURLCache = array();
/**
* usort sorting callback used inside GetCategoryListByIdList to sort categories by name
*
* @param array $a Category data
* @param array $b Category data
* @return int
*/
public static function sortByNameCallback ($a, $b) {
return strcoll($a['name'], $b['name']);
}
/**
* This function updates the number of content items associated with a category
*
* @param integer $categoryId Optional. The ID number of the category to update. If blank or zero, all categories will be updated.
* @return boolean The boolean result of the call to the mysqldb::Query() function.
*/
public function RefreshContentCount($categoryId=0) {
$categoryId = (int)$categoryId;
if($categoryId > 0) {
return $this->db->query('update ' . IWP_TABLE_CATEGORIES . ' as c set contentcount=(select count(*) from ' . IWP_TABLE_CATASSOC . ' as ca where ca.categoryid=c.categoryid) where c.categoryid=' . $categoryId);
}else{
return $this->db->query('update ' . IWP_TABLE_CATEGORIES . ' as c set contentcount=(select count(*) from ' . IWP_TABLE_CATASSOC . ' as ca where ca.categoryid=c.categoryid)');
}
}
/**
* Returns an HTML anchor list, separated by commas, based on the given CSV string of category IDs
*
* @param string $csv CSV string of category IDs e.g. 1,2,3,4
* @return string
*/
public function GetCategoryListByIdList ($csv) {
$idList= $this->valid->FilterCsvToArray($csv);
if (!sizeof($idList)) {
return false;
}
$categoryList = array();
$lookupCategories = array();
foreach ($idList as $categoryId) {
if (isset(self::$categoryNameURLCache[$categoryId])) {
$categoryList[$categoryId] = self::$categoryNameURLCache[$categoryId];
continue;
}
$lookupCategories[] = $categoryId;
}
if (!empty($lookupCategories)) {
$sql = "SELECT /* iwp_categories#GetCategoryListByIdList */ categoryid, name FROM " . IWP_TABLE_CATEGORIES . " WHERE categoryid IN (" . implode(',', $lookupCategories) . ")";
$result = $this->db->Query($sql);
while ($row = $this->db->Fetch($result)) {
$categoryId = intval($row['categoryid']);
$name = $row['name'];
self::$categoryNameURLCache[$categoryId] = array(
'name' => $name,
'url' => $this->urls->ViewCategoryURL($categoryId),
);
$categoryList[$categoryId] = self::$categoryNameURLCache[$categoryId];
}
}
usort($categoryList, array('iwp_categories', 'sortByNameCallback'));
$linkOutput = array();
foreach ($categoryList as $category) {
$linkOutput[] = $this->output->LinkTag(IWP_BASE_URI . $category['url'], $category['name']);
}
return implode(', ', $linkOutput);
}
public function GetDataById($id){
if(!iwp_IsId($id)){
return array();
}
$self = new self();
$self->Load($id);
return $self->GetData();
}
}
iwp_categories::$PermissionOptions = array(
'full' => new iwp_permissionoption(true, false, false),
'create' => new iwp_permissionoption(false, false, false),
'delete' => new iwp_permissionoption(true, false, false),
'edit' => new iwp_permissionoption(true, false, false)
);