PHP Class CategoryModel, vanilla

Inheritance: extends Model
Afficher le fichier Open project: vanilla/vanilla Class Usage Examples

Méthodes publiques

Свойство Type Description
$Categories Merged Category data, including Pure + UserCategory.
$JoinRecentUsers Whether or not to join users to recent posts. Forums with a lot of categories may need to optimize using this setting and simpler views.
$ShardCache Whether or not to explicitly shard the categories cache.
$Watching boolean

Méthodes publiques

Méthode Description
__construct ( ) Class constructor. Defines the related database table name.
addCategoryColumns ( object $Data ) Modifies category data before it is returned.
allowedDiscussionTypes ( array $PermissionCategory, array $category = [] ) : array Checks the allowed discussion types on a category.
applyUpdates ( ) If looking at the root node, make sure it exists and that the nested set columns exist in the table.
calculateDisplayAs ( &$category ) Maintains backwards compatibilty with DisplayAs: Default-type categories by calculating the DisplayAs property into an expected DisplayAs type: Categories, Heading, or Discussions. Respects the now-deprecated config setting Vanilla.Categories.DoHeadings. Once we can be sure that all instances have their categories' DisplayAs properties explicitly set in the database (i.e., not Default) we can deprecate/remove this function.
categories ( integer | string | boolean $ID = false ) : array Gets either all of the categories or a single category.
categoryUrl ( $Category, string $Page = '', boolean | true $WithDomain = true ) : string Build URL to a category page.
categoryWatch ( $AllDiscussions = true ) : array
checkPermission ( array $category, string $permission ) : boolean Check a category's permission.
clearCache ( )
clearUserCache ( )
counts ( $Column ) : array
createCollection ( Gdn_SQLDriver $sql = null, Gdn_Cache $cache = null ) : CategoryCollection Create a new category collection tied to this model.
defaultCategory ( ) : mixed
delete ( $where = [], $options = [] ) Delete a category.
deleteAndReplace ( object $category, integer $newCategoryID ) Delete a single category and assign its discussions to another.
deleteID ( integer $categoryID, array $options = [] ) : boolean Delete a category.
filterCategoryPermissions ( array $categoryIDs ) : array Remove categories that a user does not have permission to view.
filterChildren ( array &$categories, string $childField = 'Children' ) Recursively remove children from categories configured to display as "Categories" or "Flat".
filterFollowing ( array $categories ) : array Filter a category tree to only the followed categories.
flattenTree ( array $categories ) : array Return a flattened version of a tree.
get ( string $OrderFields = '', string $OrderDirection = 'asc', integer $Limit = false, integer $Offset = false ) : Gdn_DataSet Get list of categories (respecting user permission).
getAll ( ) : object Get list of categories (disregarding user permission for admins).
getAncestors ( $categoryID, boolean $checkPermissions = true, boolean $includeHeadings = false ) : array Get all of the ancestor categories above this one.
getByCode ( $Code ) : object Get data for a single category selected by Url Code. Disregards permissions.
getByPermission ( string $Permission = 'Discussions.Add', null $CategoryID = null, array $Filter = [], array $PermFilter = [] ) : array
getChildTree ( integer | string $id, array $options = [] ) : array Get a category tree based on, but not including a parent category.
getChildren ( integer $categoryID ) Get the child categories of a category.
getDescendantCountByCode ( $Code ) Return the number of descendants for a specific category.
getDescendantsByCode ( string $Code ) : object
getDisplayAsOptions ( ) : array
getFiltered ( array $RestrictIDs = false, string $Permissions = false, array $ExcludeWhere = false ) : Gdn_DataSet Get a list of categories, considering several filters
getFull ( $CategoryID = false, $Permissions = false )
getFullByUrlCode ( string $UrlCode ) : object Get full data for a single category by its URL slug. Respects permissions.
getID ( integer $categoryID, string $datasetType = DATASET_TYPE_OBJECT, array $options = [] ) : object | array Get data for a single category selected by ID. Disregards permissions.
getMaxDisplayDepth ( ) : integer Get the maximum display depth for categories.
getNavDepth ( ) : integer Get the category nav depth.
getRolePermissions ( integer $categoryID ) : array Get the role specific permissions for a category.
getRootDisplayAs ( ) : string Get the display type for the root category.
getSubtree ( string $parentCategory, boolean $includeParent = true ) : array Get the subtree starting at a given parent.
getTreeAsFlat ( integer | string $id, integer | null $offset = null, integer | null $limit = null, string | null $filter = null, string $orderFields = 'Name', string $orderDirection = 'asc' ) : array
getWhereCache ( array $Where ) : array A simplified version of GetWhere that polls the cache instead of the database.
givePoints ( integer $UserID, integer $Points, string $Source = 'Other', integer $CategoryID, integer $Timestamp = false ) Give a user points specific to this category.
hasChildren ( string $CategoryID ) : boolean Check whether category has any children categories.
instance ( ) : CategoryModel The shared instance of this object.
joinCategories ( &$Data, string $Column = 'CategoryID', array $Options = [] )
joinModerators ( array $Data, string $Permission = 'Vanilla.Comments.Edit', string $Column = 'Moderators' )
joinRecent ( &$categoryTree ) Join recent posts and users to a category tree.
joinRecentChildPosts ( null &$Category = null, null &$Categories = null )
joinRecentPosts ( &$Data, null $CategoryID = null ) : boolean
joinUserCategory ( ) : boolean Whether or not to join information from GDN_UserCategory in {@link CategoryModel::calculateUser()}.
joinUserData ( &$Categories, boolean $AddUserCategory = true ) Add UserCategory modifiers
makeTree ( $Categories, null $Root = null ) : array
permissionCategory ( mixed $Category ) Return the category that contains the permissions for the given category.
rebuildTree ( boolean $BySort = false ) Rebuilds the category tree. We are using the Nested Set tree model.
recalculateTree ( ) Recalculate the dynamic tree columns in the category.
save ( $FormPostValues, array | false $Settings = false ) : integer Saves the category.
saveSubtree ( array $subtree, $parentID ) Save a subtree.
saveTree ( array $TreeArray ) Saves the category tree based on a provided tree array. We are using the Nested Set tree model.
saveUserTree ( integer $CategoryID, mixed $Set ) Grab the Category IDs of the tree.
setCache ( integer | boolean $ID = false, array | boolean $Data = false ) Grab and update the category cache
setField ( integer $ID, array | string $Property, boolean | false $Value = false ) : array | string Set a property on a category.
setJoinUserCategory ( boolean $joinUserCategory ) : CategoryModel Set whether or not to join information from GDN_UserCategory in {@link CategoryModel::calculateUser()}.
setLocalField ( $ID, $Property, $Value ) : boolean Set a property of a currently-loaded category in memory.
setRecentPost ( $CategoryID )
setStopHeadingsCalculation ( boolean $stopHeadingCalculation ) : CategoryModel Checks to see if the passed category depth is greater than the NavDepth and if so, stops calculating Headings as a DisplayAs property in the calculateDisplayAs method. Once we can be sure that all instances have their categories' DisplayAs properties explicitly set in the database (i.e., not Default) we can deprecate/remove this function.
where ( $Row, $Where ) : boolean

