1 <?php
2 /**
3 * Role.php
4 *
5 * @author Elvyrra SAS
6 * @license http://rem.mit-license.org/ MIT
7 */
8 namespace Hawk;
9
10 /**
11 * This model describes the roles that can be affected to the users
12 *
13 * @package BaseModels
14 */
15 class Role extends Model{
16 /**
17 * The associated table
18 *
19 * @var string
20 */
21 public static $tablename = "Role";
22
23 /**
24 * The id corresponding to the 'Guest' role
25 */
26 const GUEST_ROLE_ID = 0;
27
28 /**
29 * The id corresponding to the 'Admin' role
30 */
31 const ADMIN_ROLE_ID = 1;
32
33 /**
34 * Get all roles
35 *
36 * @param string $index The field to use as key in the returned array
37 * @param array $fields The fields to get for each role. If not set, all table fields are got
38 * @param array $order The order instructions for the returned array
39 * @param bool $includeGuest If set to true, then include 'Guest' role to the result
40 *
41 * @return array The roles list
42 */
43 public static function getAll($index = null, $fields = array(), $order = array(), $includeGuest= false){
44 if($includeGuest) {
45 return parent::getAll($index, $fields, $order);
46 }
47 else{
48 $example = array('id' => array('$ne' => self::GUEST_ROLE_ID));
49 return self::getListByExample(new DBExample($example), $index, $fields, $order);
50 }
51 }
52
53 /**
54 * Check if the role is removable. A role is removable if it's not 'Guest' or 'Admin', and not the default role
55 *
56 * @return boolean
57 */
58 public function isRemovable(){
59 return $this->removable && Option::get('roles.default-role') != $this->id;
60 }
61
62 /**
63 * Get the label of the role in the current language
64 *
65 * @return string
66 */
67 public function getLabel(){
68 return Lang::get('roles.role-' . $this->id . '-label');
69 }
70 }