|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Gewaer\Models; |
|
5
|
|
|
|
|
6
|
|
|
use Gewaer\Exception\ModelException; |
|
7
|
|
|
use Phalcon\Di; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AppsPlans |
|
11
|
|
|
* |
|
12
|
|
|
* @package Gewaer\Models |
|
13
|
|
|
* |
|
14
|
|
|
* @property Users $user |
|
15
|
|
|
* @property Config $config |
|
16
|
|
|
* @property Apps $app |
|
17
|
|
|
* @property Companies $defaultCompany |
|
18
|
|
|
* @property \Phalcon\Di $di |
|
19
|
|
|
*/ |
|
20
|
|
|
class AppsPlans extends AbstractModel |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var integer |
|
25
|
|
|
*/ |
|
26
|
|
|
public $id; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var integer |
|
31
|
|
|
*/ |
|
32
|
|
|
public $apps_id; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
public $name; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
public $description; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
public $stripe_id; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
public $stripe_plan; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* @var double |
|
61
|
|
|
*/ |
|
62
|
|
|
public $pricing; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* |
|
66
|
|
|
* @var integer |
|
67
|
|
|
*/ |
|
68
|
|
|
public $currency_id; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* |
|
72
|
|
|
* @var integer |
|
73
|
|
|
*/ |
|
74
|
|
|
public $free_trial_dates; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* @var integer |
|
79
|
|
|
*/ |
|
80
|
|
|
public $is_default; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
public $created_at; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* |
|
90
|
|
|
* @var string |
|
91
|
|
|
*/ |
|
92
|
|
|
public $updated_at; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
* @var integer |
|
97
|
|
|
*/ |
|
98
|
|
|
public $is_deleted; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Initialize method for model. |
|
102
|
|
|
*/ |
|
103
|
11 |
|
public function initialize() |
|
104
|
|
|
{ |
|
105
|
11 |
|
$this->setSource('apps_plans'); |
|
106
|
|
|
|
|
107
|
11 |
|
$this->belongsTo( |
|
108
|
11 |
|
'apps_id', |
|
109
|
11 |
|
'Gewaer\Models\Apps', |
|
110
|
11 |
|
'id', |
|
111
|
11 |
|
['alias' => 'app'] |
|
112
|
|
|
); |
|
113
|
|
|
|
|
114
|
11 |
|
$this->hasMany( |
|
115
|
11 |
|
'apps_id', |
|
116
|
11 |
|
'Gewaer\Models\AppsPlansSettings', |
|
117
|
11 |
|
'apps_id', |
|
118
|
11 |
|
['alias' => 'settings'] |
|
119
|
|
|
); |
|
120
|
11 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns table name mapped in the model. |
|
124
|
|
|
* |
|
125
|
|
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
11 |
|
public function getSource() : string |
|
128
|
|
|
{ |
|
129
|
11 |
|
return 'apps_plans'; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Just a preatty function that returns the same object for |
|
134
|
|
|
* |
|
135
|
|
|
* $app->settings()->set(key, value); |
|
136
|
|
|
* $app->settings()->get(key); |
|
137
|
|
|
* $app->settings()->get(key)->delete(); |
|
138
|
|
|
* |
|
139
|
|
|
* @return AppsPlans |
|
140
|
|
|
*/ |
|
141
|
|
|
public function settings() : AppsPlans |
|
142
|
|
|
{ |
|
143
|
|
|
return $this; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the default plan for this given app |
|
148
|
|
|
* |
|
149
|
|
|
* @return AppsPlans |
|
150
|
|
|
*/ |
|
151
|
3 |
|
public static function getDefaultPlan() : AppsPlans |
|
152
|
|
|
{ |
|
153
|
3 |
|
return AppsPlans::findFirst([ |
|
154
|
3 |
|
'conditions' => 'apps_id = ?0 and is_default = 1', |
|
155
|
3 |
|
'bind' => [Di::getDefault()->getApp()->getId()] |
|
156
|
|
|
]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Get the value of the settins by it key |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $key |
|
163
|
|
|
* @param string $value |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public function get(string $key) : ?string |
|
166
|
|
|
{ |
|
167
|
1 |
|
$setting = AppsPlansSettings::findFirst([ |
|
168
|
1 |
|
'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2', |
|
169
|
1 |
|
'bind' => [$this->getId(), $this->apps_id, $key] |
|
170
|
|
|
]); |
|
171
|
|
|
|
|
172
|
1 |
|
if (is_object($setting)) { |
|
173
|
|
|
return (string) $setting->value; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
1 |
|
return null; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Set a setting for the given app |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $key |
|
183
|
|
|
* @param string $value |
|
184
|
|
|
*/ |
|
185
|
3 |
|
public function set(string $key, $value) : bool |
|
186
|
|
|
{ |
|
187
|
3 |
|
$setting = AppsPlansSettings::findFirst([ |
|
188
|
3 |
|
'conditions' => 'apps_plans_id = ?0 and apps_id = ?1 and key = ?2', |
|
189
|
3 |
|
'bind' => [$this->getId(), $this->apps_id, $key] |
|
190
|
|
|
]); |
|
191
|
|
|
|
|
192
|
3 |
|
if (!is_object($setting)) { |
|
193
|
1 |
|
$setting = new AppsPlansSettings(); |
|
194
|
1 |
|
$setting->apps_plans_id = $this->getId(); |
|
195
|
1 |
|
$setting->apps_id = $this->getId(); |
|
196
|
1 |
|
$setting->key = $key; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
3 |
|
$setting->value = $value; |
|
200
|
|
|
|
|
201
|
3 |
|
if (!$setting->save()) { |
|
202
|
|
|
throw new ModelException((string)current($setting->getMessages())); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
3 |
|
return true; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* After save |
|
210
|
|
|
* |
|
211
|
|
|
* @return void |
|
212
|
|
|
*/ |
|
213
|
|
|
public function afterSave() |
|
214
|
|
|
{ |
|
215
|
|
|
//if we udpate the is_default for this plan we need to remove all others and update the main app |
|
216
|
|
|
if ($this->is_default) { |
|
217
|
|
|
$this->app->default_apps_plan_id = $this->getId(); |
|
218
|
|
|
$this->app->update(); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|