Méthodes protégées

Méthode Description
_MakeTreeChildren ( $Category, $Categories, null $DepthAdj = null ) : array
_SetTree ( array &$Node, integer $Left = 1, integer $Depth )
_treeSort ( $A, $B ) : integer Utility method for sorting via usort.
buildCache ( integer $CategoryID = null ) Build and augment the category cache.
rebuildLock ( boolean $release = false ) : boolean Request rebuild mutex.

Private Methods

Méthode Description
calculate ( &$category ) Calculate the dynamic fields of a category.
calculateData ( array &$Data ) Build calculated category data on the passed set.
calculateUser ( &$category, $addUserCategory = null ) Calculate the user-specific information on a category.
castID ( integer | string | null $category ) : integer | string | null Cast a category ID or slug to be passed to the various {@link CategoryCollection} methods.
gatherLastIDs ( array $categoryTree, &$result = null ) Gather all of the last discussion and comment IDs from the categories.
getOne ( string | integer $id ) Get a single category from the collection.
getUserCategories ( ) : array | mixed Get the per-category information for the current user.
joinRecentInternal ( &$categoryTree, array $discussions, array $comments ) This method supports {@link CategoryModel::joinRecent()}.
loadAllCategories ( ) Load all of the categories from the cache or the database.
saveSubtreeInternal ( array $subtree, integer | null $parentID = null, boolean $rebuild = true ) Save a subtree.

