Search Results

Go to the documentation of this file.
1 <?php
2 namespace App\Models;
3 
6 
12 final class Company extends Model
13 {
14  protected $table = 'companies';
15 
16  // Declare the rules for the model validation
17  protected $rules = ['name' => 'required|min:2|max:255|unique:companies,name'];
18 
26  protected $injectUniqueIdentifier = true;
27  use ValidatingTrait;
28 
29 
35  protected $fillable = ['name'];
36 
37  private static function isFullMultipleCompanySupportEnabled()
38  {
39  $settings = Setting::getSettings();
40 
41  // NOTE: this can happen when seeding the database
42  if (is_null($settings)) {
43  return false;
44  } else {
45  return $settings->full_multiple_companies_support == 1;
46  }
47  }
48 
49  private static function scopeCompanyablesDirectly($query, $column = 'company_id')
50  {
51  if (Auth::user()) {
52  $company_id = Auth::user()->company_id;
53  } else {
54  $company_id = null;
55  }
56 
57  if ($company_id == null) {
58  return $query;
59  } else {
60  return $query->where($column, '=', $company_id);
61  }
62  }
63 
64  public static function getSelectList()
65  {
66  $select_company = Lang::get('general.select_company');
67  return ['0' => $select_company] + DB::table('companies')->orderBy('name', 'ASC')->lists('name', 'id');
68  }
69 
70  public static function getIdFromInput($unescaped_input)
71  {
72  $escaped_input = e($unescaped_input);
73 
74  if ($escaped_input == '0') {
75  return null;
76  } else {
77  return $escaped_input;
78  }
79  }
80 
81  public static function getIdForCurrentUser($unescaped_input)
82  {
83  if (!static::isFullMultipleCompanySupportEnabled()) {
84  return static::getIdFromInput($unescaped_input);
85  } else {
86  $current_user = Auth::user();
87 
88  if ($current_user->company_id != null) {
89  return $current_user->company_id;
90  } else {
91  return static::getIdFromInput($unescaped_input);
92  }
93  }
94  }
95 
96  public static function isCurrentUserHasAccess($companyable)
97  {
98  if (is_null($companyable)) {
99  return false;
100  } elseif (!static::isFullMultipleCompanySupportEnabled()) {
101  return true;
102  } else {
103  $current_user_company_id = Auth::user()->company_id;
104  $companyable_company_id = $companyable->company_id;
105 
106  return ($current_user_company_id == null || $current_user_company_id == $companyable_company_id);
107  }
108  }
109 
110  public static function isCurrentUserAuthorized()
111  {
112  return (!static::isFullMultipleCompanySupportEnabled() || Auth::user()->company_id == null);
113  }
114 
115  public static function canManageUsersCompanies()
116  {
117  return (!static::isFullMultipleCompanySupportEnabled() || Auth::user()->isSuperUser() ||
118  Auth::user()->company_id == null);
119  }
120 
121  public static function getIdForUser($unescaped_input)
122  {
123  if (!static::isFullMultipleCompanySupportEnabled() || Auth::user()->isSuperUser()) {
124  return static::getIdFromInput($unescaped_input);
125  } else {
126  return static::getIdForCurrentUser($unescaped_input);
127  }
128  }
129 
130  public static function scopeCompanyables($query, $column = 'company_id')
131  {
132  if (!static::isFullMultipleCompanySupportEnabled()) {
133  return $query;
134  } else {
135  return static::scopeCompanyablesDirectly($query, $column);
136  }
137  }
138 
139  public static function scopeCompanyableChildren(array $companyable_names, $query)
140  {
141  if (count($companyable_names) == 0) {
142  throw new Exception('No Companyable Children to scope');
143  } elseif (!static::isFullMultipleCompanySupportEnabled()) {
144  return $query;
145  } else {
146  $f = function ($q) {
147 
148  static::scopeCompanyablesDirectly($q);
149  };
150 
151  $q = $query->where(function ($q) use ($companyable_names, $f) {
152  $q2 = $q->whereHas($companyable_names[0], $f);
153 
154  for ($i = 1; $i < count($companyable_names); $i++) {
155  $q2 = $q2->orWhereHas($companyable_names[$i], $f);
156  }
157  });
158  return $q;
159  }
160  }
161 
162  public static function getName($companyable)
163  {
164  $company = $companyable->company;
165 
166  if (is_null($company)) {
167  return '';
168  } else {
169  return e($company->name);
170  }
171  }
172 }
static getIdForUser($unescaped_input)
Definition: Company.php:121
static getIdForCurrentUser($unescaped_input)
Definition: Company.php:81
Model for Companies.
Definition: Company.php:12
static scopeCompanyableChildren(array $companyable_names, $query)
Definition: Company.php:139
static isCurrentUserHasAccess($companyable)
Definition: Company.php:96
static getIdFromInput($unescaped_input)
Definition: Company.php:70
static getSettings()
Definition: Setting.php:33
static getSelectList()
Definition: Company.php:64
static isCurrentUserAuthorized()
Definition: Company.php:110
static scopeCompanyables($query, $column= 'company_id')
Definition: Company.php:130
static getName($companyable)
Definition: Company.php:162
static canManageUsersCompanies()
Definition: Company.php:115