3 Extension Name: CategoryRoles
4 Extension Url: http://lussumo.com/addons/?PostBackAction=AddOn&AddOnID=230
5 Description: Add a way to define distinct roles level for adding comment and opening a new discussion in a given category.
8 Author Url: http://ac-mb.info
12 0.2 added check for install and creation of database column
13 0.3 corrected error on new category
14 corrected bug on indexes when removing categories from select
15 0.4 Corrected a misuse of DicussionID rather than CategoryID
16 0.5 set Cat_filter column to NULL for MySQL 5 compat
17 added $Context->Database->GetConnection() in case an implementation use several connections
18 0.6 Added a check on setting values for unitialied categories
19 0.7 Readme reflects addition of delegate to the core.
20 Replaced "LUM_" with $Configuration['DATABASE_TABLE_PREFIX']
23 //=============inline processing=============
24 // Check to see if this extension has been configured
25 if (!isset($Configuration['EXTENSION_CATEGORYROLES'])) {
26 $res = mysql_query("DESCRIBE {$Configuration['DATABASE_TABLE_PREFIX']}Category Cat_filter", $Context->Database
->GetConnection())
27 or die("error while cheking {$Configuration['DATABASE_TABLE_PREFIX']}Category table from CategoryRoles extension<br/>\n$req<br/>\n" . mysql_error()) ;
28 $current = mysql_fetch_assoc($res);
29 if ($current and $current['Field'] == 'Cat_filter') { //and $current['Type'] == 'text'
30 //column already there, presumably from a previous install
31 $res = mysql_query("ALTER TABLE {$Configuration['DATABASE_TABLE_PREFIX']}Category MODIFY COLUMN Cat_filter text", $Context->Database
->GetConnection())
32 or die("error while updating Cat_filter column in {$Configuration['DATABASE_TABLE_PREFIX']}Category table, in CategoryRoles extension<br/>\n" . mysql_error()) ;
33 AddConfigurationSetting($Context, 'EXTENSION_CATEGORYROLES', '0.6');
36 $res = mysql_query("ALTER TABLE {$Configuration['DATABASE_TABLE_PREFIX']}Category ADD COLUMN Cat_filter text", $Context->Database
->GetConnection())
37 or die("error while adding Cat_filter column to {$Configuration['DATABASE_TABLE_PREFIX']}Category table, in CategoryRoles extension<br/>\n" . mysql_error()) ;
38 AddConfigurationSetting($Context, 'EXTENSION_CATEGORYROLES', '0.6');
40 } elseif ($Configuration['EXTENSION_CATEGORYROLES'] < '0.6') {
41 //update column definition
42 $res = mysql_query("ALTER TABLE {$Configuration['DATABASE_TABLE_PREFIX']}Category MODIFY COLUMN Cat_filter text", $Context->Database
->GetConnection())
43 or die("error while updating Cat_filter column in {$Configuration['DATABASE_TABLE_PREFIX']}Category table, in CategoryRoles extension<br/>\n" . mysql_error()) ;
44 AddConfigurationSetting($Context, 'EXTENSION_CATEGORYROLES', '0.6');
47 //=============delegation=============
48 if ($Context->SelfUrl
== 'post.php') {
49 //This is the page where we need to filter categories out
50 //This add an object whose some method will be called from theme file.
51 $Context->AddToDelegate("DiscussionForm", "PreLoadData", "CategoryFilter");
53 elseif ($Context->SelfUrl
== 'settings.php') {
54 //addon to the category setting: select for roles levels
55 $Context->AddToDelegate("CategoryForm", "Constructor", "CategoryRoles");
56 $Context->AddToDelegate("CategoryForm", "PostSaveCategory", "CategoryRoles");
58 elseif ($Context->SelfUrl
== 'comments.php') {
59 //onn this page we hide the comment form, using the core ShowForm variable.
60 $Context->AddToDelegate("CommentGrid", "Constructor", "UserCanPost");
63 //=============Classes and function declarations=============
65 //this classe manage to hide categories from post and comment page for unauthorized roles.
66 var $cat_levels = array(); //array to store the categories autorization levels
69 function get_levels($user_role = false) {
71 //retreive level informations from the database
72 $req = "SELECT CategoryID, Cat_filter FROM {$Context->Configuration['DATABASE_TABLE_PREFIX']}Category";
73 $res = mysql_query($req, $Context->Database
->GetConnection())
74 or die("error while reading {$Context->Configuration['DATABASE_TABLE_PREFIX']}Category table from CategoryRoles extension<br/>\n$req<br/>\n" . mysql_error()) ;
75 while ($c = mysql_fetch_assoc($res)) $this->cat_levels
[$c['CategoryID']] = $c['Cat_filter'] ?
unserialize($c['Cat_filter']) : array('create_level'=>0,'post_level'=>0);
78 //retreive user role level (= Priority)
79 $req = "SELECT Priority FROM {$Context->Configuration['DATABASE_TABLE_PREFIX']}Role WHERE RoleID=$user_role";
80 $res = mysql_query($req, $Context->Database
->GetConnection())
81 or die("error while reading {$Context->Configuration['DATABASE_TABLE_PREFIX']}Role table from CategoryRoles extension<br/>\n$req<br/>\n" . mysql_error()) ;
82 $r = mysql_fetch_assoc($res);
83 $this->user_level
= $r['Priority'];
87 function hide_categories(&$cs, $user_role) {
88 //called from discussion_form customized theme, get the select to filter and the current user role
89 if (!$this->cat_levels
) $this->get_levels($user_role);
90 foreach ($cs->aOptions
as $i=>$o)
91 if (isset($this->cat_levels
[$o['IdValue']]['create_level']) //the category has a create level set
92 and $this->cat_levels
[$o['IdValue']]['create_level'] > $this->user_level
) // and it's beyond current user level
93 unset($cs->aOptions
[$i]); // then remove the category from the available choices
94 //array_splice($cs->aOptions, $i, 1);
95 $cs->aOptions
= array_values($cs->aOptions
); //compact array
100 function CategoryRoles(&$cat_form) {
103 if ($cat_form->PostBackAction
== 'Category') {
104 $cat_filter = new Cat_filter();
105 $cat_filter->get_levels();
106 //build roles selects
107 $req = "SELECT Name, Priority FROM {$Context->Configuration['DATABASE_TABLE_PREFIX']}Role ORDER BY Priority";
108 $roles_data = mysql_query($req, $Context->Database
->GetConnection())
109 or die("error while reading {$Context->Configuration['DATABASE_TABLE_PREFIX']}Role table from CategoryRoles extension<br/>\n$req<br/>\n" . mysql_error()) ;
110 $cat_form->create_level
= $cat_form->Context
->ObjectFactory
->NewObject($cat_form->Context
, 'Select');
111 $cat_form->create_level
->Name
= 'create_level';
112 $cat_form->create_level
->CssClass
= 'SmallInput';
113 $cat_form->create_level
->AddOptionsFromDataSet($cat_form->Context
->Database
, $roles_data, 'Priority', 'Name');
114 if ($cat_form->Category
->CategoryID
)
115 $cat_form->create_level
->SelectedValue
= $cat_filter->cat_levels
[$cat_form->Category
->CategoryID
]['create_level'];
117 $cat_form->post_level
= clone($cat_form->create_level
);
118 $cat_form->post_level
->Name
= 'post_level';
119 if ($cat_form->Category
->CategoryID
)
120 $cat_form->post_level
->SelectedValue
= $cat_filter->cat_levels
[$cat_form->Category
->CategoryID
]['post_level'];
122 elseif ($cat_form->PostBackAction
== 'ProcessCategory') {
123 $cat_level['create_level'] = ForceIncomingInt('create_level', 0);
124 $cat_level['post_level'] = ForceIncomingInt('post_level', 0);
125 $cat_level = serialize($cat_level);
126 $req = "UPDATE {$Context->Configuration['DATABASE_TABLE_PREFIX']}Category SET Cat_filter='$cat_level' WHERE CategoryID={$cat_form->Category->CategoryID}";
127 mysql_query($req, $Context->Database
->GetConnection())
128 or die("error while updating {$Context->Configuration['DATABASE_TABLE_PREFIX']}Category table from CategoryRoles extension<br/>\n$req<br/>\n" . mysql_error()) ;
132 function UserCanPost(&$comment_grid) {
133 $cat_filter = new Cat_filter();
134 $cat_filter->get_levels($comment_grid->Context
->Session
->User
->RoleID
);
135 $CategoryID = $comment_grid->Discussion
->CategoryID
;
136 if (isset($cat_filter->cat_levels
[$CategoryID]['post_level']) //the category has a post level set
137 and $cat_filter->cat_levels
[$CategoryID]['post_level'] > $cat_filter->user_level
) // and it's beyond current user level
138 $comment_grid->ShowForm
=0;
140 function CategoryFilter(&$discuss_form) {
141 $discuss_form->cat_filter
= new Cat_filter();