Method Details

_MakeTreeChildren() protected static méthode

protected static _MakeTreeChildren ( $Category, $Categories, null $DepthAdj = null ) : array
$Category
$Categories
$DepthAdj null
Résultat array

_SetTree() protected méthode

Since: 2.0.18
protected _SetTree ( array &$Node, integer $Left = 1, integer $Depth )
$Node array
$Left integer
$Depth integer

__construct() public méthode

Class constructor. Defines the related database table name.
Since: 2.0.0
public __construct ( )

_treeSort() protected méthode

Utility method for sorting via usort.
Since: 2.0.18
protected _treeSort ( $A, $B ) : integer
$A First element to compare.
$B Second element to compare.
Résultat integer -1, 1, 0 (per usort)

addCategoryColumns() public static méthode

Adds CountAllDiscussions column to each category representing the sum of discussions within this category as well as all subcategories.
Since: 2.0.17
public static addCategoryColumns ( object $Data )
$Data object SQL result.

allowedDiscussionTypes() public static méthode

Checks the allowed discussion types on a category.
public static allowedDiscussionTypes ( array $PermissionCategory, array $category = [] ) : array
$PermissionCategory array The permission category of the category.
$category array The category we're checking the permission on.
Résultat array The allowed discussion types on the category.

applyUpdates() public méthode

If looking at the root node, make sure it exists and that the nested set columns exist in the table.
Since: 2.0.15
public applyUpdates ( )

buildCache() protected static méthode

Build and augment the category cache.
protected static buildCache ( integer $CategoryID = null )
$CategoryID integer The category to

calculateDisplayAs() public static méthode

Maintains backwards compatibilty with DisplayAs: Default-type categories by calculating the DisplayAs property into an expected DisplayAs type: Categories, Heading, or Discussions. Respects the now-deprecated config setting Vanilla.Categories.DoHeadings. Once we can be sure that all instances have their categories' DisplayAs properties explicitly set in the database (i.e., not Default) we can deprecate/remove this function.
public static calculateDisplayAs ( &$category )
$category The category to calculate the DisplayAs property for.

categories() public static méthode

Gets either all of the categories or a single category.
Since: 2.0.18
public static categories ( integer | string | boolean $ID = false ) : array
$ID integer | string | boolean Either the category ID or the category url code. If nothing is passed then all categories are returned.
Résultat array Returns either one or all categories.

categoryUrl() public static méthode

Build URL to a category page.
public static categoryUrl ( $Category, string $Page = '', boolean | true $WithDomain = true ) : string
$Category
$Page string
$WithDomain boolean | true
Résultat string

categoryWatch() public static méthode

Since: 2.0.18
public static categoryWatch ( $AllDiscussions = true ) : array
Résultat array Category IDs.

checkPermission() public static méthode

Check a category's permission.
public static checkPermission ( array $category, string $permission ) : boolean
$category array The category to check.
$permission string The permission name to check.
Résultat boolean Returns **true** if the current user has the permission or **false** otherwise.

clearCache() public static méthode

public static clearCache ( )

clearUserCache() public static méthode

public static clearUserCache ( )

counts() public méthode

public counts ( $Column ) : array
$Column
Résultat array

createCollection() public méthode

Create a new category collection tied to this model.
public createCollection ( Gdn_SQLDriver $sql = null, Gdn_Cache $cache = null ) : CategoryCollection
$sql Gdn_SQLDriver
$cache Gdn_Cache
Résultat CategoryCollection Returns a new collection.

defaultCategory() public static méthode

public static defaultCategory ( ) : mixed
Résultat mixed

delete() public méthode

public delete ( $where = [], $options = [] )

deleteAndReplace() public méthode

