PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/ca_user_groups.php

https://github.com/libis/providencelibiscode
PHP | 757 lines | 465 code | 71 blank | 221 comment | 70 complexity | 6f5a94e1a05b6202291ed6908129d6d8 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, GPL-3.0, LGPL-2.0, LGPL-3.0
  1. <?php
  2. /** ---------------------------------------------------------------------
  3. * app/models/ca_user_groups.php : table access class for table ca_user_groups
  4. * ----------------------------------------------------------------------
  5. * CollectiveAccess
  6. * Open-source collections management software
  7. * ----------------------------------------------------------------------
  8. *
  9. * Software by Whirl-i-Gig (http://www.whirl-i-gig.com)
  10. * Copyright 2008-2011 Whirl-i-Gig
  11. *
  12. * For more information visit http://www.CollectiveAccess.org
  13. *
  14. * This program is free software; you may redistribute it and/or modify it under
  15. * the terms of the provided license as published by Whirl-i-Gig
  16. *
  17. * CollectiveAccess is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTIES whatsoever, including any implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * This source code is free and modifiable under the terms of
  22. * GNU General Public License. (http://www.gnu.org/copyleft/gpl.html). See
  23. * the "license.txt" file for details, or visit the CollectiveAccess web site at
  24. * http://www.CollectiveAccess.org
  25. *
  26. * @package CollectiveAccess
  27. * @subpackage models
  28. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License version 3
  29. *
  30. * ----------------------------------------------------------------------
  31. */
  32. /**
  33. *
  34. */
  35. require_once(__CA_APP_DIR__.'/models/ca_user_roles.php');
  36. BaseModel::$s_ca_models_definitions['ca_user_groups'] = array(
  37. 'NAME_SINGULAR' => _t('user group'),
  38. 'NAME_PLURAL' => _t('user groups'),
  39. 'FIELDS' => array(
  40. 'group_id' => array(
  41. 'FIELD_TYPE' => FT_NUMBER, 'DISPLAY_TYPE' => DT_HIDDEN,
  42. 'IDENTITY' => true, 'DISPLAY_WIDTH' => 10, 'DISPLAY_HEIGHT' => 1,
  43. 'IS_NULL' => false,
  44. 'DEFAULT' => '',
  45. 'LABEL' => _t('Group id'), 'DESCRIPTION' => _t('Unique numeric identifier used by CollectiveAccess internally to identify this group')
  46. ),
  47. 'parent_id' => array(
  48. 'FIELD_TYPE' => FT_NUMBER, 'DISPLAY_TYPE' => DT_OMIT,
  49. 'DISPLAY_WIDTH' => 10, 'DISPLAY_HEIGHT' => 1,
  50. 'IS_NULL' => true,
  51. 'DEFAULT' => '',
  52. 'LABEL' => 'Parent id', 'DESCRIPTION' => 'Identifier for parent record'
  53. ),
  54. 'name' => array(
  55. 'FIELD_TYPE' => FT_TEXT, 'DISPLAY_TYPE' => DT_FIELD,
  56. 'DISPLAY_WIDTH' => 88, 'DISPLAY_HEIGHT' => 1,
  57. 'IS_NULL' => false,
  58. 'DEFAULT' => '',
  59. 'LABEL' => _t('Name'), 'DESCRIPTION' => _t('Name of group. Should be unique.'),
  60. 'BOUNDS_LENGTH' => array(1,255)
  61. ),
  62. 'code' => array(
  63. 'FIELD_TYPE' => FT_TEXT, 'DISPLAY_TYPE' => DT_FIELD,
  64. 'DISPLAY_WIDTH' => 10, 'DISPLAY_HEIGHT' => 1,
  65. 'IS_NULL' => false,
  66. 'DEFAULT' => '',
  67. 'LABEL' => _t('Code'), 'DESCRIPTION' => _t('Short code (up to 8 characters) for group (must be unique)'),
  68. 'BOUNDS_LENGTH' => array(1,20)
  69. ),
  70. 'description' => array(
  71. 'FIELD_TYPE' => FT_TEXT, 'DISPLAY_TYPE' => DT_FIELD,
  72. 'DISPLAY_WIDTH' => 88, 'DISPLAY_HEIGHT' => 6,
  73. 'IS_NULL' => false,
  74. 'DEFAULT' => '',
  75. 'LABEL' => _t('Description'), 'DESCRIPTION' => _t('Description of group. This text will be displayed to system administrators only and should clearly document the purpose of the group.'),
  76. 'BOUNDS_LENGTH' => array(0,65535)
  77. ),
  78. 'user_id' => array(
  79. 'FIELD_TYPE' => FT_NUMBER, 'DISPLAY_TYPE' => DT_OMIT,
  80. 'DISPLAY_WIDTH' => 40, 'DISPLAY_HEIGHT' => 1,
  81. 'IS_NULL' => true,
  82. 'DISPLAY_FIELD' => array('ca_users.lname', 'ca_users.fname'),
  83. 'DEFAULT' => '',
  84. 'LABEL' => _t('Group administrator'), 'DESCRIPTION' => _t('The user who administers the group.')
  85. ),
  86. 'rank' => array(
  87. 'FIELD_TYPE' => FT_NUMBER, 'DISPLAY_TYPE' => DT_OMIT,
  88. 'DISPLAY_WIDTH' => 10, 'DISPLAY_HEIGHT' => 1,
  89. 'IS_NULL' => false,
  90. 'DEFAULT' => '',
  91. 'LABEL' => _t('Sort order'), 'DESCRIPTION' => _t('The relative priority of the group when displayed in a list with other relationship types. Lower numbers indicate higher priority.'),
  92. 'BOUNDS_VALUE' => array(0,65535)
  93. ),
  94. 'vars' => array(
  95. 'FIELD_TYPE' => FT_VARS, 'DISPLAY_TYPE' => DT_OMIT,
  96. 'DISPLAY_WIDTH' => 88, 'DISPLAY_HEIGHT' => 15,
  97. 'IS_NULL' => false,
  98. 'DEFAULT' => '',
  99. 'LABEL' => 'Vars', 'DESCRIPTION' => 'Storage for group-level variables',
  100. 'BOUNDS_LENGTH' => array(0,65535)
  101. ),
  102. 'hier_left' => array(
  103. 'FIELD_TYPE' => FT_NUMBER, 'DISPLAY_TYPE' => DT_OMIT,
  104. 'DISPLAY_WIDTH' => 10, 'DISPLAY_HEIGHT' => 1,
  105. 'IS_NULL' => false,
  106. 'DEFAULT' => '',
  107. 'LABEL' => 'Hierarchical index - left bound', 'DESCRIPTION' => 'Left-side boundary for nested set-style hierarchical indexing; used to accelerate search and retrieval of hierarchical record sets.'
  108. ),
  109. 'hier_right' => array(
  110. 'FIELD_TYPE' => FT_NUMBER, 'DISPLAY_TYPE' => DT_OMIT,
  111. 'DISPLAY_WIDTH' => 10, 'DISPLAY_HEIGHT' => 1,
  112. 'IS_NULL' => false,
  113. 'DEFAULT' => '',
  114. 'LABEL' => 'Hierarchical index - right bound', 'DESCRIPTION' => 'Right-side boundary for nested set-style hierarchical indexing; used to accelerate search and retrieval of hierarchical record sets.'
  115. )
  116. )
  117. );
  118. class ca_user_groups extends BaseModel {
  119. # ---------------------------------
  120. # --- Object attribute properties
  121. # ---------------------------------
  122. # Describe structure of content object's properties - eg. database fields and their
  123. # associated types, what modes are supported, et al.
  124. #
  125. # ------------------------------------------------------
  126. # --- Basic object parameters
  127. # ------------------------------------------------------
  128. # what table does this class represent?
  129. protected $TABLE = 'ca_user_groups';
  130. # what is the primary key of the table?
  131. protected $PRIMARY_KEY = 'group_id';
  132. # ------------------------------------------------------
  133. # --- Properties used by standard editing scripts
  134. #
  135. # These class properties allow generic scripts to properly display
  136. # records from the table represented by this class
  137. #
  138. # ------------------------------------------------------
  139. # Array of fields to display in a listing of records from this table
  140. protected $LIST_FIELDS = array('name');
  141. # When the list of "list fields" above contains more than one field,
  142. # the LIST_DELIMITER text is displayed between fields as a delimiter.
  143. # This is typically a comma or space, but can be any string you like
  144. protected $LIST_DELIMITER = ' ';
  145. # What you'd call a single record from this table (eg. a "person")
  146. protected $NAME_SINGULAR;
  147. # What you'd call more than one record from this table (eg. "people")
  148. protected $NAME_PLURAL;
  149. # List of fields to sort listing of records by; you can use
  150. # SQL 'ASC' and 'DESC' here if you like.
  151. protected $ORDER_BY = array('name');
  152. # Maximum number of record to display per page in a listing
  153. protected $MAX_RECORDS_PER_PAGE = 20;
  154. # How do you want to page through records in a listing: by number pages ordered
  155. # according to your setting above? Or alphabetically by the letters of the first
  156. # LIST_FIELD?
  157. protected $PAGE_SCHEME = 'alpha'; # alpha [alphabetical] or num [numbered pages; default]
  158. # If you want to order records arbitrarily, add a numeric field to the table and place
  159. # its name here. The generic list scripts can then use it to order table records.
  160. protected $RANK = 'rank';
  161. # ------------------------------------------------------
  162. # Hierarchical table properties
  163. # ------------------------------------------------------
  164. protected $HIERARCHY_TYPE = __CA_HIER_TYPE_SIMPLE_MONO__;
  165. protected $HIERARCHY_LEFT_INDEX_FLD = 'hier_left';
  166. protected $HIERARCHY_RIGHT_INDEX_FLD = 'hier_right';
  167. protected $HIERARCHY_PARENT_ID_FLD = 'parent_id';
  168. protected $HIERARCHY_DEFINITION_TABLE = null;
  169. protected $HIERARCHY_ID_FLD = null;
  170. protected $HIERARCHY_POLY_TABLE = null;
  171. # ------------------------------------------------------
  172. # Change logging
  173. # ------------------------------------------------------
  174. protected $UNIT_ID_FIELD = null;
  175. protected $LOG_CHANGES_TO_SELF = false;
  176. protected $LOG_CHANGES_USING_AS_SUBJECT = array(
  177. "FOREIGN_KEYS" => array(
  178. ),
  179. "RELATED_TABLES" => array(
  180. )
  181. );
  182. # ------------------------------------------------------
  183. # Search
  184. # ------------------------------------------------------
  185. protected $SEARCH_CLASSNAME = 'UserGroupSearch';
  186. protected $SEARCH_RESULT_CLASSNAME = 'UserGroupSearchResult';
  187. # ------------------------------------------------------
  188. # $FIELDS contains information about each field in the table. The order in which the fields
  189. # are listed here is the order in which they will be returned using getFields()
  190. protected $FIELDS;
  191. # ------------------------------------------------------
  192. # --- Constructor
  193. #
  194. # This is a function called when a new instance of this object is created. This
  195. # standard constructor supports three calling modes:
  196. #
  197. # 1. If called without parameters, simply creates a new, empty objects object
  198. # 2. If called with a single, valid primary key value, creates a new objects object and loads
  199. # the record identified by the primary key value
  200. #
  201. # ------------------------------------------------------
  202. public function __construct($pn_id=null) {
  203. parent::__construct($pn_id); # call superclass constructor
  204. }
  205. # ------------------------------------------------------
  206. /**
  207. * Returns number of available user groups.
  208. * By default it will return all groups. If a user_id is specified in $pn_user_id then only groups created (and administered) by
  209. * the user are counted.
  210. *
  211. * @param int $pn_user_id Optional user_id to restrict group list with. If set only groups owned by the specified user are counted
  212. *
  213. * @return int Numer of groups
  214. *
  215. */
  216. public function getGroupCount($pn_user_id=null) {
  217. $o_db = $this->getDb();
  218. $vs_user_id_sql = '';
  219. if ((int)$pn_user_id) {
  220. $vs_user_id_sql = ' AND (user_id = '.(int)$pn_user_id.')';
  221. }
  222. $vs_sql = "
  223. SELECT count(*) c
  224. FROM ca_user_groups
  225. WHERE
  226. parent_id IS NOT NULL
  227. {$vs_user_id_sql}
  228. {$vs_sort}
  229. ";
  230. $qr_groups = $o_db->query($vs_sql);
  231. if ($qr_groups->nextRow()) {
  232. return (int)$qr_groups->get('c');
  233. }
  234. return 0;
  235. }
  236. # ------------------------------------------------------
  237. /**
  238. * Returns list of available user groups, sorted by $ps_sort_field in ascending or descending order by $ps_sort_direction.
  239. * By default it will return all groups. If a user_id is specified in $pn_user_id then only groups created (and administered) by
  240. * the user are returned.
  241. *
  242. * @param string $ps_sort_field Optional field to sort the group list on (ex. 'name'; 'code'; 'rank')
  243. * @param string $ps_sort_direction Optional direction of sort, either 'asc' for ascending or 'desc' for descending; default is ascending
  244. * @param int $pn_user_id Optional user_id to restrict group list with. If set only groups owned by the specified user are returned. If not set then only groups with no user_id set are returned.
  245. *
  246. * @return array List of groups, keyed on group_id. Values are arrays keyed on field name.
  247. *
  248. */
  249. public function getGroupList($ps_sort_field='', $ps_sort_direction='asc', $pn_user_id=null) {
  250. $o_db = $this->getDb();
  251. $va_valid_sorts = array('name', 'code');
  252. if (!in_array($ps_sort_field, $va_valid_sorts)) {
  253. $ps_sort_field = 'name';
  254. }
  255. $va_valid_sort_directions = array('asc', 'desc');
  256. if (!in_array($ps_sort_direction, $va_valid_sort_directions)) {
  257. $ps_sort_direction = 'asc';
  258. }
  259. $vs_user_id_sql = '';
  260. if ((int)$pn_user_id) {
  261. $vs_user_id_sql = ' AND (user_id = '.(int)$pn_user_id.')';
  262. } else {
  263. $vs_user_id_sql = ' AND (user_id IS NULL)';
  264. }
  265. $vs_sort = "ORDER BY {$ps_sort_field} {$ps_sort_direction}";
  266. $vs_sql = "
  267. SELECT *
  268. FROM ca_user_groups
  269. WHERE
  270. parent_id IS NOT NULL
  271. {$vs_user_id_sql}
  272. {$vs_sort}
  273. ";
  274. $qr_groups = $o_db->query($vs_sql);
  275. $va_groups = array();
  276. while($qr_groups->nextRow()) {
  277. $vn_group_id = $qr_groups->get('group_id');
  278. $qr_members = $o_db->query("
  279. SELECT u.fname, u.lname, u.email, u.user_id
  280. FROM ca_users u
  281. INNER JOIN ca_users_x_groups AS uxg ON u.user_id = uxg.user_id
  282. WHERE
  283. uxg.group_id = ?
  284. ", (int)$vn_group_id);
  285. $va_members = $va_member_list = array();
  286. while($qr_members->nextRow()) {
  287. $va_members[$qr_members->get('user_id')] = $qr_members->getRow();
  288. $va_member_list[] = $qr_members->get('fname').' '.$qr_members->get('lname');
  289. }
  290. $va_groups[$vn_group_id] = $qr_groups->getRow();
  291. $va_groups[$vn_group_id]['members'] = $va_members;
  292. $va_groups[$vn_group_id]['member_list'] = join(', ', $va_member_list);
  293. }
  294. return $va_groups;
  295. }
  296. # ------------------------------------------------------
  297. public function getName() {
  298. return $this->get('name');
  299. }
  300. # ------------------------------------------------------
  301. # --- Roles
  302. # ------------------------------------------------------
  303. /**
  304. * Add roles to current user.
  305. *
  306. * @access public
  307. * @param mixed $pm_roles Single role or list (array) of roles to add. Roles may be specified by name, code or id.
  308. * @return integer Returns number of roles added or false if there was an error. The number of roles added will not necessarily match the number of roles you tried to add. If you try to add the same role twice, or to add a role that already exists for this user, addRoles() will silently ignore it.
  309. */
  310. function addRoles($pm_roles) {
  311. if (!is_array($pm_roles)) {
  312. $pm_roles = array($pm_roles);
  313. }
  314. if ($pn_group_id = $this->getPrimaryKey()) {
  315. $t_role = new ca_user_roles();
  316. $vn_roles_added = 0;
  317. $o_db = $this->getDb();
  318. foreach ($pm_roles as $vs_role) {
  319. $vs_role = trim(preg_replace('![\n\r\t]+!', '', $vs_role));
  320. $vb_got_role = 0;
  321. if (is_numeric($vs_role)) {
  322. $vb_got_role = $t_role->load($vs_role);
  323. }
  324. if (!$vb_got_role) {
  325. if (!$t_role->load(array("code" => $vs_role))) {
  326. if (!$t_role->load(array("name" => $vs_role))) {
  327. continue;
  328. }
  329. }
  330. $vb_got_role = 1;
  331. }
  332. $o_db->query("
  333. INSERT INTO ca_groups_x_roles
  334. (group_id, role_id)
  335. VALUES
  336. (?, ?)
  337. ", (int)$pn_group_id, (int)$t_role->getPrimaryKey());
  338. if ($o_db->numErrors() == 0) {
  339. $vn_roles_added++;
  340. } else {
  341. $this->postError(930, _t("Database error adding role '%1': %2", $vs_role, join(';', $o_db->getErrors())),"ca_user_groups->addRoles()");
  342. }
  343. }
  344. return $vn_roles_added;
  345. } else {
  346. return false;
  347. }
  348. }
  349. # ------------------------------------------------------
  350. /**
  351. * Remove roles from current group.
  352. *
  353. * @access public
  354. * @param mixed $pm_roles Single role or list (array) of roles to remove. Roles may be specified by name, code or id.
  355. * @return bool Returns true on success, false on error.
  356. */
  357. function removeRoles($pm_roles) {
  358. if (!is_array($pm_roles)) {
  359. $pm_roles = array($pm_roles);
  360. }
  361. if ($vn_group_id = $this->getPrimaryKey()) {
  362. $t_role = new ca_user_roles();
  363. $vn_roles_added = 0;
  364. $va_role_ids = array();
  365. foreach ($pm_roles as $vs_role) {
  366. $vb_got_role = 0;
  367. if (is_numeric($vs_role)) {
  368. $vb_got_role = $t_role->load($vs_role);
  369. }
  370. if (!$vb_got_role) {
  371. if (!$t_role->load(array("name" => $vs_role))) {
  372. if (!$t_role->load(array("code" => $vs_role))) {
  373. continue;
  374. }
  375. }
  376. $vb_got_role = 1;
  377. }
  378. if ($vb_got_role) {
  379. $va_role_ids[] = $t_role->getPrimaryKey();
  380. }
  381. }
  382. if (sizeof($va_role_ids) > 0) {
  383. $o_db = $this->getDb();
  384. $o_db->query("
  385. DELETE FROM ca_groups_x_roles WHERE (group_id = ?) AND (role_id IN (".join(", ", $va_role_ids)."))
  386. ", (int)$vn_group_id);
  387. if ($o_db->numErrors()) {
  388. $this->postError(931, _t("Database error: %1", join(';', $o_db->getErrors())),"ca_user_groups->removeRoles()");
  389. return false;
  390. } else {
  391. return true;
  392. }
  393. } else {
  394. $this->postError(931, _t("No roles specified"),"ca_user_groups->removeRoles()");
  395. return false;
  396. }
  397. } else {
  398. return false;
  399. }
  400. }
  401. # ------------------------------------------------------
  402. /**
  403. * Removes all roles from current group.
  404. *
  405. * @access public
  406. * @return bool Returns true on success, false on error.
  407. */
  408. function removeAllRoles() {
  409. if ($vn_group_id = $this->getPrimaryKey()) {
  410. $o_db = $this->getDb();
  411. $o_db->query("DELETE FROM ca_groups_x_roles WHERE group_id = ?", (int)$vn_group_id);
  412. if ($o_db->numErrors()) {
  413. $this->postError(931, _t("Database error: %1", join(';', $o_db->getErrors())),"ca_user_groups->removeAllRoles()");
  414. return false;
  415. } else {
  416. return true;
  417. }
  418. } else {
  419. return false;
  420. }
  421. }
  422. # ------------------------------------------------------
  423. /**
  424. * Get list of all roles supported by the application. If you want to get the current group's roles, use getGroupRoles()
  425. *
  426. * @access public
  427. * @return integer Returns associative array of roles. Key is role id, value is array containing information about the role.
  428. *
  429. * The role information array contains the following keys:
  430. * role_id (numeric id you can use in addRoles(), deleteRoles(), hasRole(), etc.)
  431. * name (the full name of the role)
  432. * code (a short code used for the role)
  433. * description (narrative description of role)
  434. */
  435. function getRoleList() {
  436. $t_role = new ca_user_roles();
  437. return $t_role->getRoleList();
  438. }
  439. # ------------------------------------------------------
  440. /**
  441. * Get list of roles the current group has
  442. *
  443. * @access public
  444. * @return array Returns associative array of roles. Key is role id, value is array containing information about the role.
  445. *
  446. * The role information array contains the following keys:
  447. * role_id (numeric id you can use in addRoles(), deleteRoles(), hasRole(), etc.)
  448. * name (the full name of the role)
  449. * code (a short code used for the role)
  450. * description (narrative description of role)
  451. */
  452. function getGroupRoles() {
  453. if ($vn_group_id = $this->getPrimaryKey()) {
  454. $o_db = $this->getDb();
  455. $qr_res = $o_db->query("
  456. SELECT wur.role_id, wur.name, wur.code, wur.description, wur.rank
  457. FROM ca_user_roles wur
  458. INNER JOIN ca_groups_x_roles AS wgxr ON wgxr.role_id = wur.role_id
  459. WHERE wgxr.group_id = ?
  460. ORDER BY wur.rank
  461. ", (int)$vn_group_id);
  462. $va_roles = array();
  463. while($qr_res->nextRow()) {
  464. $va_roles[$qr_res->get("role_id")] = $qr_res->getRow();
  465. }
  466. return $va_roles;
  467. } else {
  468. return false;
  469. }
  470. }
  471. # ------------------------------------------------------
  472. /**
  473. * Get list of users who are members of the current group
  474. *
  475. * @access public
  476. * @return array Returns associative array of users. Key is user id, value is array containing information about the user.
  477. *
  478. * The role information array contains the following keys:
  479. * user_id
  480. * user_name (user's login name)
  481. * lname (user's first name)
  482. * fname (user's last name)
  483. * email (user's email address)
  484. */
  485. function getGroupUsers() {
  486. if ($vn_group_id = $this->getPrimaryKey()) {
  487. $o_db = $this->getDb();
  488. $qr_res = $o_db->query("
  489. SELECT wu.user_id, wu.user_name, wu.fname, wu.lname, wu.email
  490. FROM ca_users wu
  491. INNER JOIN ca_users_x_groups AS wuxg ON wu.user_id = wuxg.user_id
  492. WHERE wuxg.group_id = ?
  493. ORDER BY wu.lname, wu.fname
  494. ", (int)$vn_group_id);
  495. $va_users = array();
  496. while($qr_res->nextRow()) {
  497. $va_users[$qr_res->get("user_id")] = $qr_res->getRow();
  498. }
  499. return $va_users;
  500. } else {
  501. return false;
  502. }
  503. }
  504. # ------------------------------------------------------
  505. /**
  506. * Determines whether current group has a specified role.
  507. *
  508. * @access public
  509. * @param mixed $pm_role The role to test for the current group. Role may be specified by name, code or id.
  510. * @return bool Returns true if group has the role, false if not.
  511. */
  512. function hasGroupRole($ps_role) {
  513. if (!($vn_group_id = $this->getPrimaryKey())) {
  514. return false;
  515. }
  516. $vb_got_role = 0;
  517. $t_role = new ca_user_roles();
  518. if (is_numeric($ps_role)) {
  519. $vb_got_role = $t_role->load($ps_role);
  520. }
  521. if (!$vb_got_role) {
  522. if (!$t_role->load(array("name" => $ps_role))) {
  523. if (!$t_role->load(array("code" => $ps_role))) {
  524. return false;
  525. }
  526. }
  527. $vb_got_role = 1;
  528. }
  529. if ($vb_got_role) {
  530. $o_db = $this->getDb();
  531. $qr_res = $o_db->query("
  532. SELECT *
  533. FROM ca_groups_x_roles
  534. WHERE
  535. (group_id = ?) AND
  536. (role_id = ?)
  537. ", (int)$pn_group_id, (int)$t_role->getPrimaryKey());
  538. if (!$qr_res) { return false; }
  539. if ($qr_res->nextRow()) {
  540. return true;
  541. } else {
  542. return false;
  543. }
  544. } else {
  545. $this->postError(940, _t("Invalid role '%1'", $ps_role),"ca_user_groups->hasRole()");
  546. return false;
  547. }
  548. }
  549. # ------------------------------------------------------
  550. /**
  551. * Returns HTML multiple <select> with full list of roles for currently loaded group
  552. *
  553. * @param array $pa_options (optional) array of options. Keys are:
  554. * size = height of multiple select, in rows; default is 8
  555. * name = HTML form element name to apply to role <select>; default is 'roles'
  556. * id = DOM id to apply to role <select>; default is no id
  557. * label = String to label form element with
  558. * @return string Returns HTML containing form element and form label
  559. */
  560. public function roleListAsHTMLFormElement($pa_options=null) {
  561. $vn_size = (isset($pa_options['size']) && ($pa_options['size'] > 0)) ? $pa_options['size'] : 8;
  562. $vs_name = (isset($pa_options['name'])) ? $pa_options['name'] : 'roles';
  563. $vs_id = (isset($pa_options['id'])) ? $pa_options['id'] : '';
  564. $vs_label = (isset($pa_options['label'])) ? $pa_options['label'] : _t('Roles');
  565. $va_roles = $this->getRoleList();
  566. $vs_buf = '';
  567. if (sizeof($va_roles)) {
  568. if(!$va_group_roles = $this->getGroupRoles()) { $va_group_roles = array(); }
  569. $vs_buf .= "<select multiple='1' name='{$vs_name}[]' size='{$vn_size}' id='{$vs_id}'>\n";
  570. foreach($va_roles as $vn_role_id => $va_role_info) {
  571. $SELECTED = (isset($va_group_roles[$vn_role_id]) && $va_group_roles[$vn_role_id]) ? "SELECTED='1'" : "";
  572. $vs_buf .= "<option value='{$vn_role_id}' {$SELECTED}>".$va_role_info['name']." [".$va_role_info["code"]."]</option>\n";
  573. }
  574. $vs_buf .= "</select>\n";
  575. }
  576. if ($vs_format = $this->_CONFIG->get('form_element_display_format')) {
  577. $vs_format = str_replace("^ELEMENT", $vs_buf, $vs_format);
  578. $vs_format = str_replace("^LABEL", $vs_label, $vs_format);
  579. $vs_format = str_replace("^ERRORS", '', $vs_format);
  580. $vs_buf = str_replace("^EXTRA", '', $vs_format);
  581. }
  582. return $vs_buf;
  583. }
  584. # ------------------------------------------------------
  585. /**
  586. * Add users to current group
  587. *
  588. * @access public
  589. * @param mixed $pm_user_ids Single group or list (array) of user_ids to add to the current group. Users must be specified with user_ids
  590. * @return integer Returns number of users added to the group or false if there was an error. The number of users added will not necessarily match the number of users you passed in $pm_user_ids. If you try to add the user to the same group twice, addUsers() will silently ignore it.
  591. */
  592. function addUsers($pm_user_ids) {
  593. if (!is_array($pm_user_ids)) {
  594. $pm_user_ids = array($pm_user_ids);
  595. }
  596. if ($pn_group_id = $this->getPrimaryKey()) {
  597. $t_user = new ca_users();
  598. $vn_users_added = 0;
  599. foreach ($pm_user_ids as $pn_user_id) {
  600. if (!($t_user->load($pn_user_id))) {
  601. continue;
  602. }
  603. $o_db = $this->getDb();
  604. $o_db->query("
  605. INSERT INTO ca_users_x_groups
  606. (user_id, group_id)
  607. VALUES
  608. (?, ?)
  609. ", (int)$pn_user_id, (int)$pn_group_id);
  610. if ($o_db->numErrors() == 0) {
  611. $vn_users_added++;
  612. } else {
  613. $this->postError(935, _t("Database error: %1", join(';', $o_db->getErrors())),"ca_user_groups->addUsers()");
  614. }
  615. }
  616. return $vn_users_added;
  617. } else {
  618. return false;
  619. }
  620. }
  621. # ----------------------------------------
  622. /**
  623. * Remove current user from one or more groups.
  624. *
  625. * @access public
  626. * @param mixed $pm_groups Single group or list (array) of user_ids to remove from current group. Users must be specified by user_id
  627. * @return bool Returns true on success, false on error.
  628. */
  629. function removeUsers($pm_user_ids) {
  630. if (!is_array($pm_user_ids)) {
  631. $pm_user_ids = array($pm_user_ids);
  632. }
  633. if ($pn_group_id = $this->getPrimaryKey()) {
  634. $t_user = new ca_users();
  635. $vn_users_added = 0;
  636. $va_user_ids = array();
  637. foreach ($pm_user_ids as $pn_user_id) {
  638. if (!($t_user->load((int)$pn_user_id))) {
  639. continue;
  640. }
  641. $va_user_ids[] = intval($t_user->getPrimaryKey());
  642. }
  643. if (sizeof($va_user_ids) > 0) {
  644. $o_db = $this->getDb();
  645. $o_db->query("
  646. DELETE FROM ca_users_x_groups
  647. WHERE (group_id = ?) AND (user_id IN (".join(", ", $va_user_ids)."))
  648. ", (int)$pn_group_id);
  649. if ($o_db->numErrors()) {
  650. $this->postError(936, _t("Database error: %1", join(';', $o_db->getErrors())),"ca_user_groups->removeUsers()");
  651. return false;
  652. } else {
  653. return true;
  654. }
  655. } else {
  656. $this->postError(945, _t("No users specified"),"ca_user_groups->removeUsers()");
  657. return false;
  658. }
  659. } else {
  660. return false;
  661. }
  662. }
  663. # ----------------------------------------
  664. /**
  665. * Remove all users from current group.
  666. *
  667. * @access public
  668. * @return bool Returns true on success, false on error.
  669. */
  670. function removeAllUsers() {
  671. if ($vn_group_id = $this->getPrimaryKey()) {
  672. $o_db = $this->getDb();
  673. $o_db->query("DELETE FROM ca_users_x_groups WHERE group_id = ?", (int)$vn_group_id);
  674. if ($o_db->numErrors()) {
  675. $this->postError(936, _t("Database error: %1", join(';', $o_db->getErrors())),"ca_user_groups->removeAllUsers()");
  676. return false;
  677. } else {
  678. return true;
  679. }
  680. } else {
  681. return false;
  682. }
  683. }
  684. # ----------------------------------------
  685. }
  686. ?>