Author: ralfbecker
New Revision: 55442
URL: http://svn.stylite.de/viewvc/egroupware?rev=55442&view=rev
Log:
move categories class to api
Added:
trunk/egroupware/api/src/Categories.php
- copied, changed from r55427, trunk/phpgwapi/inc/class.categories.inc.php
Modified:
trunk/egroupware/api/src/Accounts.php
trunk/egroupware/api/src/Contacts.php
trunk/egroupware/api/src/Contacts/Ldap.php
trunk/egroupware/api/src/Contacts/Sql.php
trunk/egroupware/api/src/Etemplate.php
trunk/egroupware/api/src/Etemplate/Widget/Nextmatch.php
trunk/egroupware/api/src/Etemplate/Widget/Select.php
trunk/egroupware/api/src/Etemplate/Widget/Tree.php
trunk/etemplate/inc/class.etemplate.inc.php
trunk/phpgwapi/inc/class.categories.inc.php
— trunk/egroupware/api/src/Accounts.php (original)
+++ trunk/egroupware/api/src/Accounts.php Sun Mar 20 14:41:33 2016
@@ -622,7 +622,7 @@
$GLOBALS[‘egw’]->acl->delete_account($id);
// delete all categories belonging to that user or group
— trunk/phpgwapi/inc/class.categories.inc.php (original)
+++ trunk/egroupware/api/src/Categories.php Sun Mar 20 14:41:33 2016
@@ -1,6 +1,6 @@
<?php
/**
- * API - Categories
+ * EGroupware API - Categories
*
* @link http://www.egroupware.org
* @author Joseph Engo
@@ -16,6 +16,10 @@
* @access public
* @version $Id$
*/
+
+namespace EGroupware\Api;
+
+use egw_framework; // includeCSS
/**
* class to manage categories in eGroupWare
@@ -38,7 +42,7 @@
* and other functions needing to know if a cat has children. Be aware a user might not see all children, as they can
* belong to other users.
*/
-class categories
+class Categories
{
/**
* Account id this class is instanciated for (self::GLOBAL_ACCOUNT for global cats)
@@ -53,7 +57,7 @@
*/
public $app_name;
/**
- * @var egw_db
+ * @var Db
*/
private $db;
/**
@@ -73,7 +77,7 @@
*/
const TABLE = 'egw_categories';
/**
- * @deprecated use categories::TABLE
+ * @deprecated use Categories::TABLE
* @var string
*/
public $table = self::TABLE;
@@ -113,10 +117,10 @@
static public $global_marker;
/**
- * constructor for categories class
- *
- * @param int|string $accountid='' account id or lid, default to current user
- * @param string $app_name='' app name defaults to current app
+ * constructor for Categories class
+ *
+ * @param int|string $accountid ='' account id or lid, default to current user
+ * @param string $app_name ='' app name defaults to current app
*/
function __construct($accountid='',$app_name = '')
{
@@ -161,25 +165,24 @@
{
$all_children = array_merge($all_children,$this->return_all_children($children));
}
- //echo "
categories::return_all_children($cat_id)=(".implode(',',$all_children).")
\n";
return $all_children;
}
/**
* return an array populated with categories
*
- * @param string $type='all' 'subs','mains','appandmains','appandsubs','noglobal','noglobalapp', defaults to 'all'
- * @param int $start=0 see $limit
+ * @param string $type ='all' 'subs','mains','appandmains','appandsubs','noglobal','noglobalapp', defaults to 'all'
+ * @param int $start =0 see $limit
* @param boolean|int $limit if true limited query to maxmatches rows (starting with $start)
- * @param string $query='' query-pattern
- * @param string $sort='ASC' sort order, defaults to 'ASC'
- * @param string $order='' order by, default cat_main, cat_level, cat_name ASC
+ * @param string $query ='' query-pattern
+ * @param string $sort ='ASC' sort order, defaults to 'ASC'
+ * @param string $order ='' order by, default cat_main, cat_level, cat_name ASC
* @param boolean|string $globals includes the global egroupware categories or not,
* 'all_no_acl' to return global and all non-private user categories independent of ACL
- * @param array|int $parent_id=null return only subcats of $parent_id(s)
+ * @param array|int $parent_id =null return only subcats of $parent_id(s)
* @param int $lastmod = -1 if > 0 return only cats modified since then
- * @param string $column='' if column-name given only that column is returned, not the full array with all cat-data
- * @param array $filter=null array with column-name (without cat_-prefix) => value pairs (! negates the value)
+ * @param string $column ='' if column-name given only that column is returned, not the full array with all cat-data
+ * @param array $filter =null array with column-name (without cat_-prefix) => value pairs (! negates the value)
* @return array of cat-arrays or $column values
*/
function return_array($type='all', $start=0, $limit=true, $query='', $sort='ASC',$order='',$globals=false, $parent_id=null, $lastmod=-1, $column='', $filter=null)
@@ -287,9 +290,15 @@
{
if (strstr($order,'cat_data') !== false) $order = 'cat_data'; // sitemgr orders by round(cat_data)!
if (substr($order,0,4) == 'cat_') $order = substr($order,4);
- $sign = strtoupper($sort) == 'DESC' ? '-' : '';
- usort($cats,create_function('$a,$b',$func=(in_array($order,array('name','description','appname','app_name')) ?
- "return ${sign}strcasecmp(\$a['$order'],\$b['$order']);" : "return $sign(int)\$a['$order'] - $sign(int)\$b['$order'];")));
+ $sign = strtoupper($sort) == 'DESC' ? -1 : 1;
+ usort($cats, function($a, $b) use ($order, $sign)
+ {
+ if (in_array($order, array('name','description','appname','app_name')))
+ {
+ return $sign * strcasecmp($a['$order'], $b['$order']);
+ }
+ return $sign*(int)$a['$order'] - $sign*(int)$b['$order'];
+ });
}
// limit the number of returned rows
if ($limit)
@@ -314,15 +323,15 @@
/**
* return a sorted array populated with categories (main sorting criteria is hierachy!)
*
- * @param int $start=0 see $limit
+ * @param int $start =0 see $limit
* @param boolean|int $limit if true limited query to maxmatches rows (starting with $start)
- * @param string $query='' query-pattern
- * @param string $sort='ASC' sort order, either defaults to 'ASC'
- * @param string $order='cat_name' order by
+ * @param string $query ='' query-pattern
+ * @param string $sort ='ASC' sort order, either defaults to 'ASC'
+ * @param string $order ='cat_name' order by
* @param boolean|string $globals includes the global egroupware categories or not,
* 'all_no_acl' to return global and all non-private user categories independent of ACL
- * @param array|int $parent_id=0 return only subcats of $parent_id(s)
- * @param boolean $unserialize_data=true return $cat['data'] as array (not serialized array)
+ * @param array|int $parent_id =0 return only subcats of $parent_id(s)
+ * @param boolean $unserialize_data =true return $cat['data'] as array (not serialized array)
* @return array with cats
*/
function return_sorted_array($start=0,$limit=True,$query='',$sort='ASC',$order='cat_name',$globals=False, $parent_id=0,$unserialize_data=true,$filter=null)
@@ -419,7 +428,7 @@
*
* Owner and appname are set from the values used to instanciate the class!
*
- * @param array $value cat-data
+ * @param array $values cat-data
* @return int new cat-id
*/
function add($values)
@@ -454,7 +463,7 @@
$cat['cat_main'] = $id;
}
// update cache accordingly
- self::invalidate_cache(egw_db::strip_array_keys($cat, 'cat_'));
+ self::invalidate_cache(Db::strip_array_keys($cat, 'cat_'));
return $id;
}
@@ -498,7 +507,7 @@
*
* @param int $needed necessary ACL right: EGW_ACL_{READ|EDIT|DELETE}
* @param mixed $category category as array or the category_id
- * @param boolean $no_acl_check=false if true, grants are NOT checked, gives access to all non-private categories of all users
+ * @param boolean $no_acl_check =false if true, grants are NOT checked, gives access to all non-private categories of all users
* @param boolean $allow_global_read if true, global cats are allowed (independent of app) for reading
* @return boolean true permission granted, false for permission denied, null for category does not exist
*/
@@ -560,8 +569,8 @@
* delete a category
*
* @param int $cat_id category id
- * @param boolean $drop_subs=false if true delete sub-cats too
- * @param boolean $modify_subs=false if true make the subs owned by the parent of $cat_id
+ * @param boolean $drop_subs =false if true delete sub-cats too
+ * @param boolean $modify_subs =false if true make the subs owned by the parent of $cat_id
*/
function delete($cat_id, $drop_subs = False, $modify_subs = False)
{
@@ -697,7 +706,7 @@
if (isset($values['old_parent']) && (int)$values['old_parent'] != (int)$values['parent'])
{
$ret = $this->check_consistency4update($values);
- if ($ret !== true) throw new egw_exception_wrong_userinput($ret);
+ if ($ret !== true) throw new Exception\WrongUserinput($ret);
// everything seems in order -> proceed
$values['level'] = ($values['parent'] ? $this->id2name($values['parent'],'level')+1:0);
$this->adapt_level_in_subtree($values);
@@ -710,7 +719,7 @@
if ($values['parent'] > 0)
{
$ret = $this->check_consistency4update($values);
- if ($ret !== true) throw new egw_exception_wrong_userinput($ret);
+ if ($ret !== true) throw new Exception\WrongUserinput($ret);
// everything seems in order -> proceed
$values['main'] = $this->id2name($values['parent'],'main');
@@ -744,7 +753,7 @@
$cat['cat_appname'] = $this->app_name;
// update cache accordingly
- self::invalidate_cache(egw_db::strip_array_keys($cat, 'cat_'));
+ self::invalidate_cache(Db::strip_array_keys($cat, 'cat_'));
return (int)$values['id'];
}
@@ -759,7 +768,7 @@
* - cat's of other user
*
* @param string $cat_name cat-name
- * @param boolean|string $strip=false if true, strips 'X-' ($strip) from category names added by some SyncML clients
+ * @param boolean|string $strip =false if true, strips 'X-' ($strip) from category names added by some SyncML clients
* @return int cat-id or 0 if not found
*/
function name2id($cat_name, $strip=false)
@@ -811,7 +820,7 @@
* Check if category is global (owner <= 0 || appname == 'phpgw')
*
* @param int|array $cat
- * @param boolean $application_global=false true check for application global categories only (appname == 'phpgw')
+ * @param boolean $application_global =false true check for application global categories only (appname == 'phpgw')
* @return boolean
*/
static function is_global($cat,$application_global=false)
@@ -832,8 +841,8 @@
* We use a shared cache together with read
* $item == 'data' is returned as array (not serialized array)!
*
- * @param int $cat_id=0 cat-id
- * @param string $item='name' requested information, 'path' = / delimited path of category names (incl. parents)
+ * @param int $cat_id =0 cat-id
+ * @param string $item ='name' requested information, 'path' = / delimited path of category names (incl. parents)
* @return string information or '--' if not found or !$cat_id
*/
static function id2name($cat_id=0, $item='name')
@@ -872,9 +881,9 @@
* check if a category id and/or name exists, if id AND name are given the check is for a category with same name and different id (!)
*
* @param string $type subs or mains
- * @param string $cat_name='' category name
- * @param int $cat_id=0 category id
- * @param int $parent=0 category id of parent
+ * @param string $cat_name ='' category name
+ * @param int $cat_id =0 category id
+ * @param int $parent =0 category id of parent
* @return int/boolean cat_id or false if cat not exists
*/
function exists($type,$cat_name = '',$cat_id = 0,$parent = 0)
@@ -904,8 +913,8 @@
* Change the owner of all cats owned by $owner to $to OR deletes them if !$to
*
* @param int $owner owner or the cats to delete or change
- * @param int $to=0 new owner or 0 to delete the cats
- * @param string $app='' if given only cats matching $app are modifed/deleted
+ * @param int $to =0 new owner or 0 to delete the cats
+ * @param string $app ='' if given only cats matching $app are modifed/deleted
*/
function change_owner($owner,$to=0,$app='')
{
@@ -932,7 +941,7 @@
*/
public static function init_cache()
{
- self::$cache = egw_cache::getInstance(self::CACHE_APP, self::CACHE_NAME);
+ self::$cache = Cache::getInstance(self::CACHE_APP, self::CACHE_NAME);
if (is_null(self::$cache))
{
@@ -956,7 +965,7 @@
foreach($GLOBALS['egw']->db->select(self::TABLE,'*',false,__LINE__,__FILE__,
false,'ORDER BY cat_main, cat_level, cat_name ASC') as $cat)
{
- $cat = egw_db::strip_array_keys($cat,'cat_');
+ $cat = Db::strip_array_keys($cat,'cat_');
if ($cat['appname'] == '*update*') continue; // --> ignore update marker
$cat['app_name'] = $cat['appname'];
// backlink children to their parent
@@ -971,7 +980,7 @@
}
self::$cache[$cat['id']] = $cat;
}
- egw_cache::setInstance(self::CACHE_APP, self::CACHE_NAME, self::$cache);
+ Cache::setInstance(self::CACHE_APP, self::CACHE_NAME, self::$cache);
}
//error_log(__METHOD__."() cache initialised: ".function_backtrace());
}
@@ -989,7 +998,7 @@
//error_log(__METHOD__."(".array2string($cat).') '.function_backtrace());
// allways invalidate instance-global cache, as updating our own cache is not perfect and does not help other sessions
- egw_cache::unsetInstance(self::CACHE_APP, self::CACHE_NAME);
+ Cache::unsetInstance(self::CACHE_APP, self::CACHE_NAME);
// if cat given update our own cache, to work around failed sitemgr install via setup (cant read just added categories)
if ($cat)
@@ -1024,7 +1033,7 @@
*/
public static function css($appname)
{
- $cats = new categories('',$appname);
+ $cats = new Categories('',$appname);
$last_mod = $cats->return_array('all',0,1,'','DESC','last_mod', $appname == self::GLOBAL_APPNAME);
$time = count($last_mod) ? $last_mod[0]['last_mod'] : time();
$path = '/phpgwapi/categories.php?app='.$appname.'&'.$time;
@@ -1034,10 +1043,48 @@
}
/**
+ * Get the color of a category
+ *
+ * For multiple cats, the first with a color is used
+ *
+ * @param int|string $_cats multiple comma-separated cat_id's
+ * @return string
+ */
+ static function cats2color($_cats)
+ {
+ static $cat2color = array();
+
+ // ACL check
+ $cats = $GLOBALS['egw']->categories->check_list(EGW_ACL_READ, $_cats);
+
+ if (!$cats) return null;
+
+ if (isset($cat2color[$cats]))
+ {
+ return $cat2color[$cats];
+ }
+
+ foreach(explode(',', $cats) as $cat)
+ {
+ if (isset($cat2color[$cat]))
+ {
+ return $cat2color[$cat];
+ }
+ $data = self::id2name($cat, 'data');
+
+ if (is_array($data) && ($color = $data['color']))
+ {
+ return $cat2color[$cats] = $cat2color[$cat] = $color;
+ }
+ }
+ return null;
+ }
+
+ /**
* Delete categories belonging to a given account, when account got deleted
*
* @param int $account_id
- * @param int $new_owner=null for users data can be transfered to new owner
+ * @param int $new_owner =null for users data can be transfered to new owner
* @return int number of deleted or modified categories
*/
public static function delete_account($account_id, $new_owner=null)
@@ -1055,7 +1102,7 @@
{
if (!isset($cat))
{
- $cat = new categories($new_owner, $data['appname']);
+ $cat = new Categories($new_owner, $data['appname']);
}
$cat->delete($cat_id, false, true);
}
@@ -1067,7 +1114,7 @@
// app_name have to match cat to update!
if (!isset($cat) || $cat->app_name != $data['appname'])
{
- $cat = new categories($new_owner, $data['appname']);
+ $cat = new Categories($new_owner, $data['appname']);
}
$cat->add($data);
}
@@ -1104,137 +1151,4 @@
}
return $deleted;
}
-
- /**
- ******************************** old / deprecated functions ***********************************
- */
-
- /**
- * read a single category
- *
- * We use a shared cache together with id2name
- *
- * @deprecated use read($id) returning just the category array not an array with one element
- * @param int $id id of category
- * @return array|boolean array with one array of cat-data or false if cat not found
- */
- static function return_single($id)
- {
- if (is_null(self::$cache)) self::init_cache();
-
- return isset(self::$cache[$id]) ? array(self::$cache[$id]) : false;
- }
-
- /**
- * return into a select box, list or other formats
- *
- * @param string/array $format string 'select' or 'list', or array with all params
- * @param string $type='' subs or mains
- * @param int/array $selected - cat_id or array with cat_id values
- * @param boolean $globals True or False, includes the global egroupware categories or not
- * @deprecated use html class to create selectboxes
- * @return string populated with categories
- */
- function formatted_list($format,$type='',$selected = '',$globals = False,$site_link = 'site')
- {
- if(is_array($format))
- {
- $type = ($format['type']?$format['type']:'all');
- $selected = (isset($format['selected'])?$format['selected']:'');
- $self = (isset($format['self'])?$format['self']:'');
- $globals = (isset($format['globals'])?$format['globals']:True);
- $site_link = (isset($format['site_link'])?$format['site_link']:'site');
- $format = $format['format'] ? $format['format'] : 'select';
- }
-
- if (!is_array($selected))
- {
- $selected = explode(',',$selected);
- }
-
- if ($type != 'all')
- {
- $cats = $this->return_array($type,0,False,'','','',$globals);
- }
- else
- {
- $cats = $this->return_sorted_array(0,False,'','','',$globals);
- }
-
- if (!$cats) return '';
-
- if($self)
- {
- foreach($cats as $key => $cat)
- {
- if ($cat['id'] == $self)
- {
- unset($cats[$key]);
- }
- }
- }
-
- switch ($format)
- {
- case 'select':
- foreach($cats as $cat)
- {
- $s .= ''.str_repeat(' ',$cat['level']);
- $s .= $GLOBALS['egw']->strip_html($cat['name']);
- if (self::is_global($cat))
- {
- $s .= self::$global_marker;
- }
- $s .= '' . "\n";
- }
- break;
-
- case 'list':
- $space = ' ';
-
- $s = '
' . "\n";
-
- foreach($cats as $cat)
- {
- $image_set = ' ';
-
- if (in_array($cat['id'],$selected))
- {
- $image_set = '
';
- }
- if (($cat['level'] == 0) && !in_array($cat['id'],$selected))
- {
- $image_set = '
';
- }
- $space_set = str_repeat($space,$cat['level']);
-
- $s .= '' . "\n";
- $s .= '| ' . $image_set . ' | ' . "\n";
- $s .= '' . $space_set . ''
- . $GLOBALS['egw']->strip_html($cat['name'])
- . '
| ' . "\n"
- . '
' . "\n";
- }
- $s .= '
' . "\n";
- break;
- }
- return $s;
- }
-
- /**
- * return category name for a given id
- *
- * @deprecated This is only a temp wrapper, use id2name() to keep things matching across the board. (jengo)
- * @param int $cat_id
- * @return string cat_name category name
- */
- function return_name($cat_id)
- {
- return $this->id2name($cat_id);
- }
}
Modified: trunk/egroupware/api/src/Contacts.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Contacts.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Contacts.php (original)
+++ trunk/egroupware/api/src/Contacts.php Sun Mar 20 14:41:33 2016
@@ -15,10 +15,6 @@
*/
namespace EGroupware\Api;
-
-// explicitly reference classes still in phpgwapi
-use categories;
-use etemplate; // cats2color
use calendar_bo; // to_do: do NOT require it, just use if there
@@ -143,7 +139,7 @@
/**
* Categories object
*
- * @var categories
+ * @var Categories
*/
var $categories;
@@ -315,7 +311,7 @@
$this->org_fields[] = 'adr_two_countrycode';
}
}
- $this->categories = new categories($this->user,'addressbook');
+ $this->categories = new Categories($this->user,'addressbook');
$this->delete_history = $GLOBALS['egw_info']['server']['history'];
}
@@ -1525,7 +1521,7 @@
$result[$contact['id']] = call_user_func_array('imap_rfc822_write_address', $args);
}
// show category color
- if ($contact['cat_id'] && ($color = etemplate::cats2color($contact['cat_id'])))
+ if ($contact['cat_id'] && ($color = Categories::cats2color($contact['cat_id'])))
{
$result[$contact['id']] = array(
'label' => $result[$contact['id']],
@@ -1691,7 +1687,7 @@
$drop_subs = ($data['drop_subs'] && !$data['modify_subs']);
if($drop_subs)
{
- $cats = new categories('', 'addressbook');
+ $cats = new Categories('', 'addressbook');
$cat_ids = $cats->return_all_children($data['cat_id']);
}
else
@@ -2090,7 +2086,7 @@
{
if (!is_object($this->categories))
{
- $this->categories = new categories($this->user,'addressbook');
+ $this->categories = new Categories($this->user,'addressbook');
}
if (!is_array($cat_id_list))
Modified: trunk/egroupware/api/src/Contacts/Ldap.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Contacts/Ldap.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Contacts/Ldap.php (original)
+++ trunk/egroupware/api/src/Contacts/Ldap.php Sun Mar 20 14:41:33 2016
@@ -932,7 +932,7 @@
{
if (!is_object($GLOBALS['egw']->categories))
{
- $GLOBALS['egw']->categories = CreateObject('phpgwapi.categories');
+ $GLOBALS['egw']->categories = new Api\Categories();
}
$cats = $GLOBALS['egw']->categories->return_all_children((int)$value);
if (count($cats) > 1) $filters .= '(|';
@@ -1210,7 +1210,7 @@
foreach(is_array($data['cat_id']) ? $data['cat_id'] : explode(',',$data['cat_id']) as $cat)
{
$ldapContact['category'][] = Api\Translation::convert(
- ExecMethod('phpgwapi.categories.id2name',$cat),$this->charset,'utf-8');
+ Api\Categories::id2name($cat),$this->charset,'utf-8');
}
}
foreach(array(
@@ -1256,7 +1256,7 @@
{
if (!is_int($iii)) continue;
- $contact['cat_id'][] = ExecMethod('phpgwapi.categories.name2id',$cat);
+ $contact['cat_id'][] = $GLOBALS['egw']->categories->name2id($cat);
}
if ($contact['cat_id']) $contact['cat_id'] = implode(',',$contact['cat_id']);
}
Modified: trunk/egroupware/api/src/Contacts/Sql.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Contacts/Sql.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Contacts/Sql.php (original)
+++ trunk/egroupware/api/src/Contacts/Sql.php Sun Mar 20 14:41:33 2016
@@ -439,7 +439,7 @@
{
if (!is_object($GLOBALS['egw']->categories))
{
- $GLOBALS['egw']->categories = CreateObject('phpgwapi.categories');
+ $GLOBALS['egw']->categories = new Api\Categories;
}
foreach($GLOBALS['egw']->categories->return_all_children((int)$cat_id) as $cat)
{
Modified: trunk/egroupware/api/src/Etemplate.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Etemplate.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Etemplate.php (original)
+++ trunk/egroupware/api/src/Etemplate.php Sun Mar 20 14:41:33 2016
@@ -16,7 +16,6 @@
// explicitly import old not yet ported classes
use egw;
use egw_framework;
-use categories; // css
/**
* New eTemplate serverside contains:
@@ -232,7 +231,7 @@
egw_framework::validate_file('.','app',$app,false);
}
// Category styles
- categories::css($app);
+ Categories::css($app);
// set action attribute for autocomplete form tag
// as firefox complains on about:balnk action, thus we have to literaly submit the form to a blank html
Modified: trunk/egroupware/api/src/Etemplate/Widget/Nextmatch.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Etemplate/Widget/Nextmatch.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Etemplate/Widget/Nextmatch.php (original)
+++ trunk/egroupware/api/src/Etemplate/Widget/Nextmatch.php Sun Mar 20 14:41:33 2016
@@ -19,7 +19,6 @@
// explicitly import old not yet ported classes
use egw;
use egw_framework; // includeCSS
-use categories;
/**
* eTemplate serverside implementation of the nextmatch widget
@@ -920,7 +919,7 @@
public static function category_action($app, $group=0, $caption='Change category',
$prefix='cat_', $globals=true, $parent_id=0, $max_cats_flat=self::DEFAULT_MAX_MENU_LENGTH)
{
- $cat = new categories(null,$app);
+ $cat = new Api\Categories(null,$app);
$cats = $cat->return_sorted_array($start=0, false, '', 'ASC', 'cat_name', $globals, $parent_id, true);
// if more then max_length cats, switch automatically to hierarchical display
Modified: trunk/egroupware/api/src/Etemplate/Widget/Select.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Etemplate/Widget/Select.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Etemplate/Widget/Select.php (original)
+++ trunk/egroupware/api/src/Etemplate/Widget/Select.php Sun Mar 20 14:41:33 2016
@@ -17,7 +17,6 @@
use EGroupware\Api;
// explicitly import old not yet ported classes
-use categories;
use calendar_timezones;
/**
@@ -563,7 +562,7 @@
}
else // we need to instanciate a new cat object for the correct application
{
- $categories = new categories($type5,$type3);
+ $categories = new Api\Categories($type5,$type3);
}
// Allow text for global
$type = ($type && strlen($type) > 1 ? $type : !$type);
@@ -572,9 +571,9 @@
{
$s = str_repeat(' ',$cat['level']) . stripslashes($cat['name']);
- if (categories::is_global($cat))
- {
- $s .= categories::$global_marker;
+ if (Api\Categories::is_global($cat))
+ {
+ $s .= Api\Categories::$global_marker;
}
$options[$cat['id']] = array(
'label' => $s,
Modified: trunk/egroupware/api/src/Etemplate/Widget/Tree.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/egroupware/api/src/Etemplate/Widget/Tree.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/egroupware/api/src/Etemplate/Widget/Tree.php (original)
+++ trunk/egroupware/api/src/Etemplate/Widget/Tree.php Sun Mar 20 14:41:33 2016
@@ -18,7 +18,6 @@
// explicitly import old not yet ported classes
use common; // egw_exit
-use categories;
use egw_framework;
egw_framework::includeCSS('/phpgwapi/js/dhtmlxtree/codebase/dhtmlXTree.css');
@@ -436,7 +435,7 @@
}
else // we need to instanciate a new cat object for the correct application
{
- $categories = new categories('',$type3);
+ $categories = new Api\Categories('',$type3);
}
$cat2path=array();
foreach((array)$categories->return_sorted_array(0,False,'','','',!$type,0,true) as $cat)
Modified: trunk/etemplate/inc/class.etemplate.inc.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/etemplate/inc/class.etemplate.inc.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/etemplate/inc/class.etemplate.inc.php (original)
+++ trunk/etemplate/inc/class.etemplate.inc.php Sun Mar 20 14:41:33 2016
@@ -712,33 +712,7 @@
*/
static function cats2color($cats)
{
- static $cat2color;
-
- // ACL check
- $cats = $GLOBALS['egw']->categories->check_list(EGW_ACL_READ,$cats);
-
- if (!$cats) return null;
-
- if (isset($cat2color[$cats]))
- {
- return $cat2color[$cats];
- }
-
- foreach(explode(',',$cats) as $cat)
- {
- if (isset($cat2color[$cat]))
- {
- return $cat2color[$cat];
- }
- $data = categories::id2name($cat,'data');
-
- if (is_array($data) && ($color = $data['color']))
- {
- //echo "
cats2color('$cats')=$color
\n";
- return $cat2color[$cats] = $cat2color[$cat] = $color;
- }
- }
- return null;
+ return Api\Categories::cats2color($cats);
}
/**
Modified: trunk/phpgwapi/inc/class.categories.inc.php
URL: http://svn.stylite.de/viewvc/egroupware/trunk/phpgwapi/inc/class.categories.inc.php?rev=55442&r1=55441&r2=55442&view=diff
==============================================================================
--- trunk/phpgwapi/inc/class.categories.inc.php (original)
+++ trunk/phpgwapi/inc/class.categories.inc.php Sun Mar 20 14:41:33 2016
@@ -17,1097 +17,20 @@
* @version $Id$
*/
+use EGroupware\Api;
+
/**
* class to manage categories in eGroupWare
*
- * Categories are read now once from the database into a static cache variable (by the static init_cache method).
- * The egw object fills that cache ones per session, stores it in a private var, from which it restores it for each
- * request of that session.
- *
- * $cat['data'] array:
- * ------------------
- * $cat['data'] array is stored serialized in the database to allow applications to simply add all
- * sorts of values there (without the hassel of a DB schema change).
- * Before 14.1 data was PHP serialize(d), if it was an array, now it get json_encode(d),
- * we use json_php_unserialize(, true) to automatic cope with that (true allows strings too)!
- * Static methods categories::read($cat_id) and categories::id2name now returns data already as array
- * and add() or edit() methods automatically json-encode $cat['data'], if it's an array.
- * return*() methods return $cat['data'] now as array by default!
- *
- * @ToDo The cache now contains a backlink from the parent to it's children. Use that link to simplyfy return_all_children
- * and other functions needing to know if a cat has children. Be aware a user might not see all children, as they can
- * belong to other users.
+ * @deprecated use Api\Categories
*/
-class categories
+class categories extends Api\Categories
{
- /**
- * Account id this class is instanciated for (self::GLOBAL_ACCOUNT for global cats)
- *
- * @var int
- */
- public $account_id;
- /**
- * Application this class is instancated for (self::GLOBAL_APPNAME for application global cats)
- *
- * @var string
- */
- public $app_name;
- /**
- * @var egw_db
- */
- private $db;
- /**
- * Total number of records of return_(sorted_)array (returning only a limited number of rows)
- *
- * @var int
- */
- public $total_records;
- /**
- * Grants from other users for account_id and app_name (init by return array)
- *
- * @var array
- */
- public $grants;
- /**
- * Name of the categories table
- */
- const TABLE = 'egw_categories';
/**
* @deprecated use categories::TABLE
* @var string
*/
public $table = self::TABLE;
- /**
- * Cache holding all categories, set via init_cache() method
- *
- * @var array cat_id => array of data
- */
- private static $cache;
- const CACHE_APP = 'phpgwapi';
- const CACHE_NAME = 'cat_cache';
-
- /**
- * Appname for global categories
- */
- const GLOBAL_APPNAME = 'phpgw';
-
- /**
- * account_id for global categories
- */
- const GLOBAL_ACCOUNT = 0;
-
- /**
- * Owners for global accounts
- *
- * Usually the users group memberships and self::GLOBAL_ACCOUNT
- *
- * @var array
- */
- private $global_owners = array(self::GLOBAL_ACCOUNT);
-
- /**
- * string to postfix global cats
- *
- * @var string
- */
- static public $global_marker;
-
- /**
- * constructor for categories class
- *
- * @param int|string $accountid='' account id or lid, default to current user
- * @param string $app_name='' app name defaults to current app
- */
- function __construct($accountid='',$app_name = '')
- {
- if (!$app_name) $app_name = $GLOBALS['egw_info']['flags']['currentapp'];
-
- if ($accountid === self::GLOBAL_ACCOUNT ||
- $accountid < 0 && $GLOBALS['egw']->accounts->exists($accountid) == 2)
- {
- $this->account_id = (int) $accountid;
- }
- else
- {
- $this->account_id = (int) get_account_id($accountid);
- $this->global_owners = $this->account_id ? $GLOBALS['egw']->accounts->memberships($this->account_id, true) : array();
- $this->global_owners[] = self::GLOBAL_ACCOUNT;
- }
- $this->app_name = $app_name;
- $this->db = $GLOBALS['egw']->db;
-
- if (is_null(self::$cache)) // should not be necessary, as cache is load and restored by egw object
- {
- self::init_cache();
- }
- if (is_null(self::$global_marker))
- {
- // as et2 adds options with .text(), it can't be entities, but php knows no string literals with utf-8
- }
- }
-
- /**
- * returns array with id's of all children from $cat_id and $cat_id itself!
- *
- * @param int|array $cat_id (array of) integer cat-id to search for
- * @return array of cat-id's
- */
- function return_all_children($cat_id)
- {
- $all_children = (array) $cat_id;
-
- $children = $this->return_array('subs',0,False,'','','',True,$cat_id,-1,'id');
- if (is_array($children) && count($children))
- {
- $all_children = array_merge($all_children,$this->return_all_children($children));
- }
- //echo "
categories::return_all_children($cat_id)=(".implode(',',$all_children).")
\n";
- return $all_children;
- }
-
- /**
- * return an array populated with categories
- *
- * @param string $type='all' 'subs','mains','appandmains','appandsubs','noglobal','noglobalapp', defaults to 'all'
- * @param int $start=0 see $limit
- * @param boolean|int $limit if true limited query to maxmatches rows (starting with $start)
- * @param string $query='' query-pattern
- * @param string $sort='ASC' sort order, defaults to 'ASC'
- * @param string $order='' order by, default cat_main, cat_level, cat_name ASC
- * @param boolean|string $globals includes the global egroupware categories or not,
- * 'all_no_acl' to return global and all non-private user categories independent of ACL
- * @param array|int $parent_id=null return only subcats of $parent_id(s)
- * @param int $lastmod = -1 if > 0 return only cats modified since then
- * @param string $column='' if column-name given only that column is returned, not the full array with all cat-data
- * @param array $filter=null array with column-name (without cat_-prefix) => value pairs (! negates the value)
- * @return array of cat-arrays or $column values
- */
- function return_array($type='all', $start=0, $limit=true, $query='', $sort='ASC',$order='',$globals=false, $parent_id=null, $lastmod=-1, $column='', $filter=null)
- {
- //error_log(__METHOD__."($type,$start,$limit,$query,$sort,$order,globals=$globals,parent=".array2string($parent_id).",$lastmod,$column,filter=".array2string($filter).") account_id=$this->account_id, appname=$this->app_name: ".function_backtrace());
- $cats = array();
- foreach(self::$cache as $cat)
- {
- if ($filter) foreach($filter as $col => $val)
- {
- if (!is_array($val) && $val[0] === '!')
- {
- // also match against trimmed database entry on name and description fields
- if (($col == 'name' || $col == 'description') && is_string($cat[$col]))
- {
- if ($cat[$col] == substr($val,1) || trim($cat[$col]) == substr($val,1)) continue 2;
- }
- else
- {
- if ($cat[$col] == substr($val,1)) continue 2;
- }
- }
- elseif (is_array($val))
- {
- // also match against trimmed database entry on name and description fields
- if (($col == 'name' || $col == 'description') && is_string($cat[$col]))
- {
- if (!in_array($cat[$col],$val) && !in_array(trim($cat[$col]),$val)) continue 2;
- }
- else
- {
- if (!in_array($cat[$col],$val)) continue 2;
- }
- }
- else
- {
- // also match against trimmed database entry on name and description fields
- if (($col == 'name' || $col == 'description') && is_string($cat[$col]))
- {
- if ($cat[$col] != $val && trim($cat[$col]) != $val) continue 2;
- }
- else
- {
- if ($cat[$col] != $val) continue 2;
- }
- }
- }
- // check if certain parent required
- if ($parent_id && !in_array($cat['parent'],(array)$parent_id)) continue;
-
- // return global categories just if $globals is set
- if (!$globals && $cat['appname'] == self::GLOBAL_APPNAME)
- {
- continue;
- }
-
- // check for read permission
- if(!$this->check_perms(EGW_ACL_READ, $cat, $globals === 'all_no_acl'))
- {
- continue;
- }
-
- // check if we have the correct type
- switch ($type)
- {
- case 'subs':
- if (!$cat['parent']) continue 2; // 2 for switch AND foreach!
- break;
- case 'mains':
- if ($cat['parent']) continue 2;
- break;
- case 'appandmains':
- if ($cat['appname'] != $this->app_name || $cat['parent']) continue 2;
- break;
- case 'appandsubs':
- if ($cat['appname'] != $this->app_name || !$cat['parent']) continue 2;
- break;
- case 'noglobal':
- if ($cat['appname'] == $this->app_name) continue 2;
- break;
- case 'noglobalapp':
- if ($cat['appname'] != $this->app_name || $cat['owner'] == (int)$this->account_id) continue 2;
- break;
- }
-
- // check name and description for $query
- if ($query && stristr($cat['name'],$query) === false && stristr($cat['description'],$query) === false) continue;
-
- // check if last modified since
- if ($lastmod > 0 && $cat['last_mod'] <= $lastmod) continue;
-
- $cat['data'] = $cat['data'] ? json_php_unserialize($cat['data'], true) : array();
-
- $cats[] = $cat;
- }
- if (!($this->total_records = count($cats)))
- {
- //error_log(__METHOD__."($type,$start,$limit,$query,$sort,$order,$globals,parent=$parent_id,$lastmod,$column) account_id=$this->account_id, appname=$this->app_name = FALSE");
- return array();
- }
- if (!$sort) $sort = 'ASC';
- // order the entries if necessary (cache is already ordered in or default order: cat_main, cat_level, cat_name ASC)
- if ($this->total_records > 1 && !empty($order) &&
- preg_match('/^[a-zA-Z_(), ]+$/',$order) && preg_match('/^(ASC|DESC|asc|desc)$/',$sort))
- {
- if (strstr($order,'cat_data') !== false) $order = 'cat_data'; // sitemgr orders by round(cat_data)!
- if (substr($order,0,4) == 'cat_') $order = substr($order,4);
- $sign = strtoupper($sort) == 'DESC' ? '-' : '';
- usort($cats,create_function('$a,$b',$func=(in_array($order,array('name','description','appname','app_name')) ?
- "return ${sign}strcasecmp(\$a['$order'],\$b['$order']);" : "return $sign(int)\$a['$order'] - $sign(int)\$b['$order'];")));
- }
- // limit the number of returned rows
- if ($limit)
- {
- if (!is_int($limit)) $limit = (int)$GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs'];
- $cats = array_slice($cats,(int)$start,$limit);
- }
- // return only a certain column (why not return is as value?)
- if ($column)
- {
- foreach($cats as $k => $cat)
- {
- $cats[$k] = $cat[$column];
- }
- }
- //error_log(__METHOD__."($type,$start,$limit,$query,$sort,$order,$globals,parent=".array2string($parent_id).",$lastmod,$column,filter=".array2string($filter).",$unserialize_data) account_id=$this->account_id, appname=$this->app_name = ".array2string($cats));
-
- reset($cats); // some old code (eg. sitemgr) relies on the array-pointer!
- return $cats;
- }
-
- /**
- * return a sorted array populated with categories (main sorting criteria is hierachy!)
- *
- * @param int $start=0 see $limit
- * @param boolean|int $limit if true limited query to maxmatches rows (starting with $start)
- * @param string $query='' query-pattern
- * @param string $sort='ASC' sort order, either defaults to 'ASC'
- * @param string $order='cat_name' order by
- * @param boolean|string $globals includes the global egroupware categories or not,
- * 'all_no_acl' to return global and all non-private user categories independent of ACL
- * @param array|int $parent_id=0 return only subcats of $parent_id(s)
- * @param boolean $unserialize_data=true return $cat['data'] as array (not serialized array)
- * @return array with cats
- */
- function return_sorted_array($start=0,$limit=True,$query='',$sort='ASC',$order='cat_name',$globals=False, $parent_id=0,$unserialize_data=true,$filter=null)
- {
- if (!$sort) $sort = 'ASC';
- if (!$order) $order = 'cat_name';
-
- //error_log(__METHOD__."($start,$limit,$query,$sort,$order,globals=$globals,parent=$parent_id,$unserialize_data) account_id=$this->account_id, appname=$this->app_name: ".function_backtrace());
-
- $parents = $cats = array();
-
- // Cast parent_id to array, but only if there is one
- if($parent_id !== false && $parent_id !== null) $parent_id = (array)$parent_id;
- if (!($cats = $this->return_array('all',0,false,$query,$sort,$order,$globals,$parent_id,-1,'',$filter,$unserialize_data)))
- {
- $cats = array();
- }
- foreach($cats as $cat)
- {
- $parents[] = $cat['id'];
- }
-
- if($parent_id || !$cats) // Avoid wiping search results
- {
- // Go find the children
- while (count($parents))
- {
- if (!($subs = $this->return_array('all',0,false,$query,$sort,$order,$globals,$parents,-1,'',$filter,$unserialize_data)))
- {
- break;
- }
- $parents = $children = array();
- foreach($subs as $cat)
- {
- $parents[] = $cat['id'];
- $children[$cat['parent']][] = $cat;
- }
- // sort the cats into the mains
- if (count($children))
- {
- $cats2 = $cats;
- $cats = array();
- foreach($cats2 as $cat)
- {
- $cats[] = $cat;
- if (isset($children[$cat['id']]))
- {
- foreach($children[$cat['id']] as $child)
- {
- $cats[] = $child;
- }
- }
- }
- }
- }
- }
- $this->total_records = count($cats);
-
- // limit the number of returned rows
- if ($limit)
- {
- if (!is_int($limit)) $limit = (int)$GLOBALS['egw_info']['user']['preferences']['common']['maxmatchs'];
- $cats = array_slice($cats,(int)$start,$limit);
- }
- reset($cats); // some old code (eg. sitemgr) relies on the array-pointer!
- return $cats;
- }
-
- /**
- * Read a category
- *
- * We use a shared cache together with id2name
- *
- * Data array get automatically unserialized, if it was serialized!
- *
- * @param int $id id of category
- * @return array|boolean array with cat-data or false if cat not found
- */
- static function read($id)
- {
- if (is_null(self::$cache)) self::init_cache();
-
- if (!isset(self::$cache[$id])) return false;
-
- $cat = self::$cache[$id];
- $cat['data'] = $cat['data'] ? ((($arr=json_php_unserialize($cat['data'], true)) !== false || $cat['data'] === 'b:0;') ?
- $arr : $cat['data']) : array();
-
- return $cat;
- }
-
- /**
- * Add a category
- *
- * Owner and appname are set from the values used to instanciate the class!
- *
- * @param array $value cat-data
- * @return int new cat-id
- */
- function add($values)
- {
- if ((int)$values['parent'] > 0)
- {
- $values['level'] = $this->id2name($values['parent'],'level')+1;
- $values['main'] = $this->id2name($values['parent'],'main');
- }
- else
- {
- $values['level'] = 0;
- }
- $this->db->insert(self::TABLE,$cat=array(
- 'cat_parent' => $values['parent'],
- 'cat_owner' => isset($values['owner']) ? $values['owner'] : $this->account_id,
- 'cat_access' => isset($values['access']) ? $values['access'] : 'public',
- 'cat_appname' => $this->app_name,
- 'cat_name' => $values['name'],
- 'cat_description' => isset($values['description']) ? $values['description'] : $values['descr'], // support old name different from returned one
- 'cat_data' => is_array($values['data']) ? json_encode($values['data']) : $values['data'],
- 'cat_main' => $values['main'],
- 'cat_level' => $values['level'],
- 'last_mod' => time(),
- ),(int)$values['id'] > 0 ? array('cat_id' => $values['id']) : array(),__LINE__,__FILE__);
-
- $cat['cat_id'] = $id = (int)$values['id'] > 0 ? (int)$values['id'] : $this->db->get_last_insert_id(self::TABLE,'cat_id');
-
- if (!(int)$values['parent'] && $id != $values['main'])
- {
- $this->db->update(self::TABLE,array('cat_main' => $id),array('cat_id' => $id),__LINE__,__FILE__);
- $cat['cat_main'] = $id;
- }
- // update cache accordingly
- self::invalidate_cache(egw_db::strip_array_keys($cat, 'cat_'));
-
- return $id;
- }
-
- /**
- * Checks category permissions for a given list of commaseparated category ids
- * and truncates it by the ones the user do