Delete a single category and assign its discussions to another.
Since: 2.0.0
public deleteAndReplace ( object $category, integer $newCategoryID )
$category object
$newCategoryID integer Unique ID of category all discussion are being move to.

deleteID() public méthode

Delete a category.
public deleteID ( integer $categoryID, array $options = [] ) : boolean
$categoryID integer The ID of the category to delete.
$options array An array of options to affect the behavior of the delete. - **newCategoryID**: The new category to point discussions to.
Résultat boolean Returns **true** on success or **false** otherwise.

filterCategoryPermissions() public static méthode

Remove categories that a user does not have permission to view.
public static filterCategoryPermissions ( array $categoryIDs ) : array
$categoryIDs array An array of categories to filter.
Résultat array Returns an array of category IDs that are okay to view.

filterChildren() public static méthode

Recursively remove children from categories configured to display as "Categories" or "Flat".
public static filterChildren ( array &$categories, string $childField = 'Children' )
$categories array
$childField string

filterFollowing() public méthode

Filter a category tree to only the followed categories.
public filterFollowing ( array $categories ) : array
$categories array The category tree to filter.
Résultat array Returns a category tree.

flattenTree() public static méthode

Return a flattened version of a tree.
public static flattenTree ( array $categories ) : array
$categories array The category tree.
Résultat array Returns the flattened category tree.

get() public méthode

Get list of categories (respecting user permission).
Since: 2.0.0
public get ( string $OrderFields = '', string $OrderDirection = 'asc', integer $Limit = false, integer $Offset = false ) : Gdn_DataSet
$OrderFields string Ignored.
$OrderDirection string Ignored.
$Limit integer Ignored.
$Offset integer Ignored.
Résultat Gdn_DataSet SQL results.

getAll() public méthode

Get list of categories (disregarding user permission for admins).
Since: 2.0.0
public getAll ( ) : object
Résultat object SQL results.

getAncestors() public static méthode

Get all of the ancestor categories above this one.
public static getAncestors ( $categoryID, boolean $checkPermissions = true, boolean $includeHeadings = false ) : array
$checkPermissions boolean Whether or not to only return the categories with view permission.
$includeHeadings boolean Whether or not to include heading categories.
Résultat array

getByCode() public méthode

Get data for a single category selected by Url Code. Disregards permissions.
Since: 2.0.0
public getByCode ( $Code ) : object
Résultat object SQL results.

getByPermission() public static méthode

public static getByPermission ( string $Permission = 'Discussions.Add', null $CategoryID = null, array $Filter = [], array $PermFilter = [] ) : array
$Permission string
$CategoryID null
$Filter array
$PermFilter array
Résultat array

getChildTree() public méthode

Get a category tree based on, but not including a parent category.
public getChildTree ( integer | string $id, array $options = [] ) : array
$id integer | string The parent category ID or slug.
$options array See {@link CategoryCollection::getTree()}.
Résultat array Returns an array of categories with child categories in the **Children** key.

getChildren() public static méthode

Get the child categories of a category.
public static getChildren ( integer $categoryID )
$categoryID integer The category to get the children of.

getDescendantCountByCode() public méthode

Return the number of descendants for a specific category.
public getDescendantCountByCode ( $Code )

getDescendantsByCode() public méthode

Since: 2.0.18
public getDescendantsByCode ( string $Code ) : object
$Code string Where condition.
Résultat object DataSet

getDisplayAsOptions() public static méthode

public static getDisplayAsOptions ( ) : array
Résultat array

getFiltered() public méthode

Get a list of categories, considering several filters
public getFiltered ( array $RestrictIDs = false, string $Permissions = false, array $ExcludeWhere = false ) : Gdn_DataSet
$RestrictIDs array Optional list of category ids to mask the dataset
$Permissions string Optional permission to require. Defaults to Vanilla.Discussions.View.
$ExcludeWhere array Exclude categories with any of these flags
Résultat Gdn_DataSet

getFull() public méthode

public getFull ( $CategoryID = false, $Permissions = false )

getFullByUrlCode() public méthode

Get full data for a single category by its URL slug. Respects permissions.
Since: 2.0.0
public getFullByUrlCode ( string $UrlCode ) : object
$UrlCode string Unique category slug from URL.
Résultat object SQL results.

getID() public méthode

Get data for a single category selected by ID. Disregards permissions.
Since: 2.0.0
public getID ( integer $categoryID, string $datasetType = DATASET_TYPE_OBJECT, array $options = [] ) : object | array
$categoryID integer The unique ID of category we're getting data for.
$datasetType string Not used.
$options array Not used.
Résultat object | array SQL results.

getMaxDisplayDepth() public méthode

Get the maximum display depth for categories.
public getMaxDisplayDepth ( ) : integer
Résultat integer Returns the display depth as an integer.

getNavDepth() public méthode

Get the category nav depth.
public getNavDepth ( ) : integer
Résultat integer Returns the nav depth as an integer.

getRolePermissions() public méthode

Get the role specific permissions for a category.
public getRolePermissions ( integer $categoryID ) : array
$categoryID integer The ID of the category to get the permissions for.
Résultat array Returns an array of permissions.

getRootDisplayAs() public static méthode

Get the display type for the root category.
public static getRootDisplayAs ( ) : string
Résultat string

getSubtree() public static méthode

Get the subtree starting at a given parent.
Since: 2.0.18
public static getSubtree ( string $parentCategory, boolean $includeParent = true ) : array
$parentCategory string The ID or url code of the parent category.
$includeParent boolean Whether or not to include the parent in the result.
Résultat array An array of categories.

getTreeAsFlat() public méthode

public getTreeAsFlat ( integer | string $id, integer | null $offset = null, integer | null $limit = null, string | null $filter = null, string $orderFields = 'Name', string $orderDirection = 'asc' ) : array
$id integer | string The parent category ID or slug.
$offset integer | null Offset results by given value.
$limit integer | null Total number of results should not exceed this value.
$filter string | null Restrict results to only those with names matching this value, if provided.
$orderFields string
$orderDirection string
Résultat array

getWhereCache() public méthode

A simplified version of GetWhere that polls the cache instead of the database.
Since: 2.2.2
public getWhereCache ( array $Where ) : array
$Where array
Résultat array

givePoints() public static méthode

Give a user points specific to this category.
public static givePoints ( integer $UserID, integer $Points, string $Source = 'Other', integer $CategoryID, integer $Timestamp = false )
$UserID integer The user to give the points to.
$Points integer The number of points to give.
$Source string The source of the points.
$CategoryID integer The category to give the points for.
$Timestamp integer The time the points were given.

hasChildren() public méthode

Check whether category has any children categories.
Since: 2.0.0
public hasChildren ( string $CategoryID ) : boolean
$CategoryID string Unique ID for category being checked.
Résultat boolean

instance() public static méthode

The shared instance of this object.
public static instance ( ) : CategoryModel
Résultat CategoryModel Returns the instance.

joinCategories() public static méthode

Since: 2.0.18
public static joinCategories ( &$Data, string $Column = 'CategoryID', array $Options = [] )
$Column string Name of database column.
$Options array The 'Join' key may contain array of columns to join on.

joinModerators() public static méthode

Since: 2.0.0
public static joinModerators ( array $Data, string $Permission = 'Vanilla.Comments.Edit', string $Column = 'Moderators' )
$Data array
$Permission string
$Column string

joinRecent() public méthode

Join recent posts and users to a category tree.
public joinRecent ( &$categoryTree )

joinRecentChildPosts() public static méthode

public static joinRecentChildPosts ( null &$Category = null, null &$Categories = null )
$Category null
$Categories null

joinRecentPosts() public static méthode

public static joinRecentPosts ( &$Data, null $CategoryID = null ) : boolean
$Data
$CategoryID null
Résultat boolean

joinUserCategory() public méthode

You only need the information from this table when looking at categories in a list. Controllers should set this flag if they are going to be sending read/unread information with the category.
public joinUserCategory ( ) : boolean
Résultat boolean Returns the joinUserCategory.

joinUserData() public static méthode

Update &$Categories in memory by applying modifiers from UserCategory for the currently logged-in user.
Since: 2.0.18
public static joinUserData ( &$Categories, boolean $AddUserCategory = true )
$AddUserCategory boolean

makeTree() public static méthode

public static makeTree ( $Categories, null $Root = null ) : array
$Categories
$Root null
Résultat array

permissionCategory() public static méthode

Return the category that contains the permissions for the given category.
Since: 2.2
public static permissionCategory ( mixed $Category )
$Category mixed

rebuildLock() protected static méthode

Allows competing instances to "vote" on the process that gets to rebuild the category cache.
protected static rebuildLock ( boolean $release = false ) : boolean
$release boolean
Résultat boolean whether we may rebuild

rebuildTree() public méthode

Rebuilds the category tree. We are using the Nested Set tree model.
Since: 2.0.0
public rebuildTree ( boolean $BySort = false )
$BySort boolean Rebuild the tree by sort order instead of existing tree order.

recalculateTree() public méthode

Recalculate the dynamic tree columns in the category.
public recalculateTree ( )

save() public méthode

Saves the category.
Since: 2.0.0
public save ( $FormPostValues, array | false $Settings = false ) : integer
$Settings array | false Additional settings to affect saving.
Résultat integer ID of the saved category.

saveSubtree() public méthode

Save a subtree.
public saveSubtree ( array $subtree, $parentID )
$subtree array A nested array where each array contains a CategoryID and optional Children element.

saveTree() public méthode

TreeArray comes in the format: '0' ... 'item_id' => "root" 'parent_id' => "none" 'depth' => "0" 'left' => "1" 'right' => "34" '1' ... 'item_id' => "1" 'parent_id' => "root" 'depth' => "1" 'left' => "2" 'right' => "3" etc...
Since: 2.0.16
public saveTree ( array $TreeArray )
$TreeArray array A fully defined nested set model of the category tree.

saveUserTree() public méthode

Grab the Category IDs of the tree.
Since: 2.0.18
public saveUserTree ( integer $CategoryID, mixed $Set )
$CategoryID integer
$Set mixed

setCache() public static méthode

Grab and update the category cache
Since: 2.0.18
public static setCache ( integer | boolean $ID = false, array | boolean $Data = false )
$ID integer | boolean
$Data array | boolean

setField() public méthode

Set a property on a category.
public setField ( integer $ID, array | string $Property, boolean | false $Value = false ) : array | string
$ID integer
$Property array | string
$Value boolean | false
Résultat array | string

setJoinUserCategory() public méthode

Set whether or not to join information from GDN_UserCategory in {@link CategoryModel::calculateUser()}.
public setJoinUserCategory ( boolean $joinUserCategory ) : CategoryModel
$joinUserCategory boolean The new value to set.
Résultat CategoryModel Returns `$this` for fluent calls.

setLocalField() public static méthode

Set a property of a currently-loaded category in memory.
public static setLocalField ( $ID, $Property, $Value ) : boolean
$ID
$Property
$Value
Résultat boolean

setRecentPost() public méthode

public setRecentPost ( $CategoryID )
$CategoryID

setStopHeadingsCalculation() public méthode

Checks to see if the passed category depth is greater than the NavDepth and if so, stops calculating Headings as a DisplayAs property in the calculateDisplayAs method. Once we can be sure that all instances have their categories' DisplayAs properties explicitly set in the database (i.e., not Default) we can deprecate/remove this function.
public setStopHeadingsCalculation ( boolean $stopHeadingCalculation ) : CategoryModel
$stopHeadingCalculation boolean
Résultat CategoryModel

where() public static méthode

public static where ( $Row, $Where ) : boolean
$Row
$Where
Résultat boolean

Property Details

$Categories public_oe static_oe property

Merged Category data, including Pure + UserCategory.
public static $Categories

$JoinRecentUsers public_oe property

Whether or not to join users to recent posts. Forums with a lot of categories may need to optimize using this setting and simpler views.
public $JoinRecentUsers

$ShardCache public_oe static_oe property

Whether or not to explicitly shard the categories cache.
public static $ShardCache

$Watching public_oe property

public bool $Watching
Résultat boolean