@@ -1,40 +1,40 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * CodeIgniter |
|
4 | - * |
|
5 | - * An open source application development framework for PHP |
|
6 | - * |
|
7 | - * This content is released under the MIT License (MIT) |
|
8 | - * |
|
9 | - * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
|
10 | - * |
|
11 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
12 | - * of this software and associated documentation files (the "Software"), to deal |
|
13 | - * in the Software without restriction, including without limitation the rights |
|
14 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
15 | - * copies of the Software, and to permit persons to whom the Software is |
|
16 | - * furnished to do so, subject to the following conditions: |
|
17 | - * |
|
18 | - * The above copyright notice and this permission notice shall be included in |
|
19 | - * all copies or substantial portions of the Software. |
|
20 | - * |
|
21 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
22 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
23 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
24 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
25 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
26 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
27 | - * THE SOFTWARE. |
|
28 | - * |
|
29 | - * @package CodeIgniter |
|
30 | - * @author EllisLab Dev Team |
|
31 | - * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
|
32 | - * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
|
33 | - * @license http://opensource.org/licenses/MIT MIT License |
|
34 | - * @link http://codeigniter.com |
|
35 | - * @since Version 1.0.0 |
|
36 | - * @filesource |
|
37 | - */ |
|
3 | + * CodeIgniter |
|
4 | + * |
|
5 | + * An open source application development framework for PHP |
|
6 | + * |
|
7 | + * This content is released under the MIT License (MIT) |
|
8 | + * |
|
9 | + * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
|
10 | + * |
|
11 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
12 | + * of this software and associated documentation files (the "Software"), to deal |
|
13 | + * in the Software without restriction, including without limitation the rights |
|
14 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
15 | + * copies of the Software, and to permit persons to whom the Software is |
|
16 | + * furnished to do so, subject to the following conditions: |
|
17 | + * |
|
18 | + * The above copyright notice and this permission notice shall be included in |
|
19 | + * all copies or substantial portions of the Software. |
|
20 | + * |
|
21 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
22 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
23 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
24 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
25 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
26 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
27 | + * THE SOFTWARE. |
|
28 | + * |
|
29 | + * @package CodeIgniter |
|
30 | + * @author EllisLab Dev Team |
|
31 | + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
|
32 | + * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
|
33 | + * @license http://opensource.org/licenses/MIT MIT License |
|
34 | + * @link http://codeigniter.com |
|
35 | + * @since Version 1.0.0 |
|
36 | + * @filesource |
|
37 | + */ |
|
38 | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
39 | 39 | |
40 | 40 | /** |
@@ -1,40 +1,40 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * CodeIgniter |
|
4 | - * |
|
5 | - * An open source application development framework for PHP |
|
6 | - * |
|
7 | - * This content is released under the MIT License (MIT) |
|
8 | - * |
|
9 | - * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
|
10 | - * |
|
11 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
12 | - * of this software and associated documentation files (the "Software"), to deal |
|
13 | - * in the Software without restriction, including without limitation the rights |
|
14 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
15 | - * copies of the Software, and to permit persons to whom the Software is |
|
16 | - * furnished to do so, subject to the following conditions: |
|
17 | - * |
|
18 | - * The above copyright notice and this permission notice shall be included in |
|
19 | - * all copies or substantial portions of the Software. |
|
20 | - * |
|
21 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
22 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
23 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
24 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
25 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
26 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
27 | - * THE SOFTWARE. |
|
28 | - * |
|
29 | - * @package CodeIgniter |
|
30 | - * @author EllisLab Dev Team |
|
31 | - * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
|
32 | - * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
|
33 | - * @license http://opensource.org/licenses/MIT MIT License |
|
34 | - * @link http://codeigniter.com |
|
35 | - * @since Version 2.0 |
|
36 | - * @filesource |
|
37 | - */ |
|
3 | + * CodeIgniter |
|
4 | + * |
|
5 | + * An open source application development framework for PHP |
|
6 | + * |
|
7 | + * This content is released under the MIT License (MIT) |
|
8 | + * |
|
9 | + * Copyright (c) 2014 - 2015, British Columbia Institute of Technology |
|
10 | + * |
|
11 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
12 | + * of this software and associated documentation files (the "Software"), to deal |
|
13 | + * in the Software without restriction, including without limitation the rights |
|
14 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
15 | + * copies of the Software, and to permit persons to whom the Software is |
|
16 | + * furnished to do so, subject to the following conditions: |
|
17 | + * |
|
18 | + * The above copyright notice and this permission notice shall be included in |
|
19 | + * all copies or substantial portions of the Software. |
|
20 | + * |
|
21 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
22 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
23 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
24 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
25 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
26 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
27 | + * THE SOFTWARE. |
|
28 | + * |
|
29 | + * @package CodeIgniter |
|
30 | + * @author EllisLab Dev Team |
|
31 | + * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/) |
|
32 | + * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/) |
|
33 | + * @license http://opensource.org/licenses/MIT MIT License |
|
34 | + * @link http://codeigniter.com |
|
35 | + * @since Version 2.0 |
|
36 | + * @filesource |
|
37 | + */ |
|
38 | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
39 | 39 | |
40 | 40 | /** |
@@ -32,40 +32,40 @@ |
||
32 | 32 | |
33 | 33 | interface DocSearchInterface { |
34 | 34 | |
35 | - /** |
|
36 | - * The entry point for performing a search of the documentation. |
|
37 | - * |
|
38 | - * @param null $terms |
|
39 | - * @param array $folders |
|
40 | - * |
|
41 | - * @return array|null |
|
42 | - */ |
|
43 | - public function search($terms = null, $folders = []); |
|
35 | + /** |
|
36 | + * The entry point for performing a search of the documentation. |
|
37 | + * |
|
38 | + * @param null $terms |
|
39 | + * @param array $folders |
|
40 | + * |
|
41 | + * @return array|null |
|
42 | + */ |
|
43 | + public function search($terms = null, $folders = []); |
|
44 | 44 | |
45 | - //-------------------------------------------------------------------- |
|
45 | + //-------------------------------------------------------------------- |
|
46 | 46 | |
47 | - /** |
|
48 | - * Stores the name of the callback method to run to convert the source |
|
49 | - * files to viewable files. By default, this should be used to register |
|
50 | - * a Mardown Extended formatter with the system, but could be used to |
|
51 | - * extend the |
|
52 | - * |
|
53 | - * @param string $callback_name |
|
54 | - * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
55 | - * @return $this |
|
56 | - */ |
|
57 | - public function registerFormatter($callback_name='', $cascade=false); |
|
47 | + /** |
|
48 | + * Stores the name of the callback method to run to convert the source |
|
49 | + * files to viewable files. By default, this should be used to register |
|
50 | + * a Mardown Extended formatter with the system, but could be used to |
|
51 | + * extend the |
|
52 | + * |
|
53 | + * @param string $callback_name |
|
54 | + * @param bool $cascade // If FALSE the formatting of a component ends here. If TRUE, will be passed to next formatter. |
|
55 | + * @return $this |
|
56 | + */ |
|
57 | + public function registerFormatter($callback_name='', $cascade=false); |
|
58 | 58 | |
59 | - //-------------------------------------------------------------------- |
|
59 | + //-------------------------------------------------------------------- |
|
60 | 60 | |
61 | - /** |
|
62 | - * Runs the text through the registered formatters. |
|
63 | - * |
|
64 | - * @param $str |
|
65 | - * @return mixed |
|
66 | - */ |
|
67 | - public function format($str); |
|
61 | + /** |
|
62 | + * Runs the text through the registered formatters. |
|
63 | + * |
|
64 | + * @param $str |
|
65 | + * @return mixed |
|
66 | + */ |
|
67 | + public function format($str); |
|
68 | 68 | |
69 | - //-------------------------------------------------------------------- |
|
69 | + //-------------------------------------------------------------------- |
|
70 | 70 | |
71 | 71 | } |
@@ -30,506 +30,506 @@ discard block |
||
30 | 30 | */ |
31 | 31 | class CLI { |
32 | 32 | |
33 | - public static $readline_support = false; |
|
33 | + public static $readline_support = false; |
|
34 | 34 | |
35 | - public static $wait_msg = 'Press any key to continue...'; |
|
35 | + public static $wait_msg = 'Press any key to continue...'; |
|
36 | 36 | |
37 | - public static $segments = []; |
|
37 | + public static $segments = []; |
|
38 | 38 | |
39 | 39 | protected static $options = []; |
40 | 40 | |
41 | 41 | protected static $initialized = false; |
42 | 42 | |
43 | - // Used by progress bar |
|
44 | - protected static $inProgress = false; |
|
45 | - |
|
46 | - protected static $foreground_colors = array( |
|
47 | - 'black' => '0;30', |
|
48 | - 'dark_gray' => '1;30', |
|
49 | - 'blue' => '0;34', |
|
50 | - 'dark_blue' => '1;34', |
|
51 | - 'light_blue' => '1;34', |
|
52 | - 'green' => '0;32', |
|
53 | - 'light_green' => '1;32', |
|
54 | - 'cyan' => '0;36', |
|
55 | - 'light_cyan' => '1;36', |
|
56 | - 'red' => '0;31', |
|
57 | - 'light_red' => '1;31', |
|
58 | - 'purple' => '0;35', |
|
59 | - 'light_purple' => '1;35', |
|
60 | - 'light_yellow' => '0;33', |
|
61 | - 'yellow' => '1;33', |
|
62 | - 'light_gray' => '0;37', |
|
63 | - 'white' => '1;37', |
|
64 | - ); |
|
65 | - |
|
66 | - protected static $background_colors = array( |
|
67 | - 'black' => '40', |
|
68 | - 'red' => '41', |
|
69 | - 'green' => '42', |
|
70 | - 'yellow' => '43', |
|
71 | - 'blue' => '44', |
|
72 | - 'magenta' => '45', |
|
73 | - 'cyan' => '46', |
|
74 | - 'light_gray' => '47', |
|
75 | - ); |
|
76 | - |
|
77 | - //-------------------------------------------------------------------- |
|
78 | - |
|
79 | - /** |
|
80 | - * Static constructor. Parses all the CLI params. |
|
81 | - */ |
|
82 | - public static function _init() |
|
83 | - { |
|
84 | - if (static::$initialized === true) |
|
85 | - { |
|
86 | - return; |
|
87 | - } |
|
88 | - |
|
89 | - if ( ! (PHP_SAPI === 'cli' || defined('STDIN')) ) |
|
90 | - { |
|
91 | - throw new \Exception('Cli class cannot be used outside of the command line.'); |
|
92 | - } |
|
93 | - |
|
94 | - self::parseCommand(); |
|
95 | - |
|
96 | - // Readline is an extension for PHP that makes interactive with PHP much more bash-like |
|
97 | - // http://www.php.net/manual/en/readline.installation.php |
|
98 | - static::$readline_support = extension_loaded('readline'); |
|
99 | - |
|
100 | - static::$initialized = true; |
|
101 | - } |
|
102 | - |
|
103 | - //-------------------------------------------------------------------- |
|
104 | - |
|
105 | - /** |
|
106 | - * Grabs an individual |
|
107 | - * |
|
108 | - * @param $index |
|
109 | - * @return null |
|
110 | - */ |
|
111 | - public static function segment($index) |
|
112 | - { |
|
113 | - if (! isset(static::$segments[$index - 1])) |
|
114 | - { |
|
115 | - return null; |
|
116 | - } |
|
117 | - |
|
118 | - return static::$segments[$index - 1]; |
|
119 | - } |
|
120 | - |
|
121 | - //-------------------------------------------------------------------- |
|
122 | - |
|
123 | - /** |
|
124 | - * Returns the command string portion of the arguments. Used |
|
125 | - * by the 'sprint' CLI script to grab the portion of the arguments |
|
126 | - * that is used to call the CodeIgniter application. |
|
127 | - * |
|
128 | - * @return string |
|
129 | - */ |
|
130 | - public static function cli_string() |
|
131 | - { |
|
132 | - return implode(' ', static::$segments); |
|
133 | - } |
|
134 | - |
|
135 | - //-------------------------------------------------------------------- |
|
136 | - |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Get input from the shell, using readline or the standard STDIN |
|
141 | - * |
|
142 | - * Named options must be in the following formats: |
|
143 | - * php index.php user -v --v -name=John --name=John |
|
144 | - * |
|
145 | - * @param string|int $name the name of the option (int if unnamed) |
|
146 | - * @return string |
|
147 | - */ |
|
148 | - public static function input($prefix = '') |
|
149 | - { |
|
150 | - if (static::$readline_support) |
|
151 | - { |
|
152 | - return readline($prefix); |
|
153 | - } |
|
154 | - |
|
155 | - echo $prefix; |
|
156 | - return fgets(STDIN); |
|
157 | - } |
|
158 | - |
|
159 | - //-------------------------------------------------------------------- |
|
160 | - |
|
161 | - /** |
|
162 | - * Asks the user for input. This can have either 1 or 2 arguments. |
|
163 | - * |
|
164 | - * Usage: |
|
165 | - * |
|
166 | - * // Waits for any key press |
|
167 | - * CLI::prompt(); |
|
168 | - * |
|
169 | - * // Takes any input |
|
170 | - * $color = CLI::prompt('What is your favorite color?'); |
|
171 | - * |
|
172 | - * // Takes any input, but offers default |
|
173 | - * $color = CLI::prompt('What is your favourite color?', 'white'); |
|
174 | - * |
|
175 | - * // Will only accept the options in the array |
|
176 | - * $ready = CLI::prompt('Are you ready?', array('y','n')); |
|
177 | - * |
|
178 | - * @return string the user input |
|
179 | - */ |
|
180 | - public static function prompt() |
|
181 | - { |
|
182 | - $args = func_get_args(); |
|
183 | - |
|
184 | - $options = array(); |
|
185 | - $output = ''; |
|
186 | - $default = null; |
|
187 | - |
|
188 | - // How many we got |
|
189 | - $arg_count = count($args); |
|
190 | - |
|
191 | - // Is the last argument a boolean? True means required |
|
192 | - $required = end($args) === true; |
|
193 | - |
|
194 | - // Reduce the argument count if required was passed, we don't care about that anymore |
|
195 | - $required === true and --$arg_count; |
|
196 | - |
|
197 | - // This method can take a few crazy combinations of arguments, so lets work it out |
|
198 | - switch ($arg_count) |
|
199 | - { |
|
200 | - case 2: |
|
201 | - |
|
202 | - // E.g: $ready = CLI::prompt('Are you ready?', array('y','n')); |
|
203 | - if (is_array($args[1])) |
|
204 | - { |
|
205 | - list($output, $options)=$args; |
|
206 | - } |
|
207 | - |
|
208 | - // E.g: $color = CLI::prompt('What is your favourite color?', 'white'); |
|
209 | - elseif (is_string($args[1])) |
|
210 | - { |
|
211 | - list($output, $default)=$args; |
|
212 | - } |
|
213 | - |
|
214 | - break; |
|
215 | - |
|
216 | - case 1: |
|
217 | - |
|
218 | - // No question (probably been asked already) so just show options |
|
219 | - // E.g: $ready = CLI::prompt(array('y','n')); |
|
220 | - if (is_array($args[0])) |
|
221 | - { |
|
222 | - $options = $args[0]; |
|
223 | - } |
|
224 | - |
|
225 | - // Question without options |
|
226 | - // E.g: $ready = CLI::prompt('What did you do today?'); |
|
227 | - elseif (is_string($args[0])) |
|
228 | - { |
|
229 | - $output = $args[0]; |
|
230 | - } |
|
231 | - |
|
232 | - break; |
|
233 | - } |
|
234 | - |
|
235 | - // If a question has been asked with the read |
|
236 | - if ($output !== '') |
|
237 | - { |
|
238 | - $extra_output = ''; |
|
239 | - |
|
240 | - if ($default !== null) |
|
241 | - { |
|
242 | - $extra_output = ' [ Default: "'.$default.'" ]'; |
|
243 | - } |
|
244 | - |
|
245 | - elseif ($options !== array()) |
|
246 | - { |
|
247 | - $extra_output = ' [ '.implode(', ', $options).' ]'; |
|
248 | - } |
|
249 | - |
|
250 | - fwrite(STDOUT, $output.$extra_output.': '); |
|
251 | - } |
|
252 | - |
|
253 | - // Read the input from keyboard. |
|
254 | - $input = trim(static::input()) ?: $default; |
|
255 | - |
|
256 | - // No input provided and we require one (default will stop this being called) |
|
257 | - if (empty($input) and $required === true) |
|
258 | - { |
|
259 | - static::write('This is required.'); |
|
260 | - static::new_line(); |
|
261 | - |
|
262 | - $input = forward_static_call_array(array(__CLASS__, 'prompt'), $args); |
|
263 | - } |
|
264 | - |
|
265 | - // If options are provided and the choice is not in the array, tell them to try again |
|
266 | - if ( ! empty($options) and ! in_array($input, $options)) |
|
267 | - { |
|
268 | - static::write('This is not a valid option. Please try again.'); |
|
269 | - static::new_line(); |
|
270 | - |
|
271 | - $input = forward_static_call_array(array(__CLASS__, 'prompt'), $args); |
|
272 | - } |
|
273 | - |
|
274 | - return $input; |
|
275 | - } |
|
276 | - |
|
277 | - //-------------------------------------------------------------------- |
|
278 | - |
|
279 | - /** |
|
280 | - * Outputs a string to the cli. If you send an array it will implode them |
|
281 | - * with a line break. |
|
282 | - * |
|
283 | - * @param string|array $text the text to output, or array of lines |
|
284 | - */ |
|
285 | - public static function write($text = '', $foreground = null, $background = null) |
|
286 | - { |
|
287 | - if (is_array($text)) |
|
288 | - { |
|
289 | - $text = implode(PHP_EOL, $text); |
|
290 | - } |
|
291 | - |
|
292 | - if ($foreground or $background) |
|
293 | - { |
|
294 | - $text = static::color($text, $foreground, $background); |
|
295 | - } |
|
296 | - |
|
297 | - fwrite(STDOUT, $text.PHP_EOL); |
|
298 | - } |
|
299 | - |
|
300 | - //-------------------------------------------------------------------- |
|
301 | - |
|
302 | - /** |
|
303 | - * Outputs an error to the CLI using STDERR instead of STDOUT |
|
304 | - * |
|
305 | - * @param string|array $text the text to output, or array of errors |
|
306 | - */ |
|
307 | - public static function error($text = '', $foreground = 'light_red', $background = null) |
|
308 | - { |
|
309 | - if (is_array($text)) |
|
310 | - { |
|
311 | - $text = implode(PHP_EOL, $text); |
|
312 | - } |
|
313 | - |
|
314 | - if ($foreground OR $background) |
|
315 | - { |
|
316 | - $text = static::color($text, $foreground, $background); |
|
317 | - } |
|
318 | - |
|
319 | - fwrite(STDERR, $text.PHP_EOL); |
|
320 | - } |
|
321 | - |
|
322 | - //-------------------------------------------------------------------- |
|
323 | - |
|
324 | - /** |
|
325 | - * Beeps a certain number of times. |
|
326 | - * |
|
327 | - * @param int $num the number of times to beep |
|
328 | - */ |
|
329 | - public static function beep($num = 1) |
|
330 | - { |
|
331 | - echo str_repeat("\x07", $num); |
|
332 | - } |
|
333 | - |
|
334 | - //-------------------------------------------------------------------- |
|
335 | - |
|
336 | - /** |
|
337 | - * Waits a certain number of seconds, optionally showing a wait message and |
|
338 | - * waiting for a key press. |
|
339 | - * |
|
340 | - * @param int $seconds number of seconds |
|
341 | - * @param bool $countdown show a countdown or not |
|
342 | - */ |
|
343 | - public static function wait($seconds = 0, $countdown = false) |
|
344 | - { |
|
345 | - if ($countdown === true) |
|
346 | - { |
|
347 | - $time = $seconds; |
|
348 | - |
|
349 | - while ($time > 0) |
|
350 | - { |
|
351 | - fwrite(STDOUT, $time.'... '); |
|
352 | - sleep(1); |
|
353 | - $time--; |
|
354 | - } |
|
355 | - static::write(); |
|
356 | - } |
|
357 | - |
|
358 | - else |
|
359 | - { |
|
360 | - if ($seconds > 0) |
|
361 | - { |
|
362 | - sleep($seconds); |
|
363 | - } |
|
364 | - else |
|
365 | - { |
|
366 | - static::write(static::$wait_msg); |
|
367 | - static::input(); |
|
368 | - } |
|
369 | - } |
|
370 | - } |
|
371 | - |
|
372 | - |
|
373 | - //-------------------------------------------------------------------- |
|
374 | - |
|
375 | - /** |
|
376 | - * if operating system === windows |
|
377 | - */ |
|
378 | - public static function is_windows() |
|
379 | - { |
|
380 | - return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
|
381 | - } |
|
382 | - |
|
383 | - //-------------------------------------------------------------------- |
|
384 | - |
|
385 | - /** |
|
386 | - * Enter a number of empty lines |
|
387 | - * |
|
388 | - * @param integer Number of lines to output |
|
389 | - * @return void |
|
390 | - */ |
|
391 | - public static function new_line($num = 1) |
|
392 | - { |
|
393 | - // Do it once or more, write with empty string gives us a new line |
|
394 | - for($i = 0; $i < $num; $i++) |
|
395 | - { |
|
396 | - static::write(); |
|
397 | - } |
|
398 | - } |
|
399 | - |
|
400 | - //-------------------------------------------------------------------- |
|
401 | - |
|
402 | - /** |
|
403 | - * Clears the screen of output |
|
404 | - * |
|
405 | - * @return void |
|
406 | - */ |
|
407 | - public static function clear_screen() |
|
408 | - { |
|
409 | - static::is_windows() |
|
410 | - |
|
411 | - // Windows is a bit crap at this, but their terminal is tiny so shove this in |
|
412 | - ? static::new_line(40) |
|
413 | - |
|
414 | - // Anything with a flair of Unix will handle these magic characters |
|
415 | - : fwrite(STDOUT, chr(27)."[H".chr(27)."[2J"); |
|
416 | - } |
|
417 | - |
|
418 | - //-------------------------------------------------------------------- |
|
419 | - |
|
420 | - /** |
|
421 | - * Returns the given text with the correct color codes for a foreground and |
|
422 | - * optionally a background color. |
|
423 | - * |
|
424 | - * @param string $text the text to color |
|
425 | - * @param string $foreground the foreground color |
|
426 | - * @param string $background the background color |
|
427 | - * @param string $format other formatting to apply. Currently only 'underline' is understood |
|
428 | - * @return string the color coded string |
|
429 | - */ |
|
430 | - public static function color($text, $foreground, $background = null, $format=null) |
|
431 | - { |
|
432 | - if (static::is_windows() and ! isset($_SERVER['ANSICON'])) |
|
433 | - { |
|
434 | - return $text; |
|
435 | - } |
|
436 | - |
|
437 | - if ( ! array_key_exists($foreground, static::$foreground_colors)) |
|
438 | - { |
|
439 | - throw new \RuntimeException('Invalid CLI foreground color: '.$foreground); |
|
440 | - } |
|
441 | - |
|
442 | - if ( $background !== null and ! array_key_exists($background, static::$background_colors)) |
|
443 | - { |
|
444 | - throw new \RuntimeException('Invalid CLI background color: '.$background); |
|
445 | - } |
|
446 | - |
|
447 | - $string = "\033[".static::$foreground_colors[$foreground]."m"; |
|
448 | - |
|
449 | - if ($background !== null) |
|
450 | - { |
|
451 | - $string .= "\033[".static::$background_colors[$background]."m"; |
|
452 | - } |
|
453 | - |
|
454 | - if ($format === 'underline') |
|
455 | - { |
|
456 | - $string .= "\033[4m"; |
|
457 | - } |
|
458 | - |
|
459 | - $string .= $text."\033[0m"; |
|
460 | - |
|
461 | - return $string; |
|
462 | - } |
|
463 | - |
|
464 | - //-------------------------------------------------------------------- |
|
465 | - |
|
466 | - public static function getWidth($default=80) |
|
467 | - { |
|
468 | - if (static::is_windows()) |
|
469 | - { |
|
470 | - return $default; |
|
471 | - } |
|
472 | - |
|
473 | - return (int)shell_exec('tput cols'); |
|
474 | - } |
|
43 | + // Used by progress bar |
|
44 | + protected static $inProgress = false; |
|
45 | + |
|
46 | + protected static $foreground_colors = array( |
|
47 | + 'black' => '0;30', |
|
48 | + 'dark_gray' => '1;30', |
|
49 | + 'blue' => '0;34', |
|
50 | + 'dark_blue' => '1;34', |
|
51 | + 'light_blue' => '1;34', |
|
52 | + 'green' => '0;32', |
|
53 | + 'light_green' => '1;32', |
|
54 | + 'cyan' => '0;36', |
|
55 | + 'light_cyan' => '1;36', |
|
56 | + 'red' => '0;31', |
|
57 | + 'light_red' => '1;31', |
|
58 | + 'purple' => '0;35', |
|
59 | + 'light_purple' => '1;35', |
|
60 | + 'light_yellow' => '0;33', |
|
61 | + 'yellow' => '1;33', |
|
62 | + 'light_gray' => '0;37', |
|
63 | + 'white' => '1;37', |
|
64 | + ); |
|
65 | + |
|
66 | + protected static $background_colors = array( |
|
67 | + 'black' => '40', |
|
68 | + 'red' => '41', |
|
69 | + 'green' => '42', |
|
70 | + 'yellow' => '43', |
|
71 | + 'blue' => '44', |
|
72 | + 'magenta' => '45', |
|
73 | + 'cyan' => '46', |
|
74 | + 'light_gray' => '47', |
|
75 | + ); |
|
76 | + |
|
77 | + //-------------------------------------------------------------------- |
|
78 | + |
|
79 | + /** |
|
80 | + * Static constructor. Parses all the CLI params. |
|
81 | + */ |
|
82 | + public static function _init() |
|
83 | + { |
|
84 | + if (static::$initialized === true) |
|
85 | + { |
|
86 | + return; |
|
87 | + } |
|
88 | + |
|
89 | + if ( ! (PHP_SAPI === 'cli' || defined('STDIN')) ) |
|
90 | + { |
|
91 | + throw new \Exception('Cli class cannot be used outside of the command line.'); |
|
92 | + } |
|
93 | + |
|
94 | + self::parseCommand(); |
|
95 | + |
|
96 | + // Readline is an extension for PHP that makes interactive with PHP much more bash-like |
|
97 | + // http://www.php.net/manual/en/readline.installation.php |
|
98 | + static::$readline_support = extension_loaded('readline'); |
|
99 | + |
|
100 | + static::$initialized = true; |
|
101 | + } |
|
102 | + |
|
103 | + //-------------------------------------------------------------------- |
|
104 | + |
|
105 | + /** |
|
106 | + * Grabs an individual |
|
107 | + * |
|
108 | + * @param $index |
|
109 | + * @return null |
|
110 | + */ |
|
111 | + public static function segment($index) |
|
112 | + { |
|
113 | + if (! isset(static::$segments[$index - 1])) |
|
114 | + { |
|
115 | + return null; |
|
116 | + } |
|
117 | + |
|
118 | + return static::$segments[$index - 1]; |
|
119 | + } |
|
120 | + |
|
121 | + //-------------------------------------------------------------------- |
|
122 | + |
|
123 | + /** |
|
124 | + * Returns the command string portion of the arguments. Used |
|
125 | + * by the 'sprint' CLI script to grab the portion of the arguments |
|
126 | + * that is used to call the CodeIgniter application. |
|
127 | + * |
|
128 | + * @return string |
|
129 | + */ |
|
130 | + public static function cli_string() |
|
131 | + { |
|
132 | + return implode(' ', static::$segments); |
|
133 | + } |
|
134 | + |
|
135 | + //-------------------------------------------------------------------- |
|
136 | + |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Get input from the shell, using readline or the standard STDIN |
|
141 | + * |
|
142 | + * Named options must be in the following formats: |
|
143 | + * php index.php user -v --v -name=John --name=John |
|
144 | + * |
|
145 | + * @param string|int $name the name of the option (int if unnamed) |
|
146 | + * @return string |
|
147 | + */ |
|
148 | + public static function input($prefix = '') |
|
149 | + { |
|
150 | + if (static::$readline_support) |
|
151 | + { |
|
152 | + return readline($prefix); |
|
153 | + } |
|
154 | + |
|
155 | + echo $prefix; |
|
156 | + return fgets(STDIN); |
|
157 | + } |
|
158 | + |
|
159 | + //-------------------------------------------------------------------- |
|
160 | + |
|
161 | + /** |
|
162 | + * Asks the user for input. This can have either 1 or 2 arguments. |
|
163 | + * |
|
164 | + * Usage: |
|
165 | + * |
|
166 | + * // Waits for any key press |
|
167 | + * CLI::prompt(); |
|
168 | + * |
|
169 | + * // Takes any input |
|
170 | + * $color = CLI::prompt('What is your favorite color?'); |
|
171 | + * |
|
172 | + * // Takes any input, but offers default |
|
173 | + * $color = CLI::prompt('What is your favourite color?', 'white'); |
|
174 | + * |
|
175 | + * // Will only accept the options in the array |
|
176 | + * $ready = CLI::prompt('Are you ready?', array('y','n')); |
|
177 | + * |
|
178 | + * @return string the user input |
|
179 | + */ |
|
180 | + public static function prompt() |
|
181 | + { |
|
182 | + $args = func_get_args(); |
|
183 | + |
|
184 | + $options = array(); |
|
185 | + $output = ''; |
|
186 | + $default = null; |
|
187 | + |
|
188 | + // How many we got |
|
189 | + $arg_count = count($args); |
|
190 | + |
|
191 | + // Is the last argument a boolean? True means required |
|
192 | + $required = end($args) === true; |
|
193 | + |
|
194 | + // Reduce the argument count if required was passed, we don't care about that anymore |
|
195 | + $required === true and --$arg_count; |
|
196 | + |
|
197 | + // This method can take a few crazy combinations of arguments, so lets work it out |
|
198 | + switch ($arg_count) |
|
199 | + { |
|
200 | + case 2: |
|
201 | + |
|
202 | + // E.g: $ready = CLI::prompt('Are you ready?', array('y','n')); |
|
203 | + if (is_array($args[1])) |
|
204 | + { |
|
205 | + list($output, $options)=$args; |
|
206 | + } |
|
207 | + |
|
208 | + // E.g: $color = CLI::prompt('What is your favourite color?', 'white'); |
|
209 | + elseif (is_string($args[1])) |
|
210 | + { |
|
211 | + list($output, $default)=$args; |
|
212 | + } |
|
213 | + |
|
214 | + break; |
|
215 | + |
|
216 | + case 1: |
|
217 | + |
|
218 | + // No question (probably been asked already) so just show options |
|
219 | + // E.g: $ready = CLI::prompt(array('y','n')); |
|
220 | + if (is_array($args[0])) |
|
221 | + { |
|
222 | + $options = $args[0]; |
|
223 | + } |
|
224 | + |
|
225 | + // Question without options |
|
226 | + // E.g: $ready = CLI::prompt('What did you do today?'); |
|
227 | + elseif (is_string($args[0])) |
|
228 | + { |
|
229 | + $output = $args[0]; |
|
230 | + } |
|
231 | + |
|
232 | + break; |
|
233 | + } |
|
234 | + |
|
235 | + // If a question has been asked with the read |
|
236 | + if ($output !== '') |
|
237 | + { |
|
238 | + $extra_output = ''; |
|
239 | + |
|
240 | + if ($default !== null) |
|
241 | + { |
|
242 | + $extra_output = ' [ Default: "'.$default.'" ]'; |
|
243 | + } |
|
244 | + |
|
245 | + elseif ($options !== array()) |
|
246 | + { |
|
247 | + $extra_output = ' [ '.implode(', ', $options).' ]'; |
|
248 | + } |
|
249 | + |
|
250 | + fwrite(STDOUT, $output.$extra_output.': '); |
|
251 | + } |
|
252 | + |
|
253 | + // Read the input from keyboard. |
|
254 | + $input = trim(static::input()) ?: $default; |
|
255 | + |
|
256 | + // No input provided and we require one (default will stop this being called) |
|
257 | + if (empty($input) and $required === true) |
|
258 | + { |
|
259 | + static::write('This is required.'); |
|
260 | + static::new_line(); |
|
261 | + |
|
262 | + $input = forward_static_call_array(array(__CLASS__, 'prompt'), $args); |
|
263 | + } |
|
264 | + |
|
265 | + // If options are provided and the choice is not in the array, tell them to try again |
|
266 | + if ( ! empty($options) and ! in_array($input, $options)) |
|
267 | + { |
|
268 | + static::write('This is not a valid option. Please try again.'); |
|
269 | + static::new_line(); |
|
270 | + |
|
271 | + $input = forward_static_call_array(array(__CLASS__, 'prompt'), $args); |
|
272 | + } |
|
273 | + |
|
274 | + return $input; |
|
275 | + } |
|
276 | + |
|
277 | + //-------------------------------------------------------------------- |
|
278 | + |
|
279 | + /** |
|
280 | + * Outputs a string to the cli. If you send an array it will implode them |
|
281 | + * with a line break. |
|
282 | + * |
|
283 | + * @param string|array $text the text to output, or array of lines |
|
284 | + */ |
|
285 | + public static function write($text = '', $foreground = null, $background = null) |
|
286 | + { |
|
287 | + if (is_array($text)) |
|
288 | + { |
|
289 | + $text = implode(PHP_EOL, $text); |
|
290 | + } |
|
291 | + |
|
292 | + if ($foreground or $background) |
|
293 | + { |
|
294 | + $text = static::color($text, $foreground, $background); |
|
295 | + } |
|
296 | + |
|
297 | + fwrite(STDOUT, $text.PHP_EOL); |
|
298 | + } |
|
299 | + |
|
300 | + //-------------------------------------------------------------------- |
|
301 | + |
|
302 | + /** |
|
303 | + * Outputs an error to the CLI using STDERR instead of STDOUT |
|
304 | + * |
|
305 | + * @param string|array $text the text to output, or array of errors |
|
306 | + */ |
|
307 | + public static function error($text = '', $foreground = 'light_red', $background = null) |
|
308 | + { |
|
309 | + if (is_array($text)) |
|
310 | + { |
|
311 | + $text = implode(PHP_EOL, $text); |
|
312 | + } |
|
313 | + |
|
314 | + if ($foreground OR $background) |
|
315 | + { |
|
316 | + $text = static::color($text, $foreground, $background); |
|
317 | + } |
|
318 | + |
|
319 | + fwrite(STDERR, $text.PHP_EOL); |
|
320 | + } |
|
321 | + |
|
322 | + //-------------------------------------------------------------------- |
|
323 | + |
|
324 | + /** |
|
325 | + * Beeps a certain number of times. |
|
326 | + * |
|
327 | + * @param int $num the number of times to beep |
|
328 | + */ |
|
329 | + public static function beep($num = 1) |
|
330 | + { |
|
331 | + echo str_repeat("\x07", $num); |
|
332 | + } |
|
333 | + |
|
334 | + //-------------------------------------------------------------------- |
|
335 | + |
|
336 | + /** |
|
337 | + * Waits a certain number of seconds, optionally showing a wait message and |
|
338 | + * waiting for a key press. |
|
339 | + * |
|
340 | + * @param int $seconds number of seconds |
|
341 | + * @param bool $countdown show a countdown or not |
|
342 | + */ |
|
343 | + public static function wait($seconds = 0, $countdown = false) |
|
344 | + { |
|
345 | + if ($countdown === true) |
|
346 | + { |
|
347 | + $time = $seconds; |
|
348 | + |
|
349 | + while ($time > 0) |
|
350 | + { |
|
351 | + fwrite(STDOUT, $time.'... '); |
|
352 | + sleep(1); |
|
353 | + $time--; |
|
354 | + } |
|
355 | + static::write(); |
|
356 | + } |
|
357 | + |
|
358 | + else |
|
359 | + { |
|
360 | + if ($seconds > 0) |
|
361 | + { |
|
362 | + sleep($seconds); |
|
363 | + } |
|
364 | + else |
|
365 | + { |
|
366 | + static::write(static::$wait_msg); |
|
367 | + static::input(); |
|
368 | + } |
|
369 | + } |
|
370 | + } |
|
371 | + |
|
372 | + |
|
373 | + //-------------------------------------------------------------------- |
|
374 | + |
|
375 | + /** |
|
376 | + * if operating system === windows |
|
377 | + */ |
|
378 | + public static function is_windows() |
|
379 | + { |
|
380 | + return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
|
381 | + } |
|
382 | + |
|
383 | + //-------------------------------------------------------------------- |
|
384 | + |
|
385 | + /** |
|
386 | + * Enter a number of empty lines |
|
387 | + * |
|
388 | + * @param integer Number of lines to output |
|
389 | + * @return void |
|
390 | + */ |
|
391 | + public static function new_line($num = 1) |
|
392 | + { |
|
393 | + // Do it once or more, write with empty string gives us a new line |
|
394 | + for($i = 0; $i < $num; $i++) |
|
395 | + { |
|
396 | + static::write(); |
|
397 | + } |
|
398 | + } |
|
399 | + |
|
400 | + //-------------------------------------------------------------------- |
|
401 | + |
|
402 | + /** |
|
403 | + * Clears the screen of output |
|
404 | + * |
|
405 | + * @return void |
|
406 | + */ |
|
407 | + public static function clear_screen() |
|
408 | + { |
|
409 | + static::is_windows() |
|
410 | + |
|
411 | + // Windows is a bit crap at this, but their terminal is tiny so shove this in |
|
412 | + ? static::new_line(40) |
|
413 | + |
|
414 | + // Anything with a flair of Unix will handle these magic characters |
|
415 | + : fwrite(STDOUT, chr(27)."[H".chr(27)."[2J"); |
|
416 | + } |
|
417 | + |
|
418 | + //-------------------------------------------------------------------- |
|
419 | + |
|
420 | + /** |
|
421 | + * Returns the given text with the correct color codes for a foreground and |
|
422 | + * optionally a background color. |
|
423 | + * |
|
424 | + * @param string $text the text to color |
|
425 | + * @param string $foreground the foreground color |
|
426 | + * @param string $background the background color |
|
427 | + * @param string $format other formatting to apply. Currently only 'underline' is understood |
|
428 | + * @return string the color coded string |
|
429 | + */ |
|
430 | + public static function color($text, $foreground, $background = null, $format=null) |
|
431 | + { |
|
432 | + if (static::is_windows() and ! isset($_SERVER['ANSICON'])) |
|
433 | + { |
|
434 | + return $text; |
|
435 | + } |
|
436 | + |
|
437 | + if ( ! array_key_exists($foreground, static::$foreground_colors)) |
|
438 | + { |
|
439 | + throw new \RuntimeException('Invalid CLI foreground color: '.$foreground); |
|
440 | + } |
|
441 | + |
|
442 | + if ( $background !== null and ! array_key_exists($background, static::$background_colors)) |
|
443 | + { |
|
444 | + throw new \RuntimeException('Invalid CLI background color: '.$background); |
|
445 | + } |
|
446 | + |
|
447 | + $string = "\033[".static::$foreground_colors[$foreground]."m"; |
|
448 | + |
|
449 | + if ($background !== null) |
|
450 | + { |
|
451 | + $string .= "\033[".static::$background_colors[$background]."m"; |
|
452 | + } |
|
453 | + |
|
454 | + if ($format === 'underline') |
|
455 | + { |
|
456 | + $string .= "\033[4m"; |
|
457 | + } |
|
458 | + |
|
459 | + $string .= $text."\033[0m"; |
|
460 | + |
|
461 | + return $string; |
|
462 | + } |
|
463 | + |
|
464 | + //-------------------------------------------------------------------- |
|
465 | + |
|
466 | + public static function getWidth($default=80) |
|
467 | + { |
|
468 | + if (static::is_windows()) |
|
469 | + { |
|
470 | + return $default; |
|
471 | + } |
|
472 | + |
|
473 | + return (int)shell_exec('tput cols'); |
|
474 | + } |
|
475 | 475 | |
476 | - //-------------------------------------------------------------------- |
|
477 | - |
|
478 | - public static function getHeight($default=32) |
|
479 | - { |
|
480 | - if (static::is_windows()) |
|
481 | - { |
|
482 | - return $default; |
|
483 | - } |
|
484 | - |
|
485 | - return (int)shell_exec('tput lines'); |
|
486 | - } |
|
487 | - |
|
488 | - //-------------------------------------------------------------------- |
|
489 | - |
|
490 | - /** |
|
491 | - * Displays a progress bar on the CLI. You must call it repeatedly |
|
492 | - * to update it. Set $thisStep = false to erase the progress bar. |
|
493 | - * |
|
494 | - * @param int $thisStep |
|
495 | - * @param int $totalSteps |
|
496 | - */ |
|
497 | - public static function showProgress($thisStep=1, $totalSteps=10) |
|
498 | - { |
|
499 | - // The first time through, save |
|
500 | - // our position so the script knows where to go |
|
501 | - // back to when writing the bar, and |
|
502 | - // at the end of the script. |
|
503 | - if (! static::$inProgress) { |
|
504 | - fwrite(STDOUT, "\0337"); |
|
505 | - static::$inProgress = true; |
|
506 | - } |
|
507 | - |
|
508 | - // Restore position |
|
509 | - fwrite(STDERR, "\0338"); |
|
510 | - |
|
511 | - if ($thisStep !== false) { |
|
512 | - // Don't allow div by zero or negative numbers.... |
|
513 | - $thisStep = abs($thisStep); |
|
514 | - $totalSteps = $totalSteps < 1 ? 1 : $totalSteps; |
|
515 | - |
|
516 | - $percent = intval( ($thisStep / $totalSteps) * 100 ); |
|
517 | - $step = (int)round($percent / 10); |
|
518 | - |
|
519 | - // Write the progress bar |
|
520 | - fwrite(STDOUT, "[\033[32m" . str_repeat('#', $step) . str_repeat('.', 10 - $step) . "\033[0m]"); |
|
521 | - // Textual representation... |
|
522 | - fwrite(STDOUT, " {$percent}% Complete" . PHP_EOL); |
|
523 | - // Move up, undo the PHP_EOL |
|
524 | - fwrite(STDOUT, "\033[1A"); |
|
525 | - } |
|
526 | - else |
|
527 | - { |
|
528 | - fwrite(STDERR, "\007"); |
|
529 | - } |
|
530 | - } |
|
531 | - |
|
532 | - //-------------------------------------------------------------------- |
|
476 | + //-------------------------------------------------------------------- |
|
477 | + |
|
478 | + public static function getHeight($default=32) |
|
479 | + { |
|
480 | + if (static::is_windows()) |
|
481 | + { |
|
482 | + return $default; |
|
483 | + } |
|
484 | + |
|
485 | + return (int)shell_exec('tput lines'); |
|
486 | + } |
|
487 | + |
|
488 | + //-------------------------------------------------------------------- |
|
489 | + |
|
490 | + /** |
|
491 | + * Displays a progress bar on the CLI. You must call it repeatedly |
|
492 | + * to update it. Set $thisStep = false to erase the progress bar. |
|
493 | + * |
|
494 | + * @param int $thisStep |
|
495 | + * @param int $totalSteps |
|
496 | + */ |
|
497 | + public static function showProgress($thisStep=1, $totalSteps=10) |
|
498 | + { |
|
499 | + // The first time through, save |
|
500 | + // our position so the script knows where to go |
|
501 | + // back to when writing the bar, and |
|
502 | + // at the end of the script. |
|
503 | + if (! static::$inProgress) { |
|
504 | + fwrite(STDOUT, "\0337"); |
|
505 | + static::$inProgress = true; |
|
506 | + } |
|
507 | + |
|
508 | + // Restore position |
|
509 | + fwrite(STDERR, "\0338"); |
|
510 | + |
|
511 | + if ($thisStep !== false) { |
|
512 | + // Don't allow div by zero or negative numbers.... |
|
513 | + $thisStep = abs($thisStep); |
|
514 | + $totalSteps = $totalSteps < 1 ? 1 : $totalSteps; |
|
515 | + |
|
516 | + $percent = intval( ($thisStep / $totalSteps) * 100 ); |
|
517 | + $step = (int)round($percent / 10); |
|
518 | + |
|
519 | + // Write the progress bar |
|
520 | + fwrite(STDOUT, "[\033[32m" . str_repeat('#', $step) . str_repeat('.', 10 - $step) . "\033[0m]"); |
|
521 | + // Textual representation... |
|
522 | + fwrite(STDOUT, " {$percent}% Complete" . PHP_EOL); |
|
523 | + // Move up, undo the PHP_EOL |
|
524 | + fwrite(STDOUT, "\033[1A"); |
|
525 | + } |
|
526 | + else |
|
527 | + { |
|
528 | + fwrite(STDERR, "\007"); |
|
529 | + } |
|
530 | + } |
|
531 | + |
|
532 | + //-------------------------------------------------------------------- |
|
533 | 533 | |
534 | 534 | /** |
535 | 535 | * Checks to see if an option was passed to us on the CLI and returns |
@@ -541,11 +541,11 @@ discard block |
||
541 | 541 | */ |
542 | 542 | public static function option($name) |
543 | 543 | { |
544 | - if (array_key_exists($name, self::$options)) |
|
545 | - { |
|
546 | - $val = self::$options[$name] === null ? true : self::$options[$name]; |
|
547 | - return $val; |
|
548 | - } |
|
544 | + if (array_key_exists($name, self::$options)) |
|
545 | + { |
|
546 | + $val = self::$options[$name] === null ? true : self::$options[$name]; |
|
547 | + return $val; |
|
548 | + } |
|
549 | 549 | |
550 | 550 | return null; |
551 | 551 | } |
@@ -559,7 +559,7 @@ discard block |
||
559 | 559 | */ |
560 | 560 | public static function getOptions() |
561 | 561 | { |
562 | - return self::$options; |
|
562 | + return self::$options; |
|
563 | 563 | } |
564 | 564 | |
565 | 565 | //-------------------------------------------------------------------- |
@@ -615,10 +615,10 @@ discard block |
||
615 | 615 | return ''; |
616 | 616 | } |
617 | 617 | |
618 | - if ($max == 0) |
|
619 | - { |
|
620 | - $max = CLI::getWidth(); |
|
621 | - } |
|
618 | + if ($max == 0) |
|
619 | + { |
|
620 | + $max = CLI::getWidth(); |
|
621 | + } |
|
622 | 622 | |
623 | 623 | if (CLI::getWidth() < $max) |
624 | 624 | { |
@@ -36,225 +36,225 @@ discard block |
||
36 | 36 | class Auth extends \Myth\Controllers\ThemedController |
37 | 37 | { |
38 | 38 | |
39 | - public function __construct() |
|
40 | - { |
|
41 | - parent::__construct(); |
|
42 | - |
|
43 | - $this->config->load('auth'); |
|
44 | - $this->lang->load('auth'); |
|
45 | - $this->load->library('session'); |
|
46 | - } |
|
47 | - |
|
48 | - //-------------------------------------------------------------------- |
|
49 | - |
|
50 | - public function login() |
|
51 | - { |
|
52 | - $this->load->helper('form'); |
|
53 | - |
|
54 | - $auth = new LocalAuthentication(); |
|
55 | - $this->load->model('user_model'); |
|
56 | - $auth->useModel($this->user_model); |
|
57 | - |
|
58 | - $redirect_url = $this->session->userdata('redirect_url'); |
|
59 | - |
|
60 | - // No need to login again if they are already logged in... |
|
61 | - if ($auth->isLoggedIn()) |
|
62 | - { |
|
63 | - unset($_SESSION['redirect_url']); |
|
64 | - redirect($redirect_url); |
|
65 | - } |
|
66 | - |
|
67 | - if ($this->input->post()) |
|
68 | - { |
|
69 | - $post_data = [ |
|
70 | - 'email' => $this->input->post('email'), |
|
71 | - 'password' => $this->input->post('password') |
|
72 | - ]; |
|
73 | - |
|
74 | - $remember = (bool)$this->input->post('remember'); |
|
75 | - |
|
76 | - if ($auth->login($post_data, $remember)) |
|
77 | - { |
|
78 | - // Is the user being forced to reset their password? |
|
79 | - if ($auth->user()['force_pass_reset'] == 1) |
|
80 | - { |
|
81 | - redirect( Route::named('change_pass') ); |
|
82 | - } |
|
83 | - |
|
84 | - unset($_SESSION['redirect_url']); |
|
85 | - $this->setMessage(lang('auth.did_login'), 'success'); |
|
86 | - redirect($redirect_url); |
|
87 | - } |
|
88 | - |
|
89 | - $this->setMessage($auth->error(), 'danger'); |
|
90 | - } |
|
91 | - |
|
92 | - $this->themer->setLayout('login'); |
|
93 | - $this->render(); |
|
94 | - } |
|
95 | - |
|
96 | - //-------------------------------------------------------------------- |
|
97 | - |
|
98 | - public function logout() |
|
99 | - { |
|
100 | - $auth = new LocalAuthentication(); |
|
101 | - $this->load->model('user_model'); |
|
102 | - $auth->useModel($this->user_model); |
|
103 | - |
|
104 | - if ($auth->isLoggedIn()) |
|
105 | - { |
|
106 | - $auth->logout(); |
|
107 | - |
|
108 | - $this->setMessage(lang('auth.did_logout'), 'success'); |
|
109 | - } |
|
110 | - |
|
111 | - redirect('/'); |
|
112 | - } |
|
113 | - |
|
114 | - //-------------------------------------------------------------------- |
|
115 | - |
|
116 | - public function register() |
|
117 | - { |
|
118 | - $this->load->helper('form'); |
|
119 | - |
|
120 | - if ($this->input->post()) |
|
121 | - { |
|
122 | - $auth = new LocalAuthentication(); |
|
123 | - $this->load->model('user_model'); |
|
124 | - $auth->useModel($this->user_model); |
|
125 | - |
|
126 | - $post_data = [ |
|
127 | - 'first_name' => $this->input->post('first_name'), |
|
128 | - 'last_name' => $this->input->post('last_name'), |
|
129 | - 'email' => $this->input->post('email'), |
|
130 | - 'username' => $this->input->post('username'), |
|
131 | - 'password' => $this->input->post('password'), |
|
132 | - 'pass_confirm' => $this->input->post('pass_confirm') |
|
133 | - ]; |
|
134 | - |
|
135 | - if ($auth->registerUser($post_data)) |
|
136 | - { |
|
137 | - $this->setMessage(lang('auth.did_register'), 'success'); |
|
138 | - redirect( Route::named('login') ); |
|
139 | - } |
|
140 | - else |
|
141 | - { |
|
142 | - $this->setMessage($auth->error(), 'danger'); |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - $this->addScript('register.js'); |
|
147 | - $this->themer->setLayout('login'); |
|
148 | - $this->render(); |
|
149 | - } |
|
150 | - |
|
151 | - //-------------------------------------------------------------------- |
|
152 | - |
|
153 | - public function activate_user() |
|
154 | - { |
|
155 | - $this->load->helper('form'); |
|
156 | - |
|
157 | - if ($this->input->post()) |
|
158 | - { |
|
159 | - $auth = new LocalAuthentication(); |
|
160 | - $this->load->model('user_model'); |
|
161 | - $auth->useModel($this->user_model); |
|
162 | - |
|
163 | - $post_data = [ |
|
164 | - 'email' => $this->input->post('email'), |
|
165 | - 'code' => $this->input->post('code') |
|
166 | - ]; |
|
167 | - |
|
168 | - if ($auth->activateUser($post_data)) |
|
169 | - { |
|
170 | - $this->setMessage(lang('auth.did_activate'), 'success'); |
|
171 | - redirect( Route::named('login') ); |
|
172 | - } |
|
173 | - else |
|
174 | - { |
|
175 | - $this->setMessage($auth->error(), 'danger'); |
|
176 | - } |
|
177 | - } |
|
178 | - |
|
179 | - $data = [ |
|
180 | - 'email' => $this->input->get('e'), |
|
181 | - 'code' => $this->input->get('code') |
|
182 | - ]; |
|
183 | - |
|
184 | - $this->themer->setLayout('login'); |
|
185 | - $this->render($data); |
|
186 | - } |
|
187 | - |
|
188 | - //-------------------------------------------------------------------- |
|
189 | - |
|
190 | - |
|
191 | - public function forgot_password() |
|
192 | - { |
|
193 | - $this->load->helper('form'); |
|
194 | - |
|
195 | - if ($this->input->post()) |
|
196 | - { |
|
197 | - $auth = new LocalAuthentication(); |
|
198 | - $this->load->model('user_model'); |
|
199 | - $auth->useModel($this->user_model); |
|
200 | - |
|
201 | - if ($auth->remindUser($this->input->post('email'))) |
|
202 | - { |
|
203 | - $this->setMessage(lang('auth.send_success'), 'success'); |
|
204 | - redirect( Route::named('reset_pass') ); |
|
205 | - } |
|
206 | - else |
|
207 | - { |
|
208 | - $this->setMessage($auth->error(), 'danger'); |
|
209 | - } |
|
210 | - } |
|
211 | - |
|
212 | - $this->themer->setLayout('login'); |
|
213 | - $this->render(); |
|
214 | - } |
|
215 | - |
|
216 | - //-------------------------------------------------------------------- |
|
217 | - |
|
218 | - public function reset_password() |
|
219 | - { |
|
220 | - $this->load->helper('form'); |
|
221 | - |
|
222 | - if ($this->input->post()) |
|
223 | - { |
|
224 | - $auth = new LocalAuthentication(); |
|
225 | - $this->load->model('user_model'); |
|
226 | - $auth->useModel($this->user_model); |
|
227 | - |
|
228 | - $credentials = [ |
|
229 | - 'email' => $this->input->post('email'), |
|
230 | - 'code' => $this->input->post('code') |
|
231 | - ]; |
|
232 | - |
|
233 | - $password = $this->input->post('password'); |
|
234 | - $pass_confirm = $this->input->post('pass_confirm'); |
|
235 | - |
|
236 | - if ($auth->resetPassword($credentials, $password, $pass_confirm)) |
|
237 | - { |
|
238 | - $this->setMessage(lang('auth.new_password_success'), 'success'); |
|
239 | - redirect( Route::named('login') ); |
|
240 | - } |
|
241 | - else |
|
242 | - { |
|
243 | - $this->setMessage($auth->error(), 'danger'); |
|
244 | - } |
|
245 | - } |
|
246 | - |
|
247 | - $data = [ |
|
248 | - 'email' => $this->input->get('e'), |
|
249 | - 'code' => $this->input->get('code') |
|
250 | - ]; |
|
251 | - |
|
252 | - $this->addScript('register.js'); |
|
253 | - $this->themer->setLayout('login'); |
|
254 | - $this->render($data); |
|
255 | - } |
|
256 | - |
|
257 | - //-------------------------------------------------------------------- |
|
39 | + public function __construct() |
|
40 | + { |
|
41 | + parent::__construct(); |
|
42 | + |
|
43 | + $this->config->load('auth'); |
|
44 | + $this->lang->load('auth'); |
|
45 | + $this->load->library('session'); |
|
46 | + } |
|
47 | + |
|
48 | + //-------------------------------------------------------------------- |
|
49 | + |
|
50 | + public function login() |
|
51 | + { |
|
52 | + $this->load->helper('form'); |
|
53 | + |
|
54 | + $auth = new LocalAuthentication(); |
|
55 | + $this->load->model('user_model'); |
|
56 | + $auth->useModel($this->user_model); |
|
57 | + |
|
58 | + $redirect_url = $this->session->userdata('redirect_url'); |
|
59 | + |
|
60 | + // No need to login again if they are already logged in... |
|
61 | + if ($auth->isLoggedIn()) |
|
62 | + { |
|
63 | + unset($_SESSION['redirect_url']); |
|
64 | + redirect($redirect_url); |
|
65 | + } |
|
66 | + |
|
67 | + if ($this->input->post()) |
|
68 | + { |
|
69 | + $post_data = [ |
|
70 | + 'email' => $this->input->post('email'), |
|
71 | + 'password' => $this->input->post('password') |
|
72 | + ]; |
|
73 | + |
|
74 | + $remember = (bool)$this->input->post('remember'); |
|
75 | + |
|
76 | + if ($auth->login($post_data, $remember)) |
|
77 | + { |
|
78 | + // Is the user being forced to reset their password? |
|
79 | + if ($auth->user()['force_pass_reset'] == 1) |
|
80 | + { |
|
81 | + redirect( Route::named('change_pass') ); |
|
82 | + } |
|
83 | + |
|
84 | + unset($_SESSION['redirect_url']); |
|
85 | + $this->setMessage(lang('auth.did_login'), 'success'); |
|
86 | + redirect($redirect_url); |
|
87 | + } |
|
88 | + |
|
89 | + $this->setMessage($auth->error(), 'danger'); |
|
90 | + } |
|
91 | + |
|
92 | + $this->themer->setLayout('login'); |
|
93 | + $this->render(); |
|
94 | + } |
|
95 | + |
|
96 | + //-------------------------------------------------------------------- |
|
97 | + |
|
98 | + public function logout() |
|
99 | + { |
|
100 | + $auth = new LocalAuthentication(); |
|
101 | + $this->load->model('user_model'); |
|
102 | + $auth->useModel($this->user_model); |
|
103 | + |
|
104 | + if ($auth->isLoggedIn()) |
|
105 | + { |
|
106 | + $auth->logout(); |
|
107 | + |
|
108 | + $this->setMessage(lang('auth.did_logout'), 'success'); |
|
109 | + } |
|
110 | + |
|
111 | + redirect('/'); |
|
112 | + } |
|
113 | + |
|
114 | + //-------------------------------------------------------------------- |
|
115 | + |
|
116 | + public function register() |
|
117 | + { |
|
118 | + $this->load->helper('form'); |
|
119 | + |
|
120 | + if ($this->input->post()) |
|
121 | + { |
|
122 | + $auth = new LocalAuthentication(); |
|
123 | + $this->load->model('user_model'); |
|
124 | + $auth->useModel($this->user_model); |
|
125 | + |
|
126 | + $post_data = [ |
|
127 | + 'first_name' => $this->input->post('first_name'), |
|
128 | + 'last_name' => $this->input->post('last_name'), |
|
129 | + 'email' => $this->input->post('email'), |
|
130 | + 'username' => $this->input->post('username'), |
|
131 | + 'password' => $this->input->post('password'), |
|
132 | + 'pass_confirm' => $this->input->post('pass_confirm') |
|
133 | + ]; |
|
134 | + |
|
135 | + if ($auth->registerUser($post_data)) |
|
136 | + { |
|
137 | + $this->setMessage(lang('auth.did_register'), 'success'); |
|
138 | + redirect( Route::named('login') ); |
|
139 | + } |
|
140 | + else |
|
141 | + { |
|
142 | + $this->setMessage($auth->error(), 'danger'); |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + $this->addScript('register.js'); |
|
147 | + $this->themer->setLayout('login'); |
|
148 | + $this->render(); |
|
149 | + } |
|
150 | + |
|
151 | + //-------------------------------------------------------------------- |
|
152 | + |
|
153 | + public function activate_user() |
|
154 | + { |
|
155 | + $this->load->helper('form'); |
|
156 | + |
|
157 | + if ($this->input->post()) |
|
158 | + { |
|
159 | + $auth = new LocalAuthentication(); |
|
160 | + $this->load->model('user_model'); |
|
161 | + $auth->useModel($this->user_model); |
|
162 | + |
|
163 | + $post_data = [ |
|
164 | + 'email' => $this->input->post('email'), |
|
165 | + 'code' => $this->input->post('code') |
|
166 | + ]; |
|
167 | + |
|
168 | + if ($auth->activateUser($post_data)) |
|
169 | + { |
|
170 | + $this->setMessage(lang('auth.did_activate'), 'success'); |
|
171 | + redirect( Route::named('login') ); |
|
172 | + } |
|
173 | + else |
|
174 | + { |
|
175 | + $this->setMessage($auth->error(), 'danger'); |
|
176 | + } |
|
177 | + } |
|
178 | + |
|
179 | + $data = [ |
|
180 | + 'email' => $this->input->get('e'), |
|
181 | + 'code' => $this->input->get('code') |
|
182 | + ]; |
|
183 | + |
|
184 | + $this->themer->setLayout('login'); |
|
185 | + $this->render($data); |
|
186 | + } |
|
187 | + |
|
188 | + //-------------------------------------------------------------------- |
|
189 | + |
|
190 | + |
|
191 | + public function forgot_password() |
|
192 | + { |
|
193 | + $this->load->helper('form'); |
|
194 | + |
|
195 | + if ($this->input->post()) |
|
196 | + { |
|
197 | + $auth = new LocalAuthentication(); |
|
198 | + $this->load->model('user_model'); |
|
199 | + $auth->useModel($this->user_model); |
|
200 | + |
|
201 | + if ($auth->remindUser($this->input->post('email'))) |
|
202 | + { |
|
203 | + $this->setMessage(lang('auth.send_success'), 'success'); |
|
204 | + redirect( Route::named('reset_pass') ); |
|
205 | + } |
|
206 | + else |
|
207 | + { |
|
208 | + $this->setMessage($auth->error(), 'danger'); |
|
209 | + } |
|
210 | + } |
|
211 | + |
|
212 | + $this->themer->setLayout('login'); |
|
213 | + $this->render(); |
|
214 | + } |
|
215 | + |
|
216 | + //-------------------------------------------------------------------- |
|
217 | + |
|
218 | + public function reset_password() |
|
219 | + { |
|
220 | + $this->load->helper('form'); |
|
221 | + |
|
222 | + if ($this->input->post()) |
|
223 | + { |
|
224 | + $auth = new LocalAuthentication(); |
|
225 | + $this->load->model('user_model'); |
|
226 | + $auth->useModel($this->user_model); |
|
227 | + |
|
228 | + $credentials = [ |
|
229 | + 'email' => $this->input->post('email'), |
|
230 | + 'code' => $this->input->post('code') |
|
231 | + ]; |
|
232 | + |
|
233 | + $password = $this->input->post('password'); |
|
234 | + $pass_confirm = $this->input->post('pass_confirm'); |
|
235 | + |
|
236 | + if ($auth->resetPassword($credentials, $password, $pass_confirm)) |
|
237 | + { |
|
238 | + $this->setMessage(lang('auth.new_password_success'), 'success'); |
|
239 | + redirect( Route::named('login') ); |
|
240 | + } |
|
241 | + else |
|
242 | + { |
|
243 | + $this->setMessage($auth->error(), 'danger'); |
|
244 | + } |
|
245 | + } |
|
246 | + |
|
247 | + $data = [ |
|
248 | + 'email' => $this->input->get('e'), |
|
249 | + 'code' => $this->input->get('code') |
|
250 | + ]; |
|
251 | + |
|
252 | + $this->addScript('register.js'); |
|
253 | + $this->themer->setLayout('login'); |
|
254 | + $this->render($data); |
|
255 | + } |
|
256 | + |
|
257 | + //-------------------------------------------------------------------- |
|
258 | 258 | |
259 | 259 | /** |
260 | 260 | * Allows a logged in user to enter their current password |
@@ -320,24 +320,24 @@ discard block |
||
320 | 320 | //-------------------------------------------------------------------- |
321 | 321 | |
322 | 322 | |
323 | - //-------------------------------------------------------------------- |
|
324 | - // AJAX Methods |
|
325 | - //-------------------------------------------------------------------- |
|
323 | + //-------------------------------------------------------------------- |
|
324 | + // AJAX Methods |
|
325 | + //-------------------------------------------------------------------- |
|
326 | 326 | |
327 | - /** |
|
328 | - * Checks the password strength and returns pass/fail. |
|
329 | - * |
|
330 | - * @param $str |
|
331 | - */ |
|
332 | - public function password_check($str) |
|
333 | - { |
|
334 | - $this->load->helper('auth/password'); |
|
327 | + /** |
|
328 | + * Checks the password strength and returns pass/fail. |
|
329 | + * |
|
330 | + * @param $str |
|
331 | + */ |
|
332 | + public function password_check($str) |
|
333 | + { |
|
334 | + $this->load->helper('auth/password'); |
|
335 | 335 | |
336 | - $strength = isStrongPassword($str); |
|
336 | + $strength = isStrongPassword($str); |
|
337 | 337 | |
338 | - $this->renderJSON(['status' => $strength ? 'pass' : 'fail']); |
|
339 | - } |
|
338 | + $this->renderJSON(['status' => $strength ? 'pass' : 'fail']); |
|
339 | + } |
|
340 | 340 | |
341 | - //-------------------------------------------------------------------- |
|
341 | + //-------------------------------------------------------------------- |
|
342 | 342 | |
343 | 343 | } |
@@ -1,34 +1,34 @@ discard block |
||
1 | 1 | <?php namespace Myth\Events; |
2 | 2 | /** |
3 | - * Sprint |
|
4 | - * |
|
5 | - * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | - * |
|
7 | - * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | - * of this software and associated documentation files (the "Software"), to deal |
|
9 | - * in the Software without restriction, including without limitation the rights |
|
10 | - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | - * copies of the Software, and to permit persons to whom the Software is |
|
12 | - * furnished to do so, subject to the following conditions: |
|
13 | - * |
|
14 | - * The above copyright notice and this permission notice shall be included in |
|
15 | - * all copies or substantial portions of the Software. |
|
16 | - * |
|
17 | - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | - * THE SOFTWARE. |
|
24 | - * |
|
25 | - * @package Sprint |
|
26 | - * @author Lonnie Ezell |
|
27 | - * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | - * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | - * @link http://sprintphp.com |
|
30 | - * @since Version 1.0 |
|
31 | - */ |
|
3 | + * Sprint |
|
4 | + * |
|
5 | + * A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6 | + * |
|
7 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8 | + * of this software and associated documentation files (the "Software"), to deal |
|
9 | + * in the Software without restriction, including without limitation the rights |
|
10 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11 | + * copies of the Software, and to permit persons to whom the Software is |
|
12 | + * furnished to do so, subject to the following conditions: |
|
13 | + * |
|
14 | + * The above copyright notice and this permission notice shall be included in |
|
15 | + * all copies or substantial portions of the Software. |
|
16 | + * |
|
17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23 | + * THE SOFTWARE. |
|
24 | + * |
|
25 | + * @package Sprint |
|
26 | + * @author Lonnie Ezell |
|
27 | + * @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28 | + * @license http://opensource.org/licenses/MIT (MIT) |
|
29 | + * @link http://sprintphp.com |
|
30 | + * @since Version 1.0 |
|
31 | + */ |
|
32 | 32 | |
33 | 33 | define('EVENTS_PRIORITY_LOW', 200); |
34 | 34 | define('EVENTS_PRIORITY_NORMAL', 100); |
@@ -36,176 +36,176 @@ discard block |
||
36 | 36 | |
37 | 37 | class Events { |
38 | 38 | |
39 | - /** |
|
40 | - * The list of listeners. |
|
41 | - * |
|
42 | - * @var array |
|
43 | - */ |
|
44 | - protected static $listeners = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * Flag to let us know if we've read from the config file |
|
48 | - * and have all of the defined events. |
|
49 | - * |
|
50 | - * @var bool |
|
51 | - */ |
|
52 | - protected static $have_read_from_file = false; |
|
53 | - |
|
54 | - //-------------------------------------------------------------------- |
|
55 | - |
|
56 | - /** |
|
57 | - * Registers an action to happen on an event. The action can be any sort |
|
58 | - * of callable: |
|
59 | - * |
|
60 | - * Events::on('create', 'myFunction'); // procedural function |
|
61 | - * Events::on('create', ['myClass', 'myMethod']); // Class::method |
|
62 | - * Events::on('create', [$myInstance, 'myMethod']); // Method on an existing instance |
|
63 | - * Events::on('create', function() {}); // Closure |
|
64 | - * |
|
65 | - * @param $event_name |
|
66 | - * @param callable $callback |
|
67 | - * @param int $priority |
|
68 | - */ |
|
69 | - public static function on($event_name, callable $callback, $priority=EVENTS_PRIORITY_NORMAL) |
|
70 | - { |
|
71 | - if (! isset(self::$listeners[$event_name])) |
|
72 | - { |
|
73 | - self::$listeners[$event_name] = [ |
|
74 | - true, // If there's only 1 item, it's sorted. |
|
75 | - [$priority], |
|
76 | - [$callback] |
|
77 | - ]; |
|
78 | - } |
|
79 | - else |
|
80 | - { |
|
81 | - self::$listeners[$event_name][0] = false; // Not sorted |
|
82 | - self::$listeners[$event_name][1][] = $priority; |
|
83 | - self::$listeners[$event_name][2][] = $callback; |
|
84 | - } |
|
85 | - } |
|
39 | + /** |
|
40 | + * The list of listeners. |
|
41 | + * |
|
42 | + * @var array |
|
43 | + */ |
|
44 | + protected static $listeners = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * Flag to let us know if we've read from the config file |
|
48 | + * and have all of the defined events. |
|
49 | + * |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | + protected static $have_read_from_file = false; |
|
53 | + |
|
54 | + //-------------------------------------------------------------------- |
|
55 | + |
|
56 | + /** |
|
57 | + * Registers an action to happen on an event. The action can be any sort |
|
58 | + * of callable: |
|
59 | + * |
|
60 | + * Events::on('create', 'myFunction'); // procedural function |
|
61 | + * Events::on('create', ['myClass', 'myMethod']); // Class::method |
|
62 | + * Events::on('create', [$myInstance, 'myMethod']); // Method on an existing instance |
|
63 | + * Events::on('create', function() {}); // Closure |
|
64 | + * |
|
65 | + * @param $event_name |
|
66 | + * @param callable $callback |
|
67 | + * @param int $priority |
|
68 | + */ |
|
69 | + public static function on($event_name, callable $callback, $priority=EVENTS_PRIORITY_NORMAL) |
|
70 | + { |
|
71 | + if (! isset(self::$listeners[$event_name])) |
|
72 | + { |
|
73 | + self::$listeners[$event_name] = [ |
|
74 | + true, // If there's only 1 item, it's sorted. |
|
75 | + [$priority], |
|
76 | + [$callback] |
|
77 | + ]; |
|
78 | + } |
|
79 | + else |
|
80 | + { |
|
81 | + self::$listeners[$event_name][0] = false; // Not sorted |
|
82 | + self::$listeners[$event_name][1][] = $priority; |
|
83 | + self::$listeners[$event_name][2][] = $callback; |
|
84 | + } |
|
85 | + } |
|
86 | 86 | |
87 | - //-------------------------------------------------------------------- |
|
88 | - |
|
89 | - /** |
|
90 | - * Runs through all subscribed methods running them one at a time, |
|
91 | - * until either: |
|
92 | - * a) All subscribers have finished or |
|
93 | - * b) a method returns false, at which point execution of subscribers stops. |
|
94 | - * |
|
95 | - * @param $event_name |
|
96 | - * @return bool |
|
97 | - */ |
|
98 | - public static function trigger($event_name, array $arguments = []) |
|
99 | - { |
|
100 | - // Read in our config/events file so that we have them all! |
|
101 | - if (! self::$have_read_from_file) |
|
102 | - { |
|
103 | - if (is_file(APPPATH .'config/events.php')) |
|
104 | - { |
|
105 | - include APPPATH .'config/events.php'; |
|
106 | - } |
|
107 | - self::$have_read_from_file = true; |
|
108 | - } |
|
109 | - |
|
110 | - foreach (self::listeners($event_name) as $listener) |
|
111 | - { |
|
112 | - $result = call_user_func_array($listener, $arguments); |
|
113 | - |
|
114 | - if ($result === false) |
|
115 | - { |
|
116 | - return false; |
|
117 | - } |
|
118 | - } |
|
119 | - |
|
120 | - return true; |
|
121 | - } |
|
87 | + //-------------------------------------------------------------------- |
|
88 | + |
|
89 | + /** |
|
90 | + * Runs through all subscribed methods running them one at a time, |
|
91 | + * until either: |
|
92 | + * a) All subscribers have finished or |
|
93 | + * b) a method returns false, at which point execution of subscribers stops. |
|
94 | + * |
|
95 | + * @param $event_name |
|
96 | + * @return bool |
|
97 | + */ |
|
98 | + public static function trigger($event_name, array $arguments = []) |
|
99 | + { |
|
100 | + // Read in our config/events file so that we have them all! |
|
101 | + if (! self::$have_read_from_file) |
|
102 | + { |
|
103 | + if (is_file(APPPATH .'config/events.php')) |
|
104 | + { |
|
105 | + include APPPATH .'config/events.php'; |
|
106 | + } |
|
107 | + self::$have_read_from_file = true; |
|
108 | + } |
|
109 | + |
|
110 | + foreach (self::listeners($event_name) as $listener) |
|
111 | + { |
|
112 | + $result = call_user_func_array($listener, $arguments); |
|
113 | + |
|
114 | + if ($result === false) |
|
115 | + { |
|
116 | + return false; |
|
117 | + } |
|
118 | + } |
|
119 | + |
|
120 | + return true; |
|
121 | + } |
|
122 | 122 | |
123 | - //-------------------------------------------------------------------- |
|
124 | - |
|
125 | - /** |
|
126 | - * Returns an array of listeners for a single event. They are |
|
127 | - * sorted by priority. |
|
128 | - * |
|
129 | - * If the listener could not be found, returns FALSE, or TRUE if |
|
130 | - * it was removed. |
|
131 | - * |
|
132 | - * @param $event_name |
|
133 | - * @return array |
|
134 | - */ |
|
135 | - public static function listeners($event_name) |
|
136 | - { |
|
137 | - if (! isset(self::$listeners[$event_name])) |
|
138 | - { |
|
139 | - return []; |
|
140 | - } |
|
141 | - |
|
142 | - // The list is not sorted |
|
143 | - if (! self::$listeners[$event_name][0]) |
|
144 | - { |
|
145 | - // Sort it! |
|
146 | - array_multisort(self::$listeners[$event_name][1], SORT_NUMERIC, self::$listeners[$event_name][2]); |
|
147 | - |
|
148 | - // Mark it as sorted already! |
|
149 | - self::$listeners[$event_name][0] = true; |
|
150 | - } |
|
151 | - |
|
152 | - return self::$listeners[$event_name][2]; |
|
153 | - } |
|
154 | - |
|
155 | - //-------------------------------------------------------------------- |
|
156 | - |
|
157 | - /** |
|
158 | - * Removes a single listener from an event. |
|
159 | - * |
|
160 | - * If the listener couldn't be found, returns FALSE, else TRUE if |
|
161 | - * it was removed. |
|
162 | - * |
|
163 | - * @param $event_name |
|
164 | - * @param callable $listener |
|
165 | - * @return bool |
|
166 | - */ |
|
167 | - public static function removeListener($event_name, callable $listener) |
|
168 | - { |
|
169 | - if (! isset(self::$listeners[$event_name])) |
|
170 | - { |
|
171 | - return false; |
|
172 | - } |
|
173 | - |
|
174 | - foreach (self::$listeners[$event_name][2] as $index => $check) |
|
175 | - { |
|
176 | - if ($check === $listener) |
|
177 | - { |
|
178 | - unset(self::$listeners[$event_name][1][$index]); |
|
179 | - unset(self::$listeners[$event_name][2][$index]); |
|
180 | - |
|
181 | - return true; |
|
182 | - } |
|
183 | - } |
|
184 | - |
|
185 | - return false; |
|
186 | - } |
|
187 | - |
|
188 | - //-------------------------------------------------------------------- |
|
189 | - |
|
190 | - /** |
|
191 | - * Removes all listeners. |
|
192 | - * |
|
193 | - * If the event_name is specified, only listeners for that event will be |
|
194 | - * removed, otherwise all listeners for all events are removed. |
|
195 | - * |
|
196 | - * @param null $event_name |
|
197 | - */ |
|
198 | - public static function removeAllListeners($event_name=null) |
|
199 | - { |
|
200 | - if (! is_null($event_name)) |
|
201 | - { |
|
202 | - unset(self::$listeners[$event_name]); |
|
203 | - } |
|
204 | - else { |
|
205 | - self::$listeners = []; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - //-------------------------------------------------------------------- |
|
123 | + //-------------------------------------------------------------------- |
|
124 | + |
|
125 | + /** |
|
126 | + * Returns an array of listeners for a single event. They are |
|
127 | + * sorted by priority. |
|
128 | + * |
|
129 | + * If the listener could not be found, returns FALSE, or TRUE if |
|
130 | + * it was removed. |
|
131 | + * |
|
132 | + * @param $event_name |
|
133 | + * @return array |
|
134 | + */ |
|
135 | + public static function listeners($event_name) |
|
136 | + { |
|
137 | + if (! isset(self::$listeners[$event_name])) |
|
138 | + { |
|
139 | + return []; |
|
140 | + } |
|
141 | + |
|
142 | + // The list is not sorted |
|
143 | + if (! self::$listeners[$event_name][0]) |
|
144 | + { |
|
145 | + // Sort it! |
|
146 | + array_multisort(self::$listeners[$event_name][1], SORT_NUMERIC, self::$listeners[$event_name][2]); |
|
147 | + |
|
148 | + // Mark it as sorted already! |
|
149 | + self::$listeners[$event_name][0] = true; |
|
150 | + } |
|
151 | + |
|
152 | + return self::$listeners[$event_name][2]; |
|
153 | + } |
|
154 | + |
|
155 | + //-------------------------------------------------------------------- |
|
156 | + |
|
157 | + /** |
|
158 | + * Removes a single listener from an event. |
|
159 | + * |
|
160 | + * If the listener couldn't be found, returns FALSE, else TRUE if |
|
161 | + * it was removed. |
|
162 | + * |
|
163 | + * @param $event_name |
|
164 | + * @param callable $listener |
|
165 | + * @return bool |
|
166 | + */ |
|
167 | + public static function removeListener($event_name, callable $listener) |
|
168 | + { |
|
169 | + if (! isset(self::$listeners[$event_name])) |
|
170 | + { |
|
171 | + return false; |
|
172 | + } |
|
173 | + |
|
174 | + foreach (self::$listeners[$event_name][2] as $index => $check) |
|
175 | + { |
|
176 | + if ($check === $listener) |
|
177 | + { |
|
178 | + unset(self::$listeners[$event_name][1][$index]); |
|
179 | + unset(self::$listeners[$event_name][2][$index]); |
|
180 | + |
|
181 | + return true; |
|
182 | + } |
|
183 | + } |
|
184 | + |
|
185 | + return false; |
|
186 | + } |
|
187 | + |
|
188 | + //-------------------------------------------------------------------- |
|
189 | + |
|
190 | + /** |
|
191 | + * Removes all listeners. |
|
192 | + * |
|
193 | + * If the event_name is specified, only listeners for that event will be |
|
194 | + * removed, otherwise all listeners for all events are removed. |
|
195 | + * |
|
196 | + * @param null $event_name |
|
197 | + */ |
|
198 | + public static function removeAllListeners($event_name=null) |
|
199 | + { |
|
200 | + if (! is_null($event_name)) |
|
201 | + { |
|
202 | + unset(self::$listeners[$event_name]); |
|
203 | + } |
|
204 | + else { |
|
205 | + self::$listeners = []; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + //-------------------------------------------------------------------- |
|
210 | 210 | |
211 | 211 | } |
@@ -102,1479 +102,1479 @@ |
||
102 | 102 | class CIDbModel |
103 | 103 | { |
104 | 104 | |
105 | - /** |
|
106 | - * The model's default table name. |
|
107 | - * |
|
108 | - * @var string; |
|
109 | - */ |
|
110 | - protected $table_name; |
|
111 | - |
|
112 | - /** |
|
113 | - * The model's default primary key. |
|
114 | - * |
|
115 | - * @var string |
|
116 | - */ |
|
117 | - protected $primary_key = 'id'; |
|
118 | - |
|
119 | - /** |
|
120 | - * The type of date/time field used for created_on and modified_on fields. |
|
121 | - * Valid types are: 'int', 'datetime', 'date' |
|
122 | - * |
|
123 | - * @var string |
|
124 | - * @access protected |
|
125 | - */ |
|
126 | - protected $date_format = 'datetime'; |
|
127 | - |
|
128 | - /* |
|
105 | + /** |
|
106 | + * The model's default table name. |
|
107 | + * |
|
108 | + * @var string; |
|
109 | + */ |
|
110 | + protected $table_name; |
|
111 | + |
|
112 | + /** |
|
113 | + * The model's default primary key. |
|
114 | + * |
|
115 | + * @var string |
|
116 | + */ |
|
117 | + protected $primary_key = 'id'; |
|
118 | + |
|
119 | + /** |
|
120 | + * The type of date/time field used for created_on and modified_on fields. |
|
121 | + * Valid types are: 'int', 'datetime', 'date' |
|
122 | + * |
|
123 | + * @var string |
|
124 | + * @access protected |
|
125 | + */ |
|
126 | + protected $date_format = 'datetime'; |
|
127 | + |
|
128 | + /* |
|
129 | 129 | Var: $log_user |
130 | 130 | If TRUE, will log user id for 'created_by', 'modified_by' and 'deleted_by'. |
131 | 131 | |
132 | 132 | Access: |
133 | 133 | Protected |
134 | 134 | */ |
135 | - protected $log_user = FALSE; |
|
135 | + protected $log_user = FALSE; |
|
136 | 136 | |
137 | 137 | |
138 | 138 | |
139 | - /** |
|
140 | - * Whether or not to auto-fill a 'created_on' field on inserts. |
|
141 | - * |
|
142 | - * @var boolean |
|
143 | - * @access protected |
|
144 | - */ |
|
145 | - protected $set_created = TRUE; |
|
139 | + /** |
|
140 | + * Whether or not to auto-fill a 'created_on' field on inserts. |
|
141 | + * |
|
142 | + * @var boolean |
|
143 | + * @access protected |
|
144 | + */ |
|
145 | + protected $set_created = TRUE; |
|
146 | 146 | |
147 | - /** |
|
148 | - * Field name to use to the created time column in the DB table. |
|
149 | - * |
|
150 | - * @var string |
|
151 | - * @access protected |
|
152 | - */ |
|
153 | - protected $created_field = 'created_on'; |
|
147 | + /** |
|
148 | + * Field name to use to the created time column in the DB table. |
|
149 | + * |
|
150 | + * @var string |
|
151 | + * @access protected |
|
152 | + */ |
|
153 | + protected $created_field = 'created_on'; |
|
154 | 154 | |
155 | - /* |
|
155 | + /* |
|
156 | 156 | Var: $created_by_field |
157 | 157 | Field name to use to the created by column in the DB table. |
158 | 158 | |
159 | 159 | Access: |
160 | 160 | Protected |
161 | 161 | */ |
162 | - protected $created_by_field = 'created_by'; |
|
162 | + protected $created_by_field = 'created_by'; |
|
163 | 163 | |
164 | 164 | |
165 | 165 | |
166 | - /** |
|
167 | - * Whether or not to auto-fill a 'modified_on' field on updates. |
|
168 | - * |
|
169 | - * @var boolean |
|
170 | - * @access protected |
|
171 | - */ |
|
172 | - protected $set_modified = TRUE; |
|
166 | + /** |
|
167 | + * Whether or not to auto-fill a 'modified_on' field on updates. |
|
168 | + * |
|
169 | + * @var boolean |
|
170 | + * @access protected |
|
171 | + */ |
|
172 | + protected $set_modified = TRUE; |
|
173 | 173 | |
174 | - /** |
|
175 | - * Field name to use to the modified time column in the DB table. |
|
176 | - * |
|
177 | - * @var string |
|
178 | - * @access protected |
|
179 | - */ |
|
180 | - protected $modified_field = 'modified_on'; |
|
174 | + /** |
|
175 | + * Field name to use to the modified time column in the DB table. |
|
176 | + * |
|
177 | + * @var string |
|
178 | + * @access protected |
|
179 | + */ |
|
180 | + protected $modified_field = 'modified_on'; |
|
181 | 181 | |
182 | - /* |
|
182 | + /* |
|
183 | 183 | Var: $modified_by_field |
184 | 184 | Field name to use to the modified by column in the DB table. |
185 | 185 | |
186 | 186 | Access: |
187 | 187 | Protected |
188 | 188 | */ |
189 | - protected $modified_by_field = 'modified_by'; |
|
189 | + protected $modified_by_field = 'modified_by'; |
|
190 | 190 | |
191 | 191 | |
192 | - /** |
|
193 | - * Support for soft_deletes. |
|
194 | - */ |
|
195 | - protected $soft_deletes = FALSE; |
|
196 | - protected $soft_delete_key = 'deleted'; |
|
197 | - protected $temp_with_deleted = FALSE; |
|
192 | + /** |
|
193 | + * Support for soft_deletes. |
|
194 | + */ |
|
195 | + protected $soft_deletes = FALSE; |
|
196 | + protected $soft_delete_key = 'deleted'; |
|
197 | + protected $temp_with_deleted = FALSE; |
|
198 | 198 | |
199 | - /* |
|
199 | + /* |
|
200 | 200 | Var: $deleted_by_field |
201 | 201 | Field name to use for the deleted by column in the DB table. |
202 | 202 | |
203 | 203 | Access: |
204 | 204 | Protected |
205 | 205 | */ |
206 | - protected $deleted_by_field = 'deleted_by'; |
|
206 | + protected $deleted_by_field = 'deleted_by'; |
|
207 | 207 | |
208 | 208 | |
209 | 209 | |
210 | - /** |
|
211 | - * Various callbacks available to the class. They are simple lists of |
|
212 | - * method names (methods will be ran on $this). |
|
213 | - */ |
|
214 | - protected $before_insert = array(); |
|
215 | - protected $after_insert = array(); |
|
216 | - protected $before_update = array(); |
|
217 | - protected $after_update = array(); |
|
218 | - protected $before_find = array(); |
|
219 | - protected $after_find = array(); |
|
220 | - protected $before_delete = array(); |
|
221 | - protected $after_delete = array(); |
|
210 | + /** |
|
211 | + * Various callbacks available to the class. They are simple lists of |
|
212 | + * method names (methods will be ran on $this). |
|
213 | + */ |
|
214 | + protected $before_insert = array(); |
|
215 | + protected $after_insert = array(); |
|
216 | + protected $before_update = array(); |
|
217 | + protected $after_update = array(); |
|
218 | + protected $before_find = array(); |
|
219 | + protected $after_find = array(); |
|
220 | + protected $before_delete = array(); |
|
221 | + protected $after_delete = array(); |
|
222 | 222 | |
223 | - protected $callback_parameters = array(); |
|
223 | + protected $callback_parameters = array(); |
|
224 | 224 | |
225 | 225 | |
226 | 226 | |
227 | - /* |
|
227 | + /* |
|
228 | 228 | If TRUE, inserts will return the last_insert_id. However, |
229 | 229 | this can potentially slow down large imports drastically |
230 | 230 | so you can turn it off with the return_insert_id(false) method. |
231 | 231 | */ |
232 | - protected $return_insert_id = true; |
|
233 | - |
|
234 | - /** |
|
235 | - * By default, we return items as objects. You can change this for the |
|
236 | - * entire class by setting this value to 'array' instead of 'object'. |
|
237 | - * Alternatively, you can do it on a per-instance basis using the |
|
238 | - * 'as_array()' and 'as_object()' methods. |
|
239 | - */ |
|
240 | - protected $return_type = 'object'; |
|
241 | - protected $temp_return_type = NULL; |
|
242 | - |
|
243 | - /** |
|
244 | - * Protected, non-modifiable attributes |
|
245 | - */ |
|
246 | - protected $protected_attributes = array(); |
|
247 | - |
|
248 | - |
|
249 | - |
|
250 | - /** |
|
251 | - * An array of validation rules. This needs to be the same format |
|
252 | - * as validation rules passed to the Form_validation library. |
|
253 | - */ |
|
254 | - protected $validation_rules = array(); |
|
255 | - |
|
256 | - /** |
|
257 | - * Optionally skip the validation. Used in conjunction with |
|
258 | - * skip_validation() to skip data validation for any future calls. |
|
259 | - */ |
|
260 | - protected $skip_validation = FALSE; |
|
261 | - |
|
262 | - /** |
|
263 | - * An array of extra rules to add to validation rules during inserts only. |
|
264 | - * Often used for adding 'required' rules to fields on insert, but not udpates. |
|
265 | - * |
|
266 | - * array( 'username' => 'required|strip_tags' ); |
|
267 | - * @var array |
|
268 | - */ |
|
269 | - protected $insert_validate_rules = array(); |
|
270 | - |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * @var Array Columns for the model's database fields |
|
275 | - * |
|
276 | - * This can be set to avoid a database call if using $this->prep_data() |
|
277 | - */ |
|
278 | - protected $fields = array(); |
|
279 | - |
|
280 | - //-------------------------------------------------------------------- |
|
281 | - |
|
282 | - /** |
|
283 | - * Does basic setup. Can pass the database connection into the constructor |
|
284 | - * to use that $db instead of the global CI one. |
|
285 | - * |
|
286 | - * @param object $db // A database/driver instance |
|
287 | - * @param object $form_validation // A form_validation library instance |
|
288 | - */ |
|
289 | - public function __construct($db = null, $form_validation = null) |
|
290 | - { |
|
291 | - // Always protect our attributes |
|
292 | - array_unshift($this->before_insert, 'protect_attributes'); |
|
293 | - array_unshift($this->before_update, 'protect_attributes'); |
|
294 | - |
|
295 | - // Check our auto-set features and make sure they are part of |
|
296 | - // our observer system. |
|
297 | - if ($this->set_created === true) array_unshift($this->before_insert, 'created_on'); |
|
298 | - if ($this->set_modified === true) array_unshift($this->before_update, 'modified_on'); |
|
299 | - |
|
300 | - // Make sure our temp return type is correct. |
|
301 | - $this->temp_return_type = $this->return_type; |
|
302 | - |
|
303 | - // Make sure our database is loaded |
|
304 | - if (!is_null($db)) { |
|
305 | - $this->db = $db; |
|
306 | - } |
|
307 | - else { |
|
308 | - // Auto Init the damn database.... |
|
309 | - $this->load->database(); |
|
310 | - } |
|
311 | - |
|
312 | - // Do we have a form_validation library? |
|
313 | - if (! is_null($form_validation)) { |
|
314 | - $this->form_validation = $form_validation; |
|
315 | - } |
|
316 | - else { |
|
317 | - $this->load->library('form_validation'); |
|
318 | - } |
|
232 | + protected $return_insert_id = true; |
|
233 | + |
|
234 | + /** |
|
235 | + * By default, we return items as objects. You can change this for the |
|
236 | + * entire class by setting this value to 'array' instead of 'object'. |
|
237 | + * Alternatively, you can do it on a per-instance basis using the |
|
238 | + * 'as_array()' and 'as_object()' methods. |
|
239 | + */ |
|
240 | + protected $return_type = 'object'; |
|
241 | + protected $temp_return_type = NULL; |
|
242 | + |
|
243 | + /** |
|
244 | + * Protected, non-modifiable attributes |
|
245 | + */ |
|
246 | + protected $protected_attributes = array(); |
|
247 | + |
|
248 | + |
|
249 | + |
|
250 | + /** |
|
251 | + * An array of validation rules. This needs to be the same format |
|
252 | + * as validation rules passed to the Form_validation library. |
|
253 | + */ |
|
254 | + protected $validation_rules = array(); |
|
255 | + |
|
256 | + /** |
|
257 | + * Optionally skip the validation. Used in conjunction with |
|
258 | + * skip_validation() to skip data validation for any future calls. |
|
259 | + */ |
|
260 | + protected $skip_validation = FALSE; |
|
261 | + |
|
262 | + /** |
|
263 | + * An array of extra rules to add to validation rules during inserts only. |
|
264 | + * Often used for adding 'required' rules to fields on insert, but not udpates. |
|
265 | + * |
|
266 | + * array( 'username' => 'required|strip_tags' ); |
|
267 | + * @var array |
|
268 | + */ |
|
269 | + protected $insert_validate_rules = array(); |
|
270 | + |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * @var Array Columns for the model's database fields |
|
275 | + * |
|
276 | + * This can be set to avoid a database call if using $this->prep_data() |
|
277 | + */ |
|
278 | + protected $fields = array(); |
|
279 | + |
|
280 | + //-------------------------------------------------------------------- |
|
281 | + |
|
282 | + /** |
|
283 | + * Does basic setup. Can pass the database connection into the constructor |
|
284 | + * to use that $db instead of the global CI one. |
|
285 | + * |
|
286 | + * @param object $db // A database/driver instance |
|
287 | + * @param object $form_validation // A form_validation library instance |
|
288 | + */ |
|
289 | + public function __construct($db = null, $form_validation = null) |
|
290 | + { |
|
291 | + // Always protect our attributes |
|
292 | + array_unshift($this->before_insert, 'protect_attributes'); |
|
293 | + array_unshift($this->before_update, 'protect_attributes'); |
|
294 | + |
|
295 | + // Check our auto-set features and make sure they are part of |
|
296 | + // our observer system. |
|
297 | + if ($this->set_created === true) array_unshift($this->before_insert, 'created_on'); |
|
298 | + if ($this->set_modified === true) array_unshift($this->before_update, 'modified_on'); |
|
299 | + |
|
300 | + // Make sure our temp return type is correct. |
|
301 | + $this->temp_return_type = $this->return_type; |
|
302 | + |
|
303 | + // Make sure our database is loaded |
|
304 | + if (!is_null($db)) { |
|
305 | + $this->db = $db; |
|
306 | + } |
|
307 | + else { |
|
308 | + // Auto Init the damn database.... |
|
309 | + $this->load->database(); |
|
310 | + } |
|
311 | + |
|
312 | + // Do we have a form_validation library? |
|
313 | + if (! is_null($form_validation)) { |
|
314 | + $this->form_validation = $form_validation; |
|
315 | + } |
|
316 | + else { |
|
317 | + $this->load->library('form_validation'); |
|
318 | + } |
|
319 | 319 | |
320 | - log_message('debug', 'CIDbModel Class Initialized'); |
|
321 | - } |
|
322 | - |
|
323 | - //-------------------------------------------------------------------- |
|
324 | - |
|
325 | - //-------------------------------------------------------------------- |
|
326 | - // CRUD Methods |
|
327 | - //-------------------------------------------------------------------- |
|
328 | - |
|
329 | - /** |
|
330 | - * A simple way to grab the first result of a search only. |
|
331 | - */ |
|
332 | - public function first() |
|
333 | - { |
|
334 | - $rows = $this->limit(1, 0)->find_all(); |
|
335 | - |
|
336 | - if (is_array($rows) && count($rows)) { |
|
337 | - return $rows[0]; |
|
338 | - } |
|
339 | - |
|
340 | - return $rows; |
|
341 | - } |
|
342 | - |
|
343 | - //-------------------------------------------------------------------- |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * Finds a single record based on it's primary key. Will ignore deleted rows. |
|
348 | - * |
|
349 | - * @param mixed $id The primary_key value of the object to retrieve. |
|
350 | - * @return object |
|
351 | - */ |
|
352 | - public function find($id) |
|
353 | - { |
|
354 | - $this->trigger('before_find', ['id' => $id, 'method' => 'find']); |
|
355 | - |
|
356 | - // Ignore any soft-deleted rows |
|
357 | - if ($this->soft_deletes) { |
|
358 | - // We only need to modify the where statement if |
|
359 | - // temp_with_deleted is false. |
|
360 | - if ($this->temp_with_deleted !== true) { |
|
361 | - $this->db->where($this->soft_delete_key, false); |
|
362 | - } |
|
363 | - |
|
364 | - $this->temp_with_deleted = false; |
|
365 | - } |
|
366 | - |
|
367 | - $this->db->where($this->primary_key, $id); |
|
368 | - $row = $this->db->get($this->table_name); |
|
369 | - $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
370 | - |
|
371 | - if ( ! empty($row)) |
|
372 | - { |
|
373 | - $row = $this->trigger('after_find', ['id' => $id, 'method' => 'find', 'fields' => $row]); |
|
374 | - } |
|
375 | - |
|
376 | - // Reset our return type |
|
377 | - $this->temp_return_type = $this->return_type; |
|
378 | - |
|
379 | - return $row; |
|
380 | - } |
|
381 | - |
|
382 | - //-------------------------------------------------------------------- |
|
383 | - |
|
384 | - /** |
|
385 | - * Fetch a single record based on an arbitrary WHERE call. Can be |
|
386 | - * any valid value to $this->db->where(). Will not pull in deleted rows |
|
387 | - * if using soft deletes. |
|
388 | - * |
|
389 | - * @return object |
|
390 | - */ |
|
391 | - public function find_by() |
|
392 | - { |
|
393 | - $where = func_get_args(); |
|
394 | - $this->_set_where($where); |
|
395 | - |
|
396 | - // Ignore any soft-deleted rows |
|
397 | - if ($this->soft_deletes) { |
|
398 | - // We only need to modify the where statement if |
|
399 | - // temp_with_deleted is false. |
|
400 | - if ($this->temp_with_deleted !== true) { |
|
401 | - $this->db->where($this->soft_delete_key, false); |
|
402 | - } |
|
403 | - |
|
404 | - $this->temp_with_deleted = false; |
|
405 | - } |
|
406 | - |
|
407 | - $this->trigger('before_find', ['method' => 'find_by', 'fields' => $where]); |
|
408 | - |
|
409 | - $row = $this->db->get($this->table_name); |
|
410 | - $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
411 | - |
|
412 | - if ( ! empty($row)) |
|
413 | - { |
|
414 | - $row = $this->trigger('after_find', ['method' => 'find_by', 'fields' => $row]); |
|
415 | - } |
|
416 | - |
|
417 | - // Reset our return type |
|
418 | - $this->temp_return_type = $this->return_type; |
|
419 | - |
|
420 | - return $row; |
|
421 | - } |
|
422 | - |
|
423 | - //-------------------------------------------------------------------- |
|
424 | - |
|
425 | - /** |
|
426 | - * Retrieves a number of items based on an array of primary_values passed in. |
|
427 | - * |
|
428 | - * @param array $values An array of primary key values to find. |
|
429 | - * |
|
430 | - * @return object or FALSE |
|
431 | - */ |
|
432 | - public function find_many($values) |
|
433 | - { |
|
434 | - $this->db->where_in($this->primary_key, $values); |
|
435 | - |
|
436 | - return $this->find_all(); |
|
437 | - } |
|
438 | - |
|
439 | - //-------------------------------------------------------------------- |
|
440 | - |
|
441 | - /** |
|
442 | - * Retrieves a number of items based on an arbitrary WHERE call. Can be |
|
443 | - * any set of parameters valid to $db->where. |
|
444 | - * |
|
445 | - * @return object or FALSE |
|
446 | - */ |
|
447 | - public function find_many_by() |
|
448 | - { |
|
449 | - $where = func_get_args(); |
|
450 | - $this->_set_where($where); |
|
451 | - |
|
452 | - return $this->find_all(); |
|
453 | - } |
|
454 | - |
|
455 | - //-------------------------------------------------------------------- |
|
456 | - |
|
457 | - /** |
|
458 | - * Fetch all of the records in the table. Can be used with scoped calls |
|
459 | - * to restrict the results. |
|
460 | - * |
|
461 | - * @return object or FALSE |
|
462 | - */ |
|
463 | - public function find_all() |
|
464 | - { |
|
465 | - $this->trigger('before_find', ['method' => 'find_all']); |
|
466 | - |
|
467 | - // Ignore any soft-deleted rows |
|
468 | - if ($this->soft_deletes) { |
|
469 | - // We only need to modify the where statement if |
|
470 | - // temp_with_deleted is false. |
|
471 | - if ($this->temp_with_deleted !== true) { |
|
472 | - $this->db->where($this->soft_delete_key, false); |
|
473 | - } |
|
474 | - |
|
475 | - $this->temp_with_deleted = false; |
|
476 | - } |
|
477 | - |
|
478 | - $rows = $this->db->get($this->table_name); |
|
479 | - $rows = $this->temp_return_type == 'array' ? $rows->result_array() : $rows->result($this->temp_return_type); |
|
480 | - |
|
481 | - if (is_array($rows)) { |
|
482 | - foreach ($rows as $key => &$row) { |
|
483 | - $row = $this->trigger('after_find', ['method' => 'find_all', 'fields' => $row] ); |
|
484 | - } |
|
485 | - } |
|
486 | - |
|
487 | - // Reset our return type |
|
488 | - $this->temp_return_type = $this->return_type; |
|
489 | - |
|
490 | - return $rows; |
|
491 | - } |
|
492 | - |
|
493 | - //-------------------------------------------------------------------- |
|
494 | - |
|
495 | - /** |
|
496 | - * Inserts data into the database. |
|
497 | - * |
|
498 | - * @param array $data An array of key/value pairs to insert to database. |
|
499 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
500 | - * @return mixed The primary_key value of the inserted record, or FALSE. |
|
501 | - */ |
|
502 | - public function insert($data, $skip_validation = null) |
|
503 | - { |
|
504 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
505 | - |
|
506 | - if ($skip_validation === FALSE) { |
|
507 | - $data = $this->validate($data, 'insert', $skip_validation); |
|
508 | - } |
|
509 | - |
|
510 | - if ($data !== FALSE) { |
|
511 | - $data = $this->trigger('before_insert', ['method' => 'insert', 'fields' => $data]); |
|
512 | - |
|
513 | - $this->db->insert($this->table_name, $this->prep_data($data) ); |
|
514 | - |
|
515 | - if ($this->return_insert_id) { |
|
516 | - $id = $this->db->insert_id(); |
|
517 | - |
|
518 | - $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method' => 'insert']); |
|
519 | - |
|
520 | - return $id; |
|
521 | - } |
|
522 | - |
|
523 | - return TRUE; |
|
524 | - } else { |
|
525 | - return FALSE; |
|
526 | - } |
|
527 | - } |
|
528 | - |
|
529 | - //-------------------------------------------------------------------- |
|
530 | - |
|
531 | - /** |
|
532 | - * Inserts multiple rows into the database at once. Takes an associative |
|
533 | - * array of value pairs. |
|
534 | - * |
|
535 | - * $data = array( |
|
536 | - * array( |
|
537 | - * 'title' => 'My title' |
|
538 | - * ), |
|
539 | - * array( |
|
540 | - * 'title' => 'My Other Title' |
|
541 | - * ) |
|
542 | - * ); |
|
543 | - * |
|
544 | - * @param array $data An associate array of rows to insert |
|
545 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
546 | - * @return bool |
|
547 | - */ |
|
548 | - public function insert_batch($data, $skip_validation = null) |
|
549 | - { |
|
550 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
551 | - |
|
552 | - if ($skip_validation === FALSE) { |
|
553 | - $data = $this->validate($data, 'insert', $skip_validation); |
|
554 | - } |
|
555 | - |
|
556 | - if ($data !== FALSE) { |
|
557 | - $data['batch'] = true; |
|
558 | - $data = $this->trigger('before_insert', ['method' => 'insert_batch', 'fields' => $data] ); |
|
559 | - unset($data['batch']); |
|
560 | - |
|
561 | - return $this->db->insert_batch($this->table_name, $data); |
|
562 | - } else { |
|
563 | - return FALSE; |
|
564 | - } |
|
565 | - } |
|
566 | - |
|
567 | - //-------------------------------------------------------------------- |
|
568 | - |
|
569 | - /** |
|
570 | - * Performs the SQL standard for a combined DELETE + INSERT, using |
|
571 | - * PRIMARY and UNIQUE keys to determine which row to replace. |
|
572 | - * |
|
573 | - * See CI's documentation for the replace method. We simply wrap |
|
574 | - * our validation and triggers around their method. |
|
575 | - * |
|
576 | - * @param $data |
|
577 | - * @param null $skip_validation |
|
578 | - * @return bool |
|
579 | - */ |
|
580 | - public function replace($data, $skip_validation=null) |
|
581 | - { |
|
582 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
583 | - |
|
584 | - if ($skip_validation === FALSE) { |
|
585 | - $data = $this->validate($data, 'insert', $skip_validation); |
|
586 | - } |
|
587 | - |
|
588 | - if ($data !== FALSE) { |
|
589 | - $this->db->replace($this->table_name, $this->prep_data($data)); |
|
590 | - |
|
591 | - if ($this->return_insert_id) { |
|
592 | - $id = $this->db->insert_id(); |
|
593 | - |
|
594 | - $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method'=>'replace']); |
|
595 | - |
|
596 | - return $id; |
|
597 | - } |
|
598 | - |
|
599 | - return TRUE; |
|
600 | - } else { |
|
601 | - return FALSE; |
|
602 | - } |
|
603 | - } |
|
604 | - |
|
605 | - //-------------------------------------------------------------------- |
|
606 | - |
|
607 | - |
|
608 | - /** |
|
609 | - * Updates an existing record in the database. |
|
610 | - * |
|
611 | - * @param mixed $id The primary_key value of the record to update. |
|
612 | - * @param array $data An array of value pairs to update in the record. |
|
613 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
614 | - * @return bool |
|
615 | - */ |
|
616 | - public function update($id, $data, $skip_validation = null) |
|
617 | - { |
|
618 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
619 | - |
|
620 | - if ($skip_validation === FALSE) { |
|
621 | - $data = $this->validate($data); |
|
622 | - } |
|
623 | - |
|
624 | - // Will be false if it didn't validate. |
|
625 | - if ($data !== FALSE) { |
|
320 | + log_message('debug', 'CIDbModel Class Initialized'); |
|
321 | + } |
|
322 | + |
|
323 | + //-------------------------------------------------------------------- |
|
324 | + |
|
325 | + //-------------------------------------------------------------------- |
|
326 | + // CRUD Methods |
|
327 | + //-------------------------------------------------------------------- |
|
328 | + |
|
329 | + /** |
|
330 | + * A simple way to grab the first result of a search only. |
|
331 | + */ |
|
332 | + public function first() |
|
333 | + { |
|
334 | + $rows = $this->limit(1, 0)->find_all(); |
|
335 | + |
|
336 | + if (is_array($rows) && count($rows)) { |
|
337 | + return $rows[0]; |
|
338 | + } |
|
339 | + |
|
340 | + return $rows; |
|
341 | + } |
|
342 | + |
|
343 | + //-------------------------------------------------------------------- |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * Finds a single record based on it's primary key. Will ignore deleted rows. |
|
348 | + * |
|
349 | + * @param mixed $id The primary_key value of the object to retrieve. |
|
350 | + * @return object |
|
351 | + */ |
|
352 | + public function find($id) |
|
353 | + { |
|
354 | + $this->trigger('before_find', ['id' => $id, 'method' => 'find']); |
|
355 | + |
|
356 | + // Ignore any soft-deleted rows |
|
357 | + if ($this->soft_deletes) { |
|
358 | + // We only need to modify the where statement if |
|
359 | + // temp_with_deleted is false. |
|
360 | + if ($this->temp_with_deleted !== true) { |
|
361 | + $this->db->where($this->soft_delete_key, false); |
|
362 | + } |
|
363 | + |
|
364 | + $this->temp_with_deleted = false; |
|
365 | + } |
|
366 | + |
|
367 | + $this->db->where($this->primary_key, $id); |
|
368 | + $row = $this->db->get($this->table_name); |
|
369 | + $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
370 | + |
|
371 | + if ( ! empty($row)) |
|
372 | + { |
|
373 | + $row = $this->trigger('after_find', ['id' => $id, 'method' => 'find', 'fields' => $row]); |
|
374 | + } |
|
375 | + |
|
376 | + // Reset our return type |
|
377 | + $this->temp_return_type = $this->return_type; |
|
378 | + |
|
379 | + return $row; |
|
380 | + } |
|
381 | + |
|
382 | + //-------------------------------------------------------------------- |
|
383 | + |
|
384 | + /** |
|
385 | + * Fetch a single record based on an arbitrary WHERE call. Can be |
|
386 | + * any valid value to $this->db->where(). Will not pull in deleted rows |
|
387 | + * if using soft deletes. |
|
388 | + * |
|
389 | + * @return object |
|
390 | + */ |
|
391 | + public function find_by() |
|
392 | + { |
|
393 | + $where = func_get_args(); |
|
394 | + $this->_set_where($where); |
|
395 | + |
|
396 | + // Ignore any soft-deleted rows |
|
397 | + if ($this->soft_deletes) { |
|
398 | + // We only need to modify the where statement if |
|
399 | + // temp_with_deleted is false. |
|
400 | + if ($this->temp_with_deleted !== true) { |
|
401 | + $this->db->where($this->soft_delete_key, false); |
|
402 | + } |
|
403 | + |
|
404 | + $this->temp_with_deleted = false; |
|
405 | + } |
|
406 | + |
|
407 | + $this->trigger('before_find', ['method' => 'find_by', 'fields' => $where]); |
|
408 | + |
|
409 | + $row = $this->db->get($this->table_name); |
|
410 | + $row = $this->temp_return_type == 'array' ? $row->row_array() : $row->row(0, $this->temp_return_type); |
|
411 | + |
|
412 | + if ( ! empty($row)) |
|
413 | + { |
|
414 | + $row = $this->trigger('after_find', ['method' => 'find_by', 'fields' => $row]); |
|
415 | + } |
|
416 | + |
|
417 | + // Reset our return type |
|
418 | + $this->temp_return_type = $this->return_type; |
|
419 | + |
|
420 | + return $row; |
|
421 | + } |
|
422 | + |
|
423 | + //-------------------------------------------------------------------- |
|
424 | + |
|
425 | + /** |
|
426 | + * Retrieves a number of items based on an array of primary_values passed in. |
|
427 | + * |
|
428 | + * @param array $values An array of primary key values to find. |
|
429 | + * |
|
430 | + * @return object or FALSE |
|
431 | + */ |
|
432 | + public function find_many($values) |
|
433 | + { |
|
434 | + $this->db->where_in($this->primary_key, $values); |
|
435 | + |
|
436 | + return $this->find_all(); |
|
437 | + } |
|
438 | + |
|
439 | + //-------------------------------------------------------------------- |
|
440 | + |
|
441 | + /** |
|
442 | + * Retrieves a number of items based on an arbitrary WHERE call. Can be |
|
443 | + * any set of parameters valid to $db->where. |
|
444 | + * |
|
445 | + * @return object or FALSE |
|
446 | + */ |
|
447 | + public function find_many_by() |
|
448 | + { |
|
449 | + $where = func_get_args(); |
|
450 | + $this->_set_where($where); |
|
451 | + |
|
452 | + return $this->find_all(); |
|
453 | + } |
|
454 | + |
|
455 | + //-------------------------------------------------------------------- |
|
456 | + |
|
457 | + /** |
|
458 | + * Fetch all of the records in the table. Can be used with scoped calls |
|
459 | + * to restrict the results. |
|
460 | + * |
|
461 | + * @return object or FALSE |
|
462 | + */ |
|
463 | + public function find_all() |
|
464 | + { |
|
465 | + $this->trigger('before_find', ['method' => 'find_all']); |
|
466 | + |
|
467 | + // Ignore any soft-deleted rows |
|
468 | + if ($this->soft_deletes) { |
|
469 | + // We only need to modify the where statement if |
|
470 | + // temp_with_deleted is false. |
|
471 | + if ($this->temp_with_deleted !== true) { |
|
472 | + $this->db->where($this->soft_delete_key, false); |
|
473 | + } |
|
474 | + |
|
475 | + $this->temp_with_deleted = false; |
|
476 | + } |
|
477 | + |
|
478 | + $rows = $this->db->get($this->table_name); |
|
479 | + $rows = $this->temp_return_type == 'array' ? $rows->result_array() : $rows->result($this->temp_return_type); |
|
480 | + |
|
481 | + if (is_array($rows)) { |
|
482 | + foreach ($rows as $key => &$row) { |
|
483 | + $row = $this->trigger('after_find', ['method' => 'find_all', 'fields' => $row] ); |
|
484 | + } |
|
485 | + } |
|
486 | + |
|
487 | + // Reset our return type |
|
488 | + $this->temp_return_type = $this->return_type; |
|
489 | + |
|
490 | + return $rows; |
|
491 | + } |
|
492 | + |
|
493 | + //-------------------------------------------------------------------- |
|
494 | + |
|
495 | + /** |
|
496 | + * Inserts data into the database. |
|
497 | + * |
|
498 | + * @param array $data An array of key/value pairs to insert to database. |
|
499 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
500 | + * @return mixed The primary_key value of the inserted record, or FALSE. |
|
501 | + */ |
|
502 | + public function insert($data, $skip_validation = null) |
|
503 | + { |
|
504 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
505 | + |
|
506 | + if ($skip_validation === FALSE) { |
|
507 | + $data = $this->validate($data, 'insert', $skip_validation); |
|
508 | + } |
|
509 | + |
|
510 | + if ($data !== FALSE) { |
|
511 | + $data = $this->trigger('before_insert', ['method' => 'insert', 'fields' => $data]); |
|
512 | + |
|
513 | + $this->db->insert($this->table_name, $this->prep_data($data) ); |
|
514 | + |
|
515 | + if ($this->return_insert_id) { |
|
516 | + $id = $this->db->insert_id(); |
|
517 | + |
|
518 | + $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method' => 'insert']); |
|
519 | + |
|
520 | + return $id; |
|
521 | + } |
|
522 | + |
|
523 | + return TRUE; |
|
524 | + } else { |
|
525 | + return FALSE; |
|
526 | + } |
|
527 | + } |
|
528 | + |
|
529 | + //-------------------------------------------------------------------- |
|
530 | + |
|
531 | + /** |
|
532 | + * Inserts multiple rows into the database at once. Takes an associative |
|
533 | + * array of value pairs. |
|
534 | + * |
|
535 | + * $data = array( |
|
536 | + * array( |
|
537 | + * 'title' => 'My title' |
|
538 | + * ), |
|
539 | + * array( |
|
540 | + * 'title' => 'My Other Title' |
|
541 | + * ) |
|
542 | + * ); |
|
543 | + * |
|
544 | + * @param array $data An associate array of rows to insert |
|
545 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
546 | + * @return bool |
|
547 | + */ |
|
548 | + public function insert_batch($data, $skip_validation = null) |
|
549 | + { |
|
550 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
551 | + |
|
552 | + if ($skip_validation === FALSE) { |
|
553 | + $data = $this->validate($data, 'insert', $skip_validation); |
|
554 | + } |
|
555 | + |
|
556 | + if ($data !== FALSE) { |
|
557 | + $data['batch'] = true; |
|
558 | + $data = $this->trigger('before_insert', ['method' => 'insert_batch', 'fields' => $data] ); |
|
559 | + unset($data['batch']); |
|
560 | + |
|
561 | + return $this->db->insert_batch($this->table_name, $data); |
|
562 | + } else { |
|
563 | + return FALSE; |
|
564 | + } |
|
565 | + } |
|
566 | + |
|
567 | + //-------------------------------------------------------------------- |
|
568 | + |
|
569 | + /** |
|
570 | + * Performs the SQL standard for a combined DELETE + INSERT, using |
|
571 | + * PRIMARY and UNIQUE keys to determine which row to replace. |
|
572 | + * |
|
573 | + * See CI's documentation for the replace method. We simply wrap |
|
574 | + * our validation and triggers around their method. |
|
575 | + * |
|
576 | + * @param $data |
|
577 | + * @param null $skip_validation |
|
578 | + * @return bool |
|
579 | + */ |
|
580 | + public function replace($data, $skip_validation=null) |
|
581 | + { |
|
582 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
583 | + |
|
584 | + if ($skip_validation === FALSE) { |
|
585 | + $data = $this->validate($data, 'insert', $skip_validation); |
|
586 | + } |
|
587 | + |
|
588 | + if ($data !== FALSE) { |
|
589 | + $this->db->replace($this->table_name, $this->prep_data($data)); |
|
590 | + |
|
591 | + if ($this->return_insert_id) { |
|
592 | + $id = $this->db->insert_id(); |
|
593 | + |
|
594 | + $this->trigger('after_insert', ['id' => $id, 'fields' => $data, 'method'=>'replace']); |
|
595 | + |
|
596 | + return $id; |
|
597 | + } |
|
598 | + |
|
599 | + return TRUE; |
|
600 | + } else { |
|
601 | + return FALSE; |
|
602 | + } |
|
603 | + } |
|
604 | + |
|
605 | + //-------------------------------------------------------------------- |
|
606 | + |
|
607 | + |
|
608 | + /** |
|
609 | + * Updates an existing record in the database. |
|
610 | + * |
|
611 | + * @param mixed $id The primary_key value of the record to update. |
|
612 | + * @param array $data An array of value pairs to update in the record. |
|
613 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
614 | + * @return bool |
|
615 | + */ |
|
616 | + public function update($id, $data, $skip_validation = null) |
|
617 | + { |
|
618 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
619 | + |
|
620 | + if ($skip_validation === FALSE) { |
|
621 | + $data = $this->validate($data); |
|
622 | + } |
|
623 | + |
|
624 | + // Will be false if it didn't validate. |
|
625 | + if ($data !== FALSE) { |
|
626 | 626 | |
627 | - $data = $this->trigger('before_update', ['id' => $id, 'method' =>'update', 'fields' => $data] ); |
|
627 | + $data = $this->trigger('before_update', ['id' => $id, 'method' =>'update', 'fields' => $data] ); |
|
628 | 628 | |
629 | - $this->db->where($this->primary_key, $id); |
|
630 | - $this->db->set( $this->prep_data($data) ); |
|
631 | - $result = $this->db->update($this->table_name); |
|
632 | - |
|
633 | - $this->trigger('after_update', ['id' => $id, 'fields' => $data, 'result' => $result, 'method' => 'update']); |
|
634 | - |
|
635 | - return $result; |
|
636 | - } else { |
|
637 | - return FALSE; |
|
638 | - } |
|
639 | - } |
|
640 | - |
|
641 | - //-------------------------------------------------------------------- |
|
642 | - |
|
643 | - /** |
|
644 | - * Updates multiple records in the database at once. |
|
645 | - * |
|
646 | - * $data = array( |
|
647 | - * array( |
|
648 | - * 'title' => 'My title', |
|
649 | - * 'body' => 'body 1' |
|
650 | - * ), |
|
651 | - * array( |
|
652 | - * 'title' => 'Another Title', |
|
653 | - * 'body' => 'body 2' |
|
654 | - * ) |
|
655 | - * ); |
|
656 | - * |
|
657 | - * The $where_key should be the name of the column to match the record on. |
|
658 | - * If $where_key == 'title', then each record would be matched on that |
|
659 | - * 'title' value of the array. This does mean that the array key needs |
|
660 | - * to be provided with each row's data. |
|
661 | - * |
|
662 | - * @param array $data An associate array of row data to update. |
|
663 | - * @param string $where_key The column name to match on. |
|
664 | - * @return bool |
|
665 | - */ |
|
666 | - public function update_batch($data, $where_key) |
|
667 | - { |
|
668 | - foreach ($data as &$row) { |
|
669 | - $row = $this->trigger('before_update', ['method' => 'update_batch', 'fields' => $row] ); |
|
670 | - } |
|
671 | - |
|
672 | - $result = $this->db->update_batch($this->table_name, $data, $where_key); |
|
673 | - |
|
674 | - foreach ($data as &$row) { |
|
675 | - $this->trigger('after_update', ['fields' => $data, 'result' => $result, 'method' => 'update_batch']); |
|
676 | - } |
|
677 | - |
|
678 | - return $result; |
|
679 | - } |
|
680 | - |
|
681 | - //-------------------------------------------------------------------- |
|
682 | - |
|
683 | - /** |
|
684 | - * Updates many records by an array of ids. |
|
685 | - * |
|
686 | - * While update_batch() allows modifying multiple, arbitrary rows of data |
|
687 | - * on each row, update_many() sets the same values for each row. |
|
688 | - * |
|
689 | - * $ids = array(1, 2, 3, 5, 12); |
|
690 | - * $data = array( |
|
691 | - * 'deleted_by' => 1 |
|
692 | - * ); |
|
693 | - * |
|
694 | - * $this->model->update_many($ids, $data); |
|
695 | - * |
|
696 | - * @param array $ids An array of primary_key values to update. |
|
697 | - * @param array $data An array of value pairs to modify in each row. |
|
698 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
699 | - * @return bool |
|
700 | - */ |
|
701 | - public function update_many($ids, $data, $skip_validation = null) |
|
702 | - { |
|
703 | - if (!is_array($ids) || count($ids) == 0) return NULL; |
|
704 | - |
|
705 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
706 | - |
|
707 | - if ($skip_validation === FALSE) { |
|
708 | - $data = $this->validate($data, 'update', $skip_validation); |
|
709 | - } |
|
710 | - |
|
711 | - $data = $this->trigger('before_update', ['ids' => $ids, 'method' => 'update_many', 'fields' => $data]); |
|
712 | - |
|
713 | - // Will be false if it didn't validate. |
|
714 | - if ($data !== FALSE) { |
|
715 | - $this->db->where_in($this->primary_key, $ids); |
|
716 | - $this->db->set($data); |
|
717 | - $result = $this->db->update($this->table_name); |
|
718 | - |
|
719 | - $this->trigger('after_update', ['ids' => $ids, 'fields' => $data, 'result'=>$result, 'method' => 'update_many']); |
|
720 | - |
|
721 | - return $result; |
|
722 | - } else { |
|
723 | - return FALSE; |
|
724 | - } |
|
725 | - } |
|
726 | - |
|
727 | - //-------------------------------------------------------------------- |
|
728 | - |
|
729 | - /** |
|
730 | - * Update records in the database using a standard WHERE clause. |
|
731 | - * |
|
732 | - * Your last parameter should be the $data array with values to update |
|
733 | - * on the rows. Any additional parameters should be provided to make up |
|
734 | - * a typical WHERE clause. This could be a single array, or a column name |
|
735 | - * and a value. |
|
736 | - * |
|
737 | - * $data = array('deleted_by' => 1); |
|
738 | - * $wheres = array('user_id' => 15); |
|
739 | - * |
|
740 | - * $this->update_by($wheres, $data); |
|
741 | - * $this->update_by('user_id', 15, $data); |
|
742 | - * |
|
743 | - * @param array $data An array of data pairs to update |
|
744 | - * @param one or more WHERE-acceptable entries. |
|
745 | - * @return bool |
|
746 | - */ |
|
747 | - public function update_by() |
|
748 | - { |
|
749 | - $args = func_get_args(); |
|
750 | - $data = array_pop($args); |
|
751 | - $this->_set_where($args); |
|
752 | - |
|
753 | - $data = $this->trigger('before_update', ['method' => 'update_by', 'fields' => $data]); |
|
754 | - |
|
755 | - // Will be false if it didn't validate. |
|
756 | - if ($this->validate($data) !== FALSE) { |
|
757 | - $this->db->set( $this->prep_data($data) ); |
|
758 | - $result = $this->db->update($this->table_name); |
|
759 | - |
|
760 | - $this->trigger('after_update', ['method' => 'update_by', 'fields' => $data, 'result' => $result] ); |
|
761 | - |
|
762 | - return $result; |
|
763 | - } else { |
|
764 | - return FALSE; |
|
765 | - } |
|
766 | - } |
|
767 | - |
|
768 | - //-------------------------------------------------------------------- |
|
769 | - |
|
770 | - /** |
|
771 | - * Updates all records and sets the value pairs passed in the array. |
|
772 | - * |
|
773 | - * @param array $data An array of value pairs with the data to change. |
|
774 | - * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
775 | - * @return bool |
|
776 | - */ |
|
777 | - public function update_all($data, $skip_validation = FALSE) |
|
778 | - { |
|
779 | - $data = $this->trigger('before_update', ['method' => 'update_all', 'fields' => $data] ); |
|
780 | - |
|
781 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
782 | - |
|
783 | - if ($skip_validation === FALSE) { |
|
784 | - $data = $this->validate($data); |
|
785 | - } |
|
786 | - |
|
787 | - // Will be false if it didn't validate. |
|
788 | - if ($data !== FALSE) { |
|
789 | - $this->db->set( $this->prep_data($data) ); |
|
790 | - $result = $this->db->update($this->table_name); |
|
791 | - |
|
792 | - $this->trigger('after_update', ['method' => 'update_all', 'fields' => $data, 'result' => $result] ); |
|
793 | - |
|
794 | - return $result; |
|
795 | - } else { |
|
796 | - return FALSE; |
|
797 | - } |
|
798 | - } |
|
799 | - |
|
800 | - //-------------------------------------------------------------------- |
|
801 | - |
|
802 | - /** |
|
803 | - * Increments the value of field for a given row, selected by the |
|
804 | - * primary key for the table. |
|
805 | - * |
|
806 | - * @param $id |
|
807 | - * @param $field |
|
808 | - * @param int $value |
|
809 | - * @return mixed |
|
810 | - */ |
|
811 | - public function increment($id, $field, $value=1) |
|
812 | - { |
|
813 | - $value = (int)abs($value); |
|
814 | - |
|
815 | - $this->db->where($this->primary_key, $id); |
|
816 | - $this->db->set($field, "{$field}+{$value}", false); |
|
817 | - |
|
818 | - return $this->db->update($this->table_name); |
|
819 | - } |
|
820 | - |
|
821 | - //-------------------------------------------------------------------- |
|
822 | - |
|
823 | - /** |
|
824 | - * Increments the value of field for a given row, selected by the |
|
825 | - * primary key for the table. |
|
826 | - * |
|
827 | - * @param $id |
|
828 | - * @param $field |
|
829 | - * @param int $value |
|
830 | - * @return mixed |
|
831 | - */ |
|
832 | - public function decrement($id, $field, $value=1) |
|
833 | - { |
|
834 | - $value = (int)abs($value); |
|
835 | - |
|
836 | - $this->db->where($this->primary_key, $id); |
|
837 | - $this->db->set($field, "{$field}-{$value}", false); |
|
838 | - |
|
839 | - return $this->db->update($this->table_name); |
|
840 | - } |
|
841 | - |
|
842 | - //-------------------------------------------------------------------- |
|
843 | - |
|
844 | - /** |
|
845 | - * Deletes a row by it's primary key value. |
|
846 | - * |
|
847 | - * @param mixed $id The primary key value of the row to delete. |
|
848 | - * @return bool |
|
849 | - */ |
|
850 | - public function delete($id) |
|
851 | - { |
|
852 | - $this->trigger('before_delete', ['id' => $id, 'method' => 'delete'] ); |
|
853 | - |
|
854 | - $this->db->where($this->primary_key, $id); |
|
855 | - |
|
856 | - if ($this->soft_deletes) { |
|
857 | - $sets = $this->log_user && is_object($this->authenticate) |
|
858 | - ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
859 | - : array($this->soft_delete_key => 1); |
|
860 | - |
|
861 | - $result = $this->db->update($this->table_name, $sets); |
|
862 | - } // Hard Delete |
|
863 | - else { |
|
864 | - $result = $this->db->delete($this->table_name); |
|
865 | - } |
|
866 | - |
|
867 | - $this->trigger('after_delete', ['id' => $id, 'method' => 'delete', 'result' => $result] ); |
|
629 | + $this->db->where($this->primary_key, $id); |
|
630 | + $this->db->set( $this->prep_data($data) ); |
|
631 | + $result = $this->db->update($this->table_name); |
|
632 | + |
|
633 | + $this->trigger('after_update', ['id' => $id, 'fields' => $data, 'result' => $result, 'method' => 'update']); |
|
634 | + |
|
635 | + return $result; |
|
636 | + } else { |
|
637 | + return FALSE; |
|
638 | + } |
|
639 | + } |
|
640 | + |
|
641 | + //-------------------------------------------------------------------- |
|
642 | + |
|
643 | + /** |
|
644 | + * Updates multiple records in the database at once. |
|
645 | + * |
|
646 | + * $data = array( |
|
647 | + * array( |
|
648 | + * 'title' => 'My title', |
|
649 | + * 'body' => 'body 1' |
|
650 | + * ), |
|
651 | + * array( |
|
652 | + * 'title' => 'Another Title', |
|
653 | + * 'body' => 'body 2' |
|
654 | + * ) |
|
655 | + * ); |
|
656 | + * |
|
657 | + * The $where_key should be the name of the column to match the record on. |
|
658 | + * If $where_key == 'title', then each record would be matched on that |
|
659 | + * 'title' value of the array. This does mean that the array key needs |
|
660 | + * to be provided with each row's data. |
|
661 | + * |
|
662 | + * @param array $data An associate array of row data to update. |
|
663 | + * @param string $where_key The column name to match on. |
|
664 | + * @return bool |
|
665 | + */ |
|
666 | + public function update_batch($data, $where_key) |
|
667 | + { |
|
668 | + foreach ($data as &$row) { |
|
669 | + $row = $this->trigger('before_update', ['method' => 'update_batch', 'fields' => $row] ); |
|
670 | + } |
|
671 | + |
|
672 | + $result = $this->db->update_batch($this->table_name, $data, $where_key); |
|
673 | + |
|
674 | + foreach ($data as &$row) { |
|
675 | + $this->trigger('after_update', ['fields' => $data, 'result' => $result, 'method' => 'update_batch']); |
|
676 | + } |
|
677 | + |
|
678 | + return $result; |
|
679 | + } |
|
680 | + |
|
681 | + //-------------------------------------------------------------------- |
|
682 | + |
|
683 | + /** |
|
684 | + * Updates many records by an array of ids. |
|
685 | + * |
|
686 | + * While update_batch() allows modifying multiple, arbitrary rows of data |
|
687 | + * on each row, update_many() sets the same values for each row. |
|
688 | + * |
|
689 | + * $ids = array(1, 2, 3, 5, 12); |
|
690 | + * $data = array( |
|
691 | + * 'deleted_by' => 1 |
|
692 | + * ); |
|
693 | + * |
|
694 | + * $this->model->update_many($ids, $data); |
|
695 | + * |
|
696 | + * @param array $ids An array of primary_key values to update. |
|
697 | + * @param array $data An array of value pairs to modify in each row. |
|
698 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
699 | + * @return bool |
|
700 | + */ |
|
701 | + public function update_many($ids, $data, $skip_validation = null) |
|
702 | + { |
|
703 | + if (!is_array($ids) || count($ids) == 0) return NULL; |
|
704 | + |
|
705 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
706 | + |
|
707 | + if ($skip_validation === FALSE) { |
|
708 | + $data = $this->validate($data, 'update', $skip_validation); |
|
709 | + } |
|
710 | + |
|
711 | + $data = $this->trigger('before_update', ['ids' => $ids, 'method' => 'update_many', 'fields' => $data]); |
|
712 | + |
|
713 | + // Will be false if it didn't validate. |
|
714 | + if ($data !== FALSE) { |
|
715 | + $this->db->where_in($this->primary_key, $ids); |
|
716 | + $this->db->set($data); |
|
717 | + $result = $this->db->update($this->table_name); |
|
718 | + |
|
719 | + $this->trigger('after_update', ['ids' => $ids, 'fields' => $data, 'result'=>$result, 'method' => 'update_many']); |
|
720 | + |
|
721 | + return $result; |
|
722 | + } else { |
|
723 | + return FALSE; |
|
724 | + } |
|
725 | + } |
|
726 | + |
|
727 | + //-------------------------------------------------------------------- |
|
728 | + |
|
729 | + /** |
|
730 | + * Update records in the database using a standard WHERE clause. |
|
731 | + * |
|
732 | + * Your last parameter should be the $data array with values to update |
|
733 | + * on the rows. Any additional parameters should be provided to make up |
|
734 | + * a typical WHERE clause. This could be a single array, or a column name |
|
735 | + * and a value. |
|
736 | + * |
|
737 | + * $data = array('deleted_by' => 1); |
|
738 | + * $wheres = array('user_id' => 15); |
|
739 | + * |
|
740 | + * $this->update_by($wheres, $data); |
|
741 | + * $this->update_by('user_id', 15, $data); |
|
742 | + * |
|
743 | + * @param array $data An array of data pairs to update |
|
744 | + * @param one or more WHERE-acceptable entries. |
|
745 | + * @return bool |
|
746 | + */ |
|
747 | + public function update_by() |
|
748 | + { |
|
749 | + $args = func_get_args(); |
|
750 | + $data = array_pop($args); |
|
751 | + $this->_set_where($args); |
|
752 | + |
|
753 | + $data = $this->trigger('before_update', ['method' => 'update_by', 'fields' => $data]); |
|
754 | + |
|
755 | + // Will be false if it didn't validate. |
|
756 | + if ($this->validate($data) !== FALSE) { |
|
757 | + $this->db->set( $this->prep_data($data) ); |
|
758 | + $result = $this->db->update($this->table_name); |
|
759 | + |
|
760 | + $this->trigger('after_update', ['method' => 'update_by', 'fields' => $data, 'result' => $result] ); |
|
761 | + |
|
762 | + return $result; |
|
763 | + } else { |
|
764 | + return FALSE; |
|
765 | + } |
|
766 | + } |
|
767 | + |
|
768 | + //-------------------------------------------------------------------- |
|
769 | + |
|
770 | + /** |
|
771 | + * Updates all records and sets the value pairs passed in the array. |
|
772 | + * |
|
773 | + * @param array $data An array of value pairs with the data to change. |
|
774 | + * @param array $skip_validation If TRUE, will skip validation of data for this call only. |
|
775 | + * @return bool |
|
776 | + */ |
|
777 | + public function update_all($data, $skip_validation = FALSE) |
|
778 | + { |
|
779 | + $data = $this->trigger('before_update', ['method' => 'update_all', 'fields' => $data] ); |
|
780 | + |
|
781 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
782 | + |
|
783 | + if ($skip_validation === FALSE) { |
|
784 | + $data = $this->validate($data); |
|
785 | + } |
|
786 | + |
|
787 | + // Will be false if it didn't validate. |
|
788 | + if ($data !== FALSE) { |
|
789 | + $this->db->set( $this->prep_data($data) ); |
|
790 | + $result = $this->db->update($this->table_name); |
|
791 | + |
|
792 | + $this->trigger('after_update', ['method' => 'update_all', 'fields' => $data, 'result' => $result] ); |
|
793 | + |
|
794 | + return $result; |
|
795 | + } else { |
|
796 | + return FALSE; |
|
797 | + } |
|
798 | + } |
|
799 | + |
|
800 | + //-------------------------------------------------------------------- |
|
801 | + |
|
802 | + /** |
|
803 | + * Increments the value of field for a given row, selected by the |
|
804 | + * primary key for the table. |
|
805 | + * |
|
806 | + * @param $id |
|
807 | + * @param $field |
|
808 | + * @param int $value |
|
809 | + * @return mixed |
|
810 | + */ |
|
811 | + public function increment($id, $field, $value=1) |
|
812 | + { |
|
813 | + $value = (int)abs($value); |
|
814 | + |
|
815 | + $this->db->where($this->primary_key, $id); |
|
816 | + $this->db->set($field, "{$field}+{$value}", false); |
|
817 | + |
|
818 | + return $this->db->update($this->table_name); |
|
819 | + } |
|
820 | + |
|
821 | + //-------------------------------------------------------------------- |
|
822 | + |
|
823 | + /** |
|
824 | + * Increments the value of field for a given row, selected by the |
|
825 | + * primary key for the table. |
|
826 | + * |
|
827 | + * @param $id |
|
828 | + * @param $field |
|
829 | + * @param int $value |
|
830 | + * @return mixed |
|
831 | + */ |
|
832 | + public function decrement($id, $field, $value=1) |
|
833 | + { |
|
834 | + $value = (int)abs($value); |
|
835 | + |
|
836 | + $this->db->where($this->primary_key, $id); |
|
837 | + $this->db->set($field, "{$field}-{$value}", false); |
|
838 | + |
|
839 | + return $this->db->update($this->table_name); |
|
840 | + } |
|
841 | + |
|
842 | + //-------------------------------------------------------------------- |
|
843 | + |
|
844 | + /** |
|
845 | + * Deletes a row by it's primary key value. |
|
846 | + * |
|
847 | + * @param mixed $id The primary key value of the row to delete. |
|
848 | + * @return bool |
|
849 | + */ |
|
850 | + public function delete($id) |
|
851 | + { |
|
852 | + $this->trigger('before_delete', ['id' => $id, 'method' => 'delete'] ); |
|
853 | + |
|
854 | + $this->db->where($this->primary_key, $id); |
|
855 | + |
|
856 | + if ($this->soft_deletes) { |
|
857 | + $sets = $this->log_user && is_object($this->authenticate) |
|
858 | + ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
859 | + : array($this->soft_delete_key => 1); |
|
860 | + |
|
861 | + $result = $this->db->update($this->table_name, $sets); |
|
862 | + } // Hard Delete |
|
863 | + else { |
|
864 | + $result = $this->db->delete($this->table_name); |
|
865 | + } |
|
866 | + |
|
867 | + $this->trigger('after_delete', ['id' => $id, 'method' => 'delete', 'result' => $result] ); |
|
868 | + |
|
869 | + return $result; |
|
870 | + } |
|
871 | + |
|
872 | + //-------------------------------------------------------------------- |
|
873 | + |
|
874 | + public function delete_by() |
|
875 | + { |
|
876 | + $where = func_get_args(); |
|
877 | + $this->_set_where($where); |
|
878 | + |
|
879 | + $where = $this->trigger('before_delete', ['method' => 'delete_by', 'fields' => $where]); |
|
880 | + |
|
881 | + if ($this->soft_deletes) { |
|
882 | + $sets = $this->log_user && is_object($this->authenticate) |
|
883 | + ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
884 | + : array($this->soft_delete_key => 1); |
|
885 | + |
|
886 | + $result = $this->db->update($this->table_name, $sets); |
|
887 | + } else { |
|
888 | + $result = $this->db->delete($this->table_name); |
|
889 | + } |
|
890 | + |
|
891 | + $this->trigger('after_delete', ['method' => 'delete_by', 'fields' => $where, 'result' => $result] ); |
|
892 | + |
|
893 | + return $result; |
|
894 | + } |
|
895 | + |
|
896 | + //-------------------------------------------------------------------- |
|
897 | + |
|
898 | + public function delete_many($ids) |
|
899 | + { |
|
900 | + if (!is_array($ids) || count($ids) == 0) return NULL; |
|
901 | + |
|
902 | + $ids = $this->trigger('before_delete', ['ids' => $ids, 'method' => 'delete_many'] ); |
|
903 | + |
|
904 | + $this->db->where_in($this->primary_key, $ids); |
|
905 | + |
|
906 | + if ($this->soft_deletes) { |
|
907 | + $sets = $this->log_user && is_object($this->authenticate) |
|
908 | + ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
909 | + : array($this->soft_delete_key => 1); |
|
910 | + |
|
911 | + $result = $this->db->update($this->table_name, $sets); |
|
912 | + } else { |
|
913 | + $result = $this->db->delete($this->table_name); |
|
914 | + } |
|
915 | + |
|
916 | + $this->trigger('after_delete', ['ids' => $ids, 'method' => 'delete_many', 'result' => $result]); |
|
868 | 917 | |
869 | - return $result; |
|
870 | - } |
|
918 | + return $result; |
|
919 | + } |
|
920 | + |
|
921 | + //-------------------------------------------------------------------- |
|
922 | + |
|
923 | + //-------------------------------------------------------------------- |
|
924 | + // Scope Methods |
|
925 | + //-------------------------------------------------------------------- |
|
926 | + |
|
927 | + /** |
|
928 | + * Sets the value of the soft deletes flag. |
|
929 | + * |
|
930 | + * @param boolean $val If TRUE, should perform a soft delete. If FALSE, a hard delete. |
|
931 | + */ |
|
932 | + public function soft_delete($val = TRUE) |
|
933 | + { |
|
934 | + $this->soft_deletes = $val; |
|
871 | 935 | |
872 | - //-------------------------------------------------------------------- |
|
873 | - |
|
874 | - public function delete_by() |
|
875 | - { |
|
876 | - $where = func_get_args(); |
|
877 | - $this->_set_where($where); |
|
878 | - |
|
879 | - $where = $this->trigger('before_delete', ['method' => 'delete_by', 'fields' => $where]); |
|
880 | - |
|
881 | - if ($this->soft_deletes) { |
|
882 | - $sets = $this->log_user && is_object($this->authenticate) |
|
883 | - ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
884 | - : array($this->soft_delete_key => 1); |
|
885 | - |
|
886 | - $result = $this->db->update($this->table_name, $sets); |
|
887 | - } else { |
|
888 | - $result = $this->db->delete($this->table_name); |
|
889 | - } |
|
890 | - |
|
891 | - $this->trigger('after_delete', ['method' => 'delete_by', 'fields' => $where, 'result' => $result] ); |
|
892 | - |
|
893 | - return $result; |
|
894 | - } |
|
895 | - |
|
896 | - //-------------------------------------------------------------------- |
|
897 | - |
|
898 | - public function delete_many($ids) |
|
899 | - { |
|
900 | - if (!is_array($ids) || count($ids) == 0) return NULL; |
|
901 | - |
|
902 | - $ids = $this->trigger('before_delete', ['ids' => $ids, 'method' => 'delete_many'] ); |
|
903 | - |
|
904 | - $this->db->where_in($this->primary_key, $ids); |
|
905 | - |
|
906 | - if ($this->soft_deletes) { |
|
907 | - $sets = $this->log_user && is_object($this->authenticate) |
|
908 | - ? array($this->soft_delete_key => 1, $this->deleted_by_field => $this->authenticate->id()) |
|
909 | - : array($this->soft_delete_key => 1); |
|
910 | - |
|
911 | - $result = $this->db->update($this->table_name, $sets); |
|
912 | - } else { |
|
913 | - $result = $this->db->delete($this->table_name); |
|
914 | - } |
|
915 | - |
|
916 | - $this->trigger('after_delete', ['ids' => $ids, 'method' => 'delete_many', 'result' => $result]); |
|
917 | - |
|
918 | - return $result; |
|
919 | - } |
|
920 | - |
|
921 | - //-------------------------------------------------------------------- |
|
922 | - |
|
923 | - //-------------------------------------------------------------------- |
|
924 | - // Scope Methods |
|
925 | - //-------------------------------------------------------------------- |
|
926 | - |
|
927 | - /** |
|
928 | - * Sets the value of the soft deletes flag. |
|
929 | - * |
|
930 | - * @param boolean $val If TRUE, should perform a soft delete. If FALSE, a hard delete. |
|
931 | - */ |
|
932 | - public function soft_delete($val = TRUE) |
|
933 | - { |
|
934 | - $this->soft_deletes = $val; |
|
935 | - |
|
936 | - return $this; |
|
937 | - } |
|
938 | - |
|
939 | - //-------------------------------------------------------------------- |
|
940 | - |
|
941 | - /** |
|
942 | - * Temporarily sets our return type to an array. |
|
943 | - */ |
|
944 | - public function as_array() |
|
945 | - { |
|
946 | - $this->temp_return_type = 'array'; |
|
947 | - |
|
948 | - return $this; |
|
949 | - } |
|
950 | - |
|
951 | - //-------------------------------------------------------------------- |
|
952 | - |
|
953 | - /** |
|
954 | - * Temporarily sets our return type to an object. |
|
955 | - * |
|
956 | - * If $class is provided, the rows will be returned as objects that |
|
957 | - * are instances of that class. $class MUST be an fully qualified |
|
958 | - * class name, meaning that it must include the namespace, if applicable. |
|
959 | - * |
|
960 | - * @param string $class |
|
961 | - * @return $this |
|
962 | - */ |
|
963 | - public function as_object($class=null) |
|
964 | - { |
|
965 | - $this->temp_return_type = ! empty($class) ? $class : 'object'; |
|
966 | - |
|
967 | - return $this; |
|
968 | - } |
|
969 | - |
|
970 | - //-------------------------------------------------------------------- |
|
971 | - |
|
972 | - /** |
|
973 | - * Also fetches deleted items for this request only. |
|
974 | - */ |
|
975 | - public function with_deleted() |
|
976 | - { |
|
977 | - $this->temp_with_deleted = TRUE; |
|
978 | - |
|
979 | - return $this; |
|
980 | - } |
|
981 | - |
|
982 | - //-------------------------------------------------------------------- |
|
983 | - |
|
984 | - /** |
|
985 | - * Returns whether the current setup will return |
|
986 | - * soft deleted rows. |
|
987 | - * |
|
988 | - * @return bool |
|
989 | - */ |
|
990 | - public function get_with_deleted() |
|
991 | - { |
|
992 | - return $this->temp_with_deleted; |
|
993 | - } |
|
994 | - |
|
995 | - //-------------------------------------------------------------------- |
|
996 | - |
|
997 | - |
|
998 | - /** |
|
999 | - * Sets the $skip_validation parameter. |
|
1000 | - * |
|
1001 | - * @param bool $skip |
|
1002 | - * @return $this |
|
1003 | - */ |
|
1004 | - public function skip_validation($skip = true) |
|
1005 | - { |
|
1006 | - $this->skip_validation = $skip; |
|
1007 | - |
|
1008 | - return $this; |
|
1009 | - } |
|
1010 | - |
|
1011 | - //-------------------------------------------------------------------- |
|
1012 | - |
|
1013 | - |
|
1014 | - //-------------------------------------------------------------------- |
|
1015 | - // Utility Methods |
|
1016 | - //-------------------------------------------------------------------- |
|
1017 | - |
|
1018 | - /** |
|
1019 | - * Counts number of rows modified by an arbitrary WHERE call. |
|
1020 | - * @return INT |
|
1021 | - */ |
|
1022 | - public function count_by() |
|
1023 | - { |
|
1024 | - $where = func_get_args(); |
|
1025 | - $this->_set_where($where); |
|
1026 | - |
|
1027 | - return $this->db->count_all_results($this->table_name); |
|
1028 | - } |
|
1029 | - |
|
1030 | - //-------------------------------------------------------------------- |
|
1031 | - |
|
1032 | - /** |
|
1033 | - * Counts total number of records, disregarding any previous conditions. |
|
1034 | - * |
|
1035 | - * @return int |
|
1036 | - */ |
|
1037 | - public function count_all() |
|
1038 | - { |
|
1039 | - return $this->db->count_all($this->table_name); |
|
1040 | - } |
|
1041 | - |
|
1042 | - //-------------------------------------------------------------------- |
|
1043 | - |
|
1044 | - /** |
|
1045 | - * Getter for the table name. |
|
1046 | - * |
|
1047 | - * @return string The name of the table used by this class. |
|
1048 | - */ |
|
1049 | - public function table() |
|
1050 | - { |
|
1051 | - return $this->table_name; |
|
1052 | - } |
|
1053 | - |
|
1054 | - //-------------------------------------------------------------------- |
|
1055 | - |
|
1056 | - /** |
|
1057 | - * Set the return_insert_id value. |
|
1058 | - * |
|
1059 | - * @param boolean $return If TRUE, insert will return the insert_id. |
|
1060 | - */ |
|
1061 | - public function return_insert_id($return = true) |
|
1062 | - { |
|
1063 | - $this->return_insert_id = (bool)$return; |
|
1064 | - |
|
1065 | - return $this; |
|
1066 | - } |
|
1067 | - |
|
1068 | - //-------------------------------------------------------------------- |
|
1069 | - |
|
1070 | - /** |
|
1071 | - * A convenience method to return only a single field of the specified row. |
|
1072 | - * |
|
1073 | - * @param mixed $id The primary_key value to match against. |
|
1074 | - * @param string $field The field to search for. |
|
1075 | - * |
|
1076 | - * @return bool|mixed The value of the field. |
|
1077 | - */ |
|
1078 | - public function get_field($id = NULL, $field = '') |
|
1079 | - { |
|
1080 | - $this->db->select($field); |
|
1081 | - $this->db->where($this->primary_key, $id); |
|
1082 | - $query = $this->db->get($this->table_name); |
|
1083 | - |
|
1084 | - if ($query && $query->num_rows() > 0) { |
|
1085 | - return $query->row()->$field; |
|
1086 | - } |
|
1087 | - |
|
1088 | - return FALSE; |
|
1089 | - |
|
1090 | - } |
|
1091 | - |
|
1092 | - //--------------------------------------------------------------- |
|
1093 | - |
|
1094 | - /** |
|
1095 | - * Checks whether a field/value pair exists within the table. |
|
1096 | - * |
|
1097 | - * @param string $field The field to search for. |
|
1098 | - * @param string $value The value to match $field against. |
|
1099 | - * |
|
1100 | - * @return bool TRUE/FALSE |
|
1101 | - */ |
|
1102 | - public function is_unique($field, $value) |
|
1103 | - { |
|
1104 | - $this->db->where($field, $value); |
|
1105 | - $query = $this->db->get($this->table_name); |
|
1106 | - |
|
1107 | - if ($query && $query->num_rows() == 0) { |
|
1108 | - return TRUE; |
|
1109 | - } |
|
1110 | - |
|
1111 | - return FALSE; |
|
1112 | - |
|
1113 | - } |
|
1114 | - |
|
1115 | - //--------------------------------------------------------------- |
|
1116 | - |
|
1117 | - /** |
|
1118 | - * Adds a field to the protected_attributes array. |
|
1119 | - * |
|
1120 | - * @param $field |
|
1121 | - * |
|
1122 | - * @return mixed |
|
1123 | - */ |
|
1124 | - public function protect($field) |
|
1125 | - { |
|
1126 | - $this->protected_attributes[] = $field; |
|
1127 | - |
|
1128 | - return $this; |
|
1129 | - } |
|
1130 | - |
|
1131 | - //-------------------------------------------------------------------- |
|
1132 | - |
|
1133 | - /** |
|
1134 | - * Get the field names for this model's table. |
|
1135 | - * |
|
1136 | - * Returns the model's database fields stored in $this->fields |
|
1137 | - * if set, else it tries to retrieve the field list from |
|
1138 | - * $this->db->list_fields($this->table_name); |
|
1139 | - * |
|
1140 | - * @return array Returns the database fields for this model |
|
1141 | - */ |
|
1142 | - public function get_fields() |
|
1143 | - { |
|
1144 | - if (empty($this->fields)) { |
|
1145 | - $this->fields = $this->db->list_fields($this->table_name); |
|
1146 | - } |
|
1147 | - |
|
1148 | - return $this->fields; |
|
1149 | - } |
|
1150 | - |
|
1151 | - //-------------------------------------------------------------------- |
|
1152 | - |
|
1153 | - /** |
|
1154 | - * Extracts the model's fields (except the key and those handled by |
|
1155 | - * Observers) from the $post_data and returns an array of name => value pairs |
|
1156 | - * |
|
1157 | - * @param Array $post_data The post data, usually $this->input->post() when called from the controller |
|
1158 | - * |
|
1159 | - * @return Array An array of name => value pairs containing the data for the model's fields |
|
1160 | - */ |
|
1161 | - public function prep_data($post_data) |
|
1162 | - { |
|
1163 | - $data = array(); |
|
1164 | - $skippedFields = array(); |
|
1165 | - |
|
1166 | - if (empty($post_data)) |
|
1167 | - { |
|
1168 | - return []; |
|
1169 | - } |
|
1170 | - |
|
1171 | - // Though the model doesn't support multiple keys well, $this->key |
|
1172 | - // could be an array or a string... |
|
1173 | - $skippedFields = array_merge($skippedFields, (array)$this->primary_key); |
|
1174 | - |
|
1175 | - // Remove any protected attributes |
|
1176 | - $skippedFields = array_merge($skippedFields, $this->protected_attributes); |
|
1177 | - |
|
1178 | - $fields = $this->get_fields(); |
|
1179 | - |
|
1180 | - // If the field is the primary key, one of the created/modified/deleted |
|
1181 | - // fields, or has not been set in the $post_data, skip it |
|
1182 | - foreach ($post_data as $field => $value) { |
|
1183 | - if (in_array($field, $skippedFields) || |
|
1184 | - ! in_array($field, $fields)) |
|
1185 | - { |
|
1186 | - continue; |
|
1187 | - } |
|
1188 | - |
|
1189 | - $data[$field] = $value; |
|
1190 | - } |
|
1191 | - |
|
1192 | - return $data; |
|
1193 | - } |
|
1194 | - |
|
1195 | - //-------------------------------------------------------------------- |
|
1196 | - |
|
1197 | - /** |
|
1198 | - * Returns the last query string, if available. Simply a wrapper for |
|
1199 | - * CodeIgniter's database method of the same name. |
|
1200 | - * |
|
1201 | - * @return string |
|
1202 | - */ |
|
1203 | - public function last_query () |
|
1204 | - { |
|
1205 | - return $this->db->last_query(); |
|
1206 | - } |
|
1207 | - |
|
1208 | - //-------------------------------------------------------------------- |
|
1209 | - |
|
1210 | - /** |
|
1211 | - * Returns the elapsed time for the last query that was executed, if |
|
1212 | - * available, or NULL if not available, like if debug mode is off. |
|
1213 | - * |
|
1214 | - * @return mixed |
|
1215 | - */ |
|
1216 | - public function last_query_time () |
|
1217 | - { |
|
1218 | - $times = $this->db->query_times; |
|
1219 | - |
|
1220 | - if (! is_array($this->db->query_times) || ! count($this->db->query_times)) |
|
1221 | - { |
|
1222 | - return null; |
|
1223 | - } |
|
1224 | - |
|
1225 | - return end($times); |
|
1226 | - } |
|
1227 | - |
|
1228 | - //-------------------------------------------------------------------- |
|
1229 | - |
|
1230 | - //-------------------------------------------------------------------- |
|
1231 | - // Observers |
|
1232 | - //-------------------------------------------------------------------- |
|
1233 | - |
|
1234 | - /** |
|
1235 | - * Sets the created on date for the object based on the |
|
1236 | - * current date/time and date_format. Will not overwrite existing. |
|
1237 | - * |
|
1238 | - * @param array $row The array of data to be inserted |
|
1239 | - * |
|
1240 | - * @return array |
|
1241 | - */ |
|
1242 | - public function created_on($row) |
|
1243 | - { |
|
1244 | - if (empty($row['fields'])) |
|
1245 | - { |
|
1246 | - return null; |
|
1247 | - } |
|
1248 | - |
|
1249 | - $row = $row['fields']; |
|
1250 | - |
|
1251 | - // Created_on |
|
1252 | - if (! array_key_exists($this->created_field, $row)) |
|
1253 | - { |
|
1254 | - $row[$this->created_field] = $this->set_date(); |
|
1255 | - } |
|
1256 | - |
|
1257 | - // Created by |
|
1258 | - if ($this->log_user && ! array_key_exists($this->created_by_field, $row) && is_object($this->authenticate)) |
|
1259 | - { |
|
1260 | - // If you're here because of an error with $this->authenticate |
|
1261 | - // not being available, it's likely due to you not using |
|
1262 | - // the AuthTrait and/or setting log_user after model is instantiated. |
|
1263 | - $row[$this->created_by_field] = (int)$this->authenticate->id(); |
|
1264 | - } |
|
1265 | - |
|
1266 | - return $row; |
|
1267 | - } // end created_on() |
|
1268 | - |
|
1269 | - //-------------------------------------------------------------------- |
|
1270 | - |
|
1271 | - /** |
|
1272 | - * Sets the modified_on date for the object based on the |
|
1273 | - * current date/time and date_format. Will not overwrite existing. |
|
1274 | - * |
|
1275 | - * @param array $row The array of data to be inserted |
|
1276 | - * |
|
1277 | - * @return array |
|
1278 | - */ |
|
1279 | - public function modified_on($row) |
|
1280 | - { |
|
1281 | - if (empty($row['fields'])) |
|
1282 | - { |
|
1283 | - return null; |
|
1284 | - } |
|
1285 | - |
|
1286 | - $row = $row['fields']; |
|
1287 | - |
|
1288 | - if (is_array($row) && ! array_key_exists($this->modified_field, $row)) |
|
1289 | - { |
|
1290 | - $row[$this->modified_field] = $this->set_date(); |
|
1291 | - } |
|
1292 | - |
|
1293 | - // Modified by |
|
1294 | - if ($this->log_user && ! array_key_exists($this->modified_by_field, $row) && is_object($this->authenticate)) |
|
1295 | - { |
|
1296 | - // If you're here because of an error with $this->authenticate |
|
1297 | - // not being available, it's likely due to you not using |
|
1298 | - // the AuthTrait and/or setting log_user after model is instantiated. |
|
1299 | - $row[$this->modified_by_field] = $this->authenticate->id(); |
|
1300 | - } |
|
1301 | - |
|
1302 | - return $row; |
|
1303 | - } |
|
1304 | - |
|
1305 | - //-------------------------------------------------------------------- |
|
1306 | - |
|
1307 | - //-------------------------------------------------------------------- |
|
1308 | - // Internal Methods |
|
1309 | - //-------------------------------------------------------------------- |
|
1310 | - |
|
1311 | - /** |
|
1312 | - * Set WHERE parameters |
|
1313 | - */ |
|
1314 | - protected function _set_where($params) |
|
1315 | - { |
|
1316 | - if (count($params) == 1) { |
|
1317 | - $this->db->where($params[0]); |
|
1318 | - } else { |
|
1319 | - $this->db->where($params[0], $params[1]); |
|
1320 | - } |
|
1321 | - } |
|
1322 | - |
|
1323 | - //-------------------------------------------------------------------- |
|
1324 | - |
|
1325 | - /** |
|
1326 | - * Triggers a model-specific event and call each of it's observers. |
|
1327 | - * |
|
1328 | - * @param string $event The name of the event to trigger |
|
1329 | - * @param mixed $data The data to be passed to the callback functions. |
|
1330 | - * |
|
1331 | - * @return mixed |
|
1332 | - */ |
|
1333 | - public function trigger($event, $data = false) |
|
1334 | - { |
|
1335 | - if (! isset($this->$event) || ! is_array($this->$event)) |
|
1336 | - { |
|
1337 | - if (isset($data['fields'])) |
|
1338 | - { |
|
1339 | - return $data['fields']; |
|
1340 | - } |
|
1341 | - |
|
1342 | - return $data; |
|
1343 | - } |
|
1344 | - |
|
1345 | - foreach ($this->$event as $method) |
|
1346 | - { |
|
1347 | - if (strpos($method, '(')) |
|
1348 | - { |
|
1349 | - preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
1350 | - $this->callback_parameters = explode(',', $matches[3]); |
|
1351 | - } |
|
1352 | - |
|
1353 | - $data = call_user_func_array(array($this, $method), array($data)); |
|
1354 | - } |
|
1355 | - |
|
1356 | - // In case no method called or method returned |
|
1357 | - // the entire data array, we typically just need the $fields |
|
1358 | - if (isset($data['fields'])) |
|
1359 | - { |
|
1360 | - return $data['fields']; |
|
1361 | - } |
|
1362 | - |
|
1363 | - // A few methods might need to return 'ids' |
|
1364 | - if (isset($data['ids'])) |
|
1365 | - { |
|
1366 | - return $data['ids']; |
|
1367 | - } |
|
1368 | - |
|
1369 | - return $data; |
|
1370 | - } |
|
1371 | - |
|
1372 | - //-------------------------------------------------------------------- |
|
1373 | - |
|
1374 | - /** |
|
1375 | - * Validates the data passed into it based upon the form_validation rules |
|
1376 | - * setup in the $this->validate property. |
|
1377 | - * |
|
1378 | - * If $type == 'insert', any additional rules in the class var $insert_validate_rules |
|
1379 | - * for that field will be added to the rules. |
|
1380 | - * |
|
1381 | - * @param array $data An array of validation rules |
|
1382 | - * @param string $type Either 'update' or 'insert'. |
|
1383 | - * @return array/bool The original data or FALSE |
|
1384 | - */ |
|
1385 | - public function validate($data, $type = 'update', $skip_validation = null) |
|
1386 | - { |
|
1387 | - $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
1388 | - |
|
1389 | - if ($skip_validation) { |
|
1390 | - return $data; |
|
1391 | - } |
|
1392 | - |
|
1393 | - // We need the database to be loaded up at this point in case |
|
1394 | - // we want to use callbacks that hit the database. |
|
1395 | - if (empty($this->db)) |
|
1396 | - { |
|
1397 | - $this->load->database(); |
|
1398 | - } |
|
1399 | - |
|
1400 | - if (!empty($this->validation_rules)) { |
|
1401 | - $this->form_validation->set_data($data); |
|
1402 | - |
|
1403 | - if (is_array($this->validation_rules)) { |
|
1404 | - // Any insert additions? |
|
1405 | - if ($type == 'insert' |
|
1406 | - && !empty($this->insert_validate_rules) |
|
1407 | - && is_array($this->insert_validate_rules) |
|
1408 | - ) { |
|
1409 | - foreach ($this->validation_rules as &$row) { |
|
1410 | - if (isset($this->insert_validate_rules[$row['field']])) { |
|
1411 | - $row ['rules'] .= '|' . $this->insert_validate_rules[$row['field']]; |
|
1412 | - } |
|
1413 | - } |
|
1414 | - } |
|
1415 | - |
|
1416 | - $this->form_validation->set_rules($this->validation_rules); |
|
1417 | - |
|
1418 | - if ($this->form_validation->run('', $this) === TRUE) { |
|
1419 | - return $data; |
|
1420 | - } else { |
|
1421 | - return FALSE; |
|
1422 | - } |
|
1423 | - } else { |
|
1424 | - if ($this->form_validation->run($this->validate, $this) === TRUE) { |
|
1425 | - return $data; |
|
1426 | - } else { |
|
1427 | - return FALSE; |
|
1428 | - } |
|
1429 | - } |
|
1430 | - } else { |
|
1431 | - return $data; |
|
1432 | - } |
|
1433 | - } |
|
1434 | - |
|
1435 | - //-------------------------------------------------------------------- |
|
1436 | - |
|
1437 | - /** |
|
1438 | - * Protect attributes by removing them from $row array. Useful for |
|
1439 | - * removing id, or submit buttons names if you simply throw your $_POST |
|
1440 | - * array at your model. :) |
|
1441 | - * |
|
1442 | - * @param object /array $row The value pair item to remove. |
|
1443 | - */ |
|
1444 | - public function protect_attributes($row) |
|
1445 | - { |
|
1446 | - foreach ($this->protected_attributes as $attr) { |
|
1447 | - if (is_object($row)) { |
|
1448 | - unset($row->$attr); |
|
1449 | - } else { |
|
1450 | - unset($row[$attr]); |
|
1451 | - } |
|
1452 | - } |
|
1453 | - |
|
1454 | - return $row; |
|
1455 | - } |
|
1456 | - |
|
1457 | - //-------------------------------------------------------------------- |
|
1458 | - |
|
1459 | - /** |
|
1460 | - * A utility function to allow child models to use the type of |
|
1461 | - * date/time format that they prefer. This is primarily used for |
|
1462 | - * setting created_on and modified_on values, but can be used by |
|
1463 | - * inheriting classes. |
|
1464 | - * |
|
1465 | - * The available time formats are: |
|
1466 | - * * 'int' - Stores the date as an integer timestamp. |
|
1467 | - * * 'datetime' - Stores the date and time in the SQL datetime format. |
|
1468 | - * * 'date' - Stores teh date (only) in the SQL date format. |
|
1469 | - * |
|
1470 | - * @param mixed $user_date An optional PHP timestamp to be converted. |
|
1471 | - * |
|
1472 | - * @access protected |
|
1473 | - * |
|
1474 | - * @return int|null|string The current/user time converted to the proper format. |
|
1475 | - */ |
|
1476 | - protected function set_date($user_date = NULL) |
|
1477 | - { |
|
1478 | - $curr_date = !empty($user_date) ? $user_date : time(); |
|
1479 | - |
|
1480 | - switch ($this->date_format) { |
|
1481 | - case 'int': |
|
1482 | - return $curr_date; |
|
1483 | - break; |
|
1484 | - case 'datetime': |
|
1485 | - return date('Y-m-d H:i:s', $curr_date); |
|
1486 | - break; |
|
1487 | - case 'date': |
|
1488 | - return date('Y-m-d', $curr_date); |
|
1489 | - break; |
|
1490 | - } |
|
1491 | - |
|
1492 | - }//end set_date() |
|
1493 | - |
|
1494 | - //-------------------------------------------------------------------- |
|
1495 | - |
|
1496 | - /** |
|
1497 | - * Returns an array containing the 'code' and 'message' of the |
|
1498 | - * database's error, as provided by CI's database drivers. |
|
1499 | - * |
|
1500 | - * @return mixed |
|
1501 | - */ |
|
1502 | - public function error($db_array_only=false) |
|
1503 | - { |
|
1504 | - // Send any validation errors if we have any. |
|
1505 | - if (function_exists('validation_errors') && validation_errors() && ! $db_array_only) |
|
1506 | - { |
|
1507 | - return validation_errors(); |
|
1508 | - } |
|
1509 | - |
|
1510 | - // No validation errors? Return the db error. |
|
1511 | - $error = $this->db->error(); |
|
1512 | - |
|
1513 | - if ($db_array_only) |
|
1514 | - { |
|
1515 | - return $error; |
|
1516 | - } |
|
1517 | - |
|
1518 | - if (! empty($error['code'])) |
|
1519 | - { |
|
1520 | - return "Database Error {$error['code']}: {$error['message']}."; |
|
1521 | - } |
|
1522 | - |
|
1523 | - // No errors found. |
|
1524 | - return ''; |
|
1525 | - } |
|
1526 | - |
|
1527 | - //-------------------------------------------------------------------- |
|
1528 | - |
|
1529 | - //-------------------------------------------------------------------- |
|
1530 | - // Magic Methods |
|
1531 | - //-------------------------------------------------------------------- |
|
1532 | - |
|
1533 | - /** |
|
1534 | - * __get magic |
|
1535 | - * |
|
1536 | - * Allows models to access CI's loaded classes using the same |
|
1537 | - * syntax as controllers. |
|
1538 | - * |
|
1539 | - * This is the same as what CI's model uses, but we keep it |
|
1540 | - * here since that's the ONLY thing that CI's model does. |
|
1541 | - * |
|
1542 | - * @param string $key |
|
1543 | - */ |
|
1544 | - public function __get($key) |
|
1545 | - { |
|
1546 | - // Give them first crack at any protected class vars |
|
1547 | - if (isset($this->$key)) |
|
1548 | - { |
|
1549 | - return $this->$key; |
|
1550 | - } |
|
1551 | - |
|
1552 | - // Debugging note: |
|
1553 | - // If you're here because you're getting an error message |
|
1554 | - // saying 'Undefined Property: system/core/Model.php', it's |
|
1555 | - // most likely a typo in your model code. |
|
1556 | - return get_instance()->$key; |
|
1557 | - } |
|
1558 | - |
|
1559 | - //-------------------------------------------------------------------- |
|
1560 | - |
|
1561 | - /** |
|
1562 | - * Provide direct access to any of CodeIgniter's DB methods but |
|
1563 | - * make it look like it's part of the class, purely for convenience. |
|
1564 | - * |
|
1565 | - * @param $name |
|
1566 | - * @param $params |
|
1567 | - */ |
|
1568 | - public function __call($name, $params=null) |
|
1569 | - { |
|
1570 | - if (method_exists($this->db, $name)) |
|
1571 | - { |
|
1572 | - call_user_func_array([$this->db, $name], $params); |
|
1573 | - return $this; |
|
1574 | - } |
|
1575 | - } |
|
1576 | - |
|
1577 | - //-------------------------------------------------------------------- |
|
936 | + return $this; |
|
937 | + } |
|
938 | + |
|
939 | + //-------------------------------------------------------------------- |
|
940 | + |
|
941 | + /** |
|
942 | + * Temporarily sets our return type to an array. |
|
943 | + */ |
|
944 | + public function as_array() |
|
945 | + { |
|
946 | + $this->temp_return_type = 'array'; |
|
947 | + |
|
948 | + return $this; |
|
949 | + } |
|
950 | + |
|
951 | + //-------------------------------------------------------------------- |
|
952 | + |
|
953 | + /** |
|
954 | + * Temporarily sets our return type to an object. |
|
955 | + * |
|
956 | + * If $class is provided, the rows will be returned as objects that |
|
957 | + * are instances of that class. $class MUST be an fully qualified |
|
958 | + * class name, meaning that it must include the namespace, if applicable. |
|
959 | + * |
|
960 | + * @param string $class |
|
961 | + * @return $this |
|
962 | + */ |
|
963 | + public function as_object($class=null) |
|
964 | + { |
|
965 | + $this->temp_return_type = ! empty($class) ? $class : 'object'; |
|
966 | + |
|
967 | + return $this; |
|
968 | + } |
|
969 | + |
|
970 | + //-------------------------------------------------------------------- |
|
971 | + |
|
972 | + /** |
|
973 | + * Also fetches deleted items for this request only. |
|
974 | + */ |
|
975 | + public function with_deleted() |
|
976 | + { |
|
977 | + $this->temp_with_deleted = TRUE; |
|
978 | + |
|
979 | + return $this; |
|
980 | + } |
|
981 | + |
|
982 | + //-------------------------------------------------------------------- |
|
983 | + |
|
984 | + /** |
|
985 | + * Returns whether the current setup will return |
|
986 | + * soft deleted rows. |
|
987 | + * |
|
988 | + * @return bool |
|
989 | + */ |
|
990 | + public function get_with_deleted() |
|
991 | + { |
|
992 | + return $this->temp_with_deleted; |
|
993 | + } |
|
994 | + |
|
995 | + //-------------------------------------------------------------------- |
|
996 | + |
|
997 | + |
|
998 | + /** |
|
999 | + * Sets the $skip_validation parameter. |
|
1000 | + * |
|
1001 | + * @param bool $skip |
|
1002 | + * @return $this |
|
1003 | + */ |
|
1004 | + public function skip_validation($skip = true) |
|
1005 | + { |
|
1006 | + $this->skip_validation = $skip; |
|
1007 | + |
|
1008 | + return $this; |
|
1009 | + } |
|
1010 | + |
|
1011 | + //-------------------------------------------------------------------- |
|
1012 | + |
|
1013 | + |
|
1014 | + //-------------------------------------------------------------------- |
|
1015 | + // Utility Methods |
|
1016 | + //-------------------------------------------------------------------- |
|
1017 | + |
|
1018 | + /** |
|
1019 | + * Counts number of rows modified by an arbitrary WHERE call. |
|
1020 | + * @return INT |
|
1021 | + */ |
|
1022 | + public function count_by() |
|
1023 | + { |
|
1024 | + $where = func_get_args(); |
|
1025 | + $this->_set_where($where); |
|
1026 | + |
|
1027 | + return $this->db->count_all_results($this->table_name); |
|
1028 | + } |
|
1029 | + |
|
1030 | + //-------------------------------------------------------------------- |
|
1031 | + |
|
1032 | + /** |
|
1033 | + * Counts total number of records, disregarding any previous conditions. |
|
1034 | + * |
|
1035 | + * @return int |
|
1036 | + */ |
|
1037 | + public function count_all() |
|
1038 | + { |
|
1039 | + return $this->db->count_all($this->table_name); |
|
1040 | + } |
|
1041 | + |
|
1042 | + //-------------------------------------------------------------------- |
|
1043 | + |
|
1044 | + /** |
|
1045 | + * Getter for the table name. |
|
1046 | + * |
|
1047 | + * @return string The name of the table used by this class. |
|
1048 | + */ |
|
1049 | + public function table() |
|
1050 | + { |
|
1051 | + return $this->table_name; |
|
1052 | + } |
|
1053 | + |
|
1054 | + //-------------------------------------------------------------------- |
|
1055 | + |
|
1056 | + /** |
|
1057 | + * Set the return_insert_id value. |
|
1058 | + * |
|
1059 | + * @param boolean $return If TRUE, insert will return the insert_id. |
|
1060 | + */ |
|
1061 | + public function return_insert_id($return = true) |
|
1062 | + { |
|
1063 | + $this->return_insert_id = (bool)$return; |
|
1064 | + |
|
1065 | + return $this; |
|
1066 | + } |
|
1067 | + |
|
1068 | + //-------------------------------------------------------------------- |
|
1069 | + |
|
1070 | + /** |
|
1071 | + * A convenience method to return only a single field of the specified row. |
|
1072 | + * |
|
1073 | + * @param mixed $id The primary_key value to match against. |
|
1074 | + * @param string $field The field to search for. |
|
1075 | + * |
|
1076 | + * @return bool|mixed The value of the field. |
|
1077 | + */ |
|
1078 | + public function get_field($id = NULL, $field = '') |
|
1079 | + { |
|
1080 | + $this->db->select($field); |
|
1081 | + $this->db->where($this->primary_key, $id); |
|
1082 | + $query = $this->db->get($this->table_name); |
|
1083 | + |
|
1084 | + if ($query && $query->num_rows() > 0) { |
|
1085 | + return $query->row()->$field; |
|
1086 | + } |
|
1087 | + |
|
1088 | + return FALSE; |
|
1089 | + |
|
1090 | + } |
|
1091 | + |
|
1092 | + //--------------------------------------------------------------- |
|
1093 | + |
|
1094 | + /** |
|
1095 | + * Checks whether a field/value pair exists within the table. |
|
1096 | + * |
|
1097 | + * @param string $field The field to search for. |
|
1098 | + * @param string $value The value to match $field against. |
|
1099 | + * |
|
1100 | + * @return bool TRUE/FALSE |
|
1101 | + */ |
|
1102 | + public function is_unique($field, $value) |
|
1103 | + { |
|
1104 | + $this->db->where($field, $value); |
|
1105 | + $query = $this->db->get($this->table_name); |
|
1106 | + |
|
1107 | + if ($query && $query->num_rows() == 0) { |
|
1108 | + return TRUE; |
|
1109 | + } |
|
1110 | + |
|
1111 | + return FALSE; |
|
1112 | + |
|
1113 | + } |
|
1114 | + |
|
1115 | + //--------------------------------------------------------------- |
|
1116 | + |
|
1117 | + /** |
|
1118 | + * Adds a field to the protected_attributes array. |
|
1119 | + * |
|
1120 | + * @param $field |
|
1121 | + * |
|
1122 | + * @return mixed |
|
1123 | + */ |
|
1124 | + public function protect($field) |
|
1125 | + { |
|
1126 | + $this->protected_attributes[] = $field; |
|
1127 | + |
|
1128 | + return $this; |
|
1129 | + } |
|
1130 | + |
|
1131 | + //-------------------------------------------------------------------- |
|
1132 | + |
|
1133 | + /** |
|
1134 | + * Get the field names for this model's table. |
|
1135 | + * |
|
1136 | + * Returns the model's database fields stored in $this->fields |
|
1137 | + * if set, else it tries to retrieve the field list from |
|
1138 | + * $this->db->list_fields($this->table_name); |
|
1139 | + * |
|
1140 | + * @return array Returns the database fields for this model |
|
1141 | + */ |
|
1142 | + public function get_fields() |
|
1143 | + { |
|
1144 | + if (empty($this->fields)) { |
|
1145 | + $this->fields = $this->db->list_fields($this->table_name); |
|
1146 | + } |
|
1147 | + |
|
1148 | + return $this->fields; |
|
1149 | + } |
|
1150 | + |
|
1151 | + //-------------------------------------------------------------------- |
|
1152 | + |
|
1153 | + /** |
|
1154 | + * Extracts the model's fields (except the key and those handled by |
|
1155 | + * Observers) from the $post_data and returns an array of name => value pairs |
|
1156 | + * |
|
1157 | + * @param Array $post_data The post data, usually $this->input->post() when called from the controller |
|
1158 | + * |
|
1159 | + * @return Array An array of name => value pairs containing the data for the model's fields |
|
1160 | + */ |
|
1161 | + public function prep_data($post_data) |
|
1162 | + { |
|
1163 | + $data = array(); |
|
1164 | + $skippedFields = array(); |
|
1165 | + |
|
1166 | + if (empty($post_data)) |
|
1167 | + { |
|
1168 | + return []; |
|
1169 | + } |
|
1170 | + |
|
1171 | + // Though the model doesn't support multiple keys well, $this->key |
|
1172 | + // could be an array or a string... |
|
1173 | + $skippedFields = array_merge($skippedFields, (array)$this->primary_key); |
|
1174 | + |
|
1175 | + // Remove any protected attributes |
|
1176 | + $skippedFields = array_merge($skippedFields, $this->protected_attributes); |
|
1177 | + |
|
1178 | + $fields = $this->get_fields(); |
|
1179 | + |
|
1180 | + // If the field is the primary key, one of the created/modified/deleted |
|
1181 | + // fields, or has not been set in the $post_data, skip it |
|
1182 | + foreach ($post_data as $field => $value) { |
|
1183 | + if (in_array($field, $skippedFields) || |
|
1184 | + ! in_array($field, $fields)) |
|
1185 | + { |
|
1186 | + continue; |
|
1187 | + } |
|
1188 | + |
|
1189 | + $data[$field] = $value; |
|
1190 | + } |
|
1191 | + |
|
1192 | + return $data; |
|
1193 | + } |
|
1194 | + |
|
1195 | + //-------------------------------------------------------------------- |
|
1196 | + |
|
1197 | + /** |
|
1198 | + * Returns the last query string, if available. Simply a wrapper for |
|
1199 | + * CodeIgniter's database method of the same name. |
|
1200 | + * |
|
1201 | + * @return string |
|
1202 | + */ |
|
1203 | + public function last_query () |
|
1204 | + { |
|
1205 | + return $this->db->last_query(); |
|
1206 | + } |
|
1207 | + |
|
1208 | + //-------------------------------------------------------------------- |
|
1209 | + |
|
1210 | + /** |
|
1211 | + * Returns the elapsed time for the last query that was executed, if |
|
1212 | + * available, or NULL if not available, like if debug mode is off. |
|
1213 | + * |
|
1214 | + * @return mixed |
|
1215 | + */ |
|
1216 | + public function last_query_time () |
|
1217 | + { |
|
1218 | + $times = $this->db->query_times; |
|
1219 | + |
|
1220 | + if (! is_array($this->db->query_times) || ! count($this->db->query_times)) |
|
1221 | + { |
|
1222 | + return null; |
|
1223 | + } |
|
1224 | + |
|
1225 | + return end($times); |
|
1226 | + } |
|
1227 | + |
|
1228 | + //-------------------------------------------------------------------- |
|
1229 | + |
|
1230 | + //-------------------------------------------------------------------- |
|
1231 | + // Observers |
|
1232 | + //-------------------------------------------------------------------- |
|
1233 | + |
|
1234 | + /** |
|
1235 | + * Sets the created on date for the object based on the |
|
1236 | + * current date/time and date_format. Will not overwrite existing. |
|
1237 | + * |
|
1238 | + * @param array $row The array of data to be inserted |
|
1239 | + * |
|
1240 | + * @return array |
|
1241 | + */ |
|
1242 | + public function created_on($row) |
|
1243 | + { |
|
1244 | + if (empty($row['fields'])) |
|
1245 | + { |
|
1246 | + return null; |
|
1247 | + } |
|
1248 | + |
|
1249 | + $row = $row['fields']; |
|
1250 | + |
|
1251 | + // Created_on |
|
1252 | + if (! array_key_exists($this->created_field, $row)) |
|
1253 | + { |
|
1254 | + $row[$this->created_field] = $this->set_date(); |
|
1255 | + } |
|
1256 | + |
|
1257 | + // Created by |
|
1258 | + if ($this->log_user && ! array_key_exists($this->created_by_field, $row) && is_object($this->authenticate)) |
|
1259 | + { |
|
1260 | + // If you're here because of an error with $this->authenticate |
|
1261 | + // not being available, it's likely due to you not using |
|
1262 | + // the AuthTrait and/or setting log_user after model is instantiated. |
|
1263 | + $row[$this->created_by_field] = (int)$this->authenticate->id(); |
|
1264 | + } |
|
1265 | + |
|
1266 | + return $row; |
|
1267 | + } // end created_on() |
|
1268 | + |
|
1269 | + //-------------------------------------------------------------------- |
|
1270 | + |
|
1271 | + /** |
|
1272 | + * Sets the modified_on date for the object based on the |
|
1273 | + * current date/time and date_format. Will not overwrite existing. |
|
1274 | + * |
|
1275 | + * @param array $row The array of data to be inserted |
|
1276 | + * |
|
1277 | + * @return array |
|
1278 | + */ |
|
1279 | + public function modified_on($row) |
|
1280 | + { |
|
1281 | + if (empty($row['fields'])) |
|
1282 | + { |
|
1283 | + return null; |
|
1284 | + } |
|
1285 | + |
|
1286 | + $row = $row['fields']; |
|
1287 | + |
|
1288 | + if (is_array($row) && ! array_key_exists($this->modified_field, $row)) |
|
1289 | + { |
|
1290 | + $row[$this->modified_field] = $this->set_date(); |
|
1291 | + } |
|
1292 | + |
|
1293 | + // Modified by |
|
1294 | + if ($this->log_user && ! array_key_exists($this->modified_by_field, $row) && is_object($this->authenticate)) |
|
1295 | + { |
|
1296 | + // If you're here because of an error with $this->authenticate |
|
1297 | + // not being available, it's likely due to you not using |
|
1298 | + // the AuthTrait and/or setting log_user after model is instantiated. |
|
1299 | + $row[$this->modified_by_field] = $this->authenticate->id(); |
|
1300 | + } |
|
1301 | + |
|
1302 | + return $row; |
|
1303 | + } |
|
1304 | + |
|
1305 | + //-------------------------------------------------------------------- |
|
1306 | + |
|
1307 | + //-------------------------------------------------------------------- |
|
1308 | + // Internal Methods |
|
1309 | + //-------------------------------------------------------------------- |
|
1310 | + |
|
1311 | + /** |
|
1312 | + * Set WHERE parameters |
|
1313 | + */ |
|
1314 | + protected function _set_where($params) |
|
1315 | + { |
|
1316 | + if (count($params) == 1) { |
|
1317 | + $this->db->where($params[0]); |
|
1318 | + } else { |
|
1319 | + $this->db->where($params[0], $params[1]); |
|
1320 | + } |
|
1321 | + } |
|
1322 | + |
|
1323 | + //-------------------------------------------------------------------- |
|
1324 | + |
|
1325 | + /** |
|
1326 | + * Triggers a model-specific event and call each of it's observers. |
|
1327 | + * |
|
1328 | + * @param string $event The name of the event to trigger |
|
1329 | + * @param mixed $data The data to be passed to the callback functions. |
|
1330 | + * |
|
1331 | + * @return mixed |
|
1332 | + */ |
|
1333 | + public function trigger($event, $data = false) |
|
1334 | + { |
|
1335 | + if (! isset($this->$event) || ! is_array($this->$event)) |
|
1336 | + { |
|
1337 | + if (isset($data['fields'])) |
|
1338 | + { |
|
1339 | + return $data['fields']; |
|
1340 | + } |
|
1341 | + |
|
1342 | + return $data; |
|
1343 | + } |
|
1344 | + |
|
1345 | + foreach ($this->$event as $method) |
|
1346 | + { |
|
1347 | + if (strpos($method, '(')) |
|
1348 | + { |
|
1349 | + preg_match('/([a-zA-Z0-9\_\-]+)(\(([a-zA-Z0-9\_\-\., ]+)\))?/', $method, $matches); |
|
1350 | + $this->callback_parameters = explode(',', $matches[3]); |
|
1351 | + } |
|
1352 | + |
|
1353 | + $data = call_user_func_array(array($this, $method), array($data)); |
|
1354 | + } |
|
1355 | + |
|
1356 | + // In case no method called or method returned |
|
1357 | + // the entire data array, we typically just need the $fields |
|
1358 | + if (isset($data['fields'])) |
|
1359 | + { |
|
1360 | + return $data['fields']; |
|
1361 | + } |
|
1362 | + |
|
1363 | + // A few methods might need to return 'ids' |
|
1364 | + if (isset($data['ids'])) |
|
1365 | + { |
|
1366 | + return $data['ids']; |
|
1367 | + } |
|
1368 | + |
|
1369 | + return $data; |
|
1370 | + } |
|
1371 | + |
|
1372 | + //-------------------------------------------------------------------- |
|
1373 | + |
|
1374 | + /** |
|
1375 | + * Validates the data passed into it based upon the form_validation rules |
|
1376 | + * setup in the $this->validate property. |
|
1377 | + * |
|
1378 | + * If $type == 'insert', any additional rules in the class var $insert_validate_rules |
|
1379 | + * for that field will be added to the rules. |
|
1380 | + * |
|
1381 | + * @param array $data An array of validation rules |
|
1382 | + * @param string $type Either 'update' or 'insert'. |
|
1383 | + * @return array/bool The original data or FALSE |
|
1384 | + */ |
|
1385 | + public function validate($data, $type = 'update', $skip_validation = null) |
|
1386 | + { |
|
1387 | + $skip_validation = is_null($skip_validation) ? $this->skip_validation : $skip_validation; |
|
1388 | + |
|
1389 | + if ($skip_validation) { |
|
1390 | + return $data; |
|
1391 | + } |
|
1392 | + |
|
1393 | + // We need the database to be loaded up at this point in case |
|
1394 | + // we want to use callbacks that hit the database. |
|
1395 | + if (empty($this->db)) |
|
1396 | + { |
|
1397 | + $this->load->database(); |
|
1398 | + } |
|
1399 | + |
|
1400 | + if (!empty($this->validation_rules)) { |
|
1401 | + $this->form_validation->set_data($data); |
|
1402 | + |
|
1403 | + if (is_array($this->validation_rules)) { |
|
1404 | + // Any insert additions? |
|
1405 | + if ($type == 'insert' |
|
1406 | + && !empty($this->insert_validate_rules) |
|
1407 | + && is_array($this->insert_validate_rules) |
|
1408 | + ) { |
|
1409 | + foreach ($this->validation_rules as &$row) { |
|
1410 | + if (isset($this->insert_validate_rules[$row['field']])) { |
|
1411 | + $row ['rules'] .= '|' . $this->insert_validate_rules[$row['field']]; |
|
1412 | + } |
|
1413 | + } |
|
1414 | + } |
|
1415 | + |
|
1416 | + $this->form_validation->set_rules($this->validation_rules); |
|
1417 | + |
|
1418 | + if ($this->form_validation->run('', $this) === TRUE) { |
|
1419 | + return $data; |
|
1420 | + } else { |
|
1421 | + return FALSE; |
|
1422 | + } |
|
1423 | + } else { |
|
1424 | + if ($this->form_validation->run($this->validate, $this) === TRUE) { |
|
1425 | + return $data; |
|
1426 | + } else { |
|
1427 | + return FALSE; |
|
1428 | + } |
|
1429 | + } |
|
1430 | + } else { |
|
1431 | + return $data; |
|
1432 | + } |
|
1433 | + } |
|
1434 | + |
|
1435 | + //-------------------------------------------------------------------- |
|
1436 | + |
|
1437 | + /** |
|
1438 | + * Protect attributes by removing them from $row array. Useful for |
|
1439 | + * removing id, or submit buttons names if you simply throw your $_POST |
|
1440 | + * array at your model. :) |
|
1441 | + * |
|
1442 | + * @param object /array $row The value pair item to remove. |
|
1443 | + */ |
|
1444 | + public function protect_attributes($row) |
|
1445 | + { |
|
1446 | + foreach ($this->protected_attributes as $attr) { |
|
1447 | + if (is_object($row)) { |
|
1448 | + unset($row->$attr); |
|
1449 | + } else { |
|
1450 | + unset($row[$attr]); |
|
1451 | + } |
|
1452 | + } |
|
1453 | + |
|
1454 | + return $row; |
|
1455 | + } |
|
1456 | + |
|
1457 | + //-------------------------------------------------------------------- |
|
1458 | + |
|
1459 | + /** |
|
1460 | + * A utility function to allow child models to use the type of |
|
1461 | + * date/time format that they prefer. This is primarily used for |
|
1462 | + * setting created_on and modified_on values, but can be used by |
|
1463 | + * inheriting classes. |
|
1464 | + * |
|
1465 | + * The available time formats are: |
|
1466 | + * * 'int' - Stores the date as an integer timestamp. |
|
1467 | + * * 'datetime' - Stores the date and time in the SQL datetime format. |
|
1468 | + * * 'date' - Stores teh date (only) in the SQL date format. |
|
1469 | + * |
|
1470 | + * @param mixed $user_date An optional PHP timestamp to be converted. |
|
1471 | + * |
|
1472 | + * @access protected |
|
1473 | + * |
|
1474 | + * @return int|null|string The current/user time converted to the proper format. |
|
1475 | + */ |
|
1476 | + protected function set_date($user_date = NULL) |
|
1477 | + { |
|
1478 | + $curr_date = !empty($user_date) ? $user_date : time(); |
|
1479 | + |
|
1480 | + switch ($this->date_format) { |
|
1481 | + case 'int': |
|
1482 | + return $curr_date; |
|
1483 | + break; |
|
1484 | + case 'datetime': |
|
1485 | + return date('Y-m-d H:i:s', $curr_date); |
|
1486 | + break; |
|
1487 | + case 'date': |
|
1488 | + return date('Y-m-d', $curr_date); |
|
1489 | + break; |
|
1490 | + } |
|
1491 | + |
|
1492 | + }//end set_date() |
|
1493 | + |
|
1494 | + //-------------------------------------------------------------------- |
|
1495 | + |
|
1496 | + /** |
|
1497 | + * Returns an array containing the 'code' and 'message' of the |
|
1498 | + * database's error, as provided by CI's database drivers. |
|
1499 | + * |
|
1500 | + * @return mixed |
|
1501 | + */ |
|
1502 | + public function error($db_array_only=false) |
|
1503 | + { |
|
1504 | + // Send any validation errors if we have any. |
|
1505 | + if (function_exists('validation_errors') && validation_errors() && ! $db_array_only) |
|
1506 | + { |
|
1507 | + return validation_errors(); |
|
1508 | + } |
|
1509 | + |
|
1510 | + // No validation errors? Return the db error. |
|
1511 | + $error = $this->db->error(); |
|
1512 | + |
|
1513 | + if ($db_array_only) |
|
1514 | + { |
|
1515 | + return $error; |
|
1516 | + } |
|
1517 | + |
|
1518 | + if (! empty($error['code'])) |
|
1519 | + { |
|
1520 | + return "Database Error {$error['code']}: {$error['message']}."; |
|
1521 | + } |
|
1522 | + |
|
1523 | + // No errors found. |
|
1524 | + return ''; |
|
1525 | + } |
|
1526 | + |
|
1527 | + //-------------------------------------------------------------------- |
|
1528 | + |
|
1529 | + //-------------------------------------------------------------------- |
|
1530 | + // Magic Methods |
|
1531 | + //-------------------------------------------------------------------- |
|
1532 | + |
|
1533 | + /** |
|
1534 | + * __get magic |
|
1535 | + * |
|
1536 | + * Allows models to access CI's loaded classes using the same |
|
1537 | + * syntax as controllers. |
|
1538 | + * |
|
1539 | + * This is the same as what CI's model uses, but we keep it |
|
1540 | + * here since that's the ONLY thing that CI's model does. |
|
1541 | + * |
|
1542 | + * @param string $key |
|
1543 | + */ |
|
1544 | + public function __get($key) |
|
1545 | + { |
|
1546 | + // Give them first crack at any protected class vars |
|
1547 | + if (isset($this->$key)) |
|
1548 | + { |
|
1549 | + return $this->$key; |
|
1550 | + } |
|
1551 | + |
|
1552 | + // Debugging note: |
|
1553 | + // If you're here because you're getting an error message |
|
1554 | + // saying 'Undefined Property: system/core/Model.php', it's |
|
1555 | + // most likely a typo in your model code. |
|
1556 | + return get_instance()->$key; |
|
1557 | + } |
|
1558 | + |
|
1559 | + //-------------------------------------------------------------------- |
|
1560 | + |
|
1561 | + /** |
|
1562 | + * Provide direct access to any of CodeIgniter's DB methods but |
|
1563 | + * make it look like it's part of the class, purely for convenience. |
|
1564 | + * |
|
1565 | + * @param $name |
|
1566 | + * @param $params |
|
1567 | + */ |
|
1568 | + public function __call($name, $params=null) |
|
1569 | + { |
|
1570 | + if (method_exists($this->db, $name)) |
|
1571 | + { |
|
1572 | + call_user_func_array([$this->db, $name], $params); |
|
1573 | + return $this; |
|
1574 | + } |
|
1575 | + } |
|
1576 | + |
|
1577 | + //-------------------------------------------------------------------- |
|
1578 | 1578 | |
1579 | 1579 | |
1580 | 1580 | } |
@@ -43,13 +43,13 @@ discard block |
||
43 | 43 | */ |
44 | 44 | abstract class BaseGenerator extends CLIController { |
45 | 45 | |
46 | - /** |
|
47 | - * Instance of the active themer. |
|
48 | - * @var null |
|
49 | - */ |
|
50 | - protected $themer = null; |
|
46 | + /** |
|
47 | + * Instance of the active themer. |
|
48 | + * @var null |
|
49 | + */ |
|
50 | + protected $themer = null; |
|
51 | 51 | |
52 | - protected $gen_path = null; |
|
52 | + protected $gen_path = null; |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * The name of the module being used, if any. |
@@ -62,254 +62,254 @@ discard block |
||
62 | 62 | |
63 | 63 | protected $overwrite = false; |
64 | 64 | |
65 | - //-------------------------------------------------------------------- |
|
65 | + //-------------------------------------------------------------------- |
|
66 | 66 | |
67 | - public function __construct() |
|
68 | - { |
|
69 | - parent::__construct(); |
|
67 | + public function __construct() |
|
68 | + { |
|
69 | + parent::__construct(); |
|
70 | 70 | |
71 | - $this->load->config('forge'); |
|
71 | + $this->load->config('forge'); |
|
72 | 72 | |
73 | - // Detect if we're genning in a module |
|
74 | - if ($module = CLI::option('module')) |
|
75 | - { |
|
76 | - $this->module = $module; |
|
73 | + // Detect if we're genning in a module |
|
74 | + if ($module = CLI::option('module')) |
|
75 | + { |
|
76 | + $this->module = $module; |
|
77 | 77 | |
78 | - $folders = config_item('modules_locations'); |
|
78 | + $folders = config_item('modules_locations'); |
|
79 | 79 | |
80 | - if (is_array($folders)) |
|
81 | - { |
|
82 | - $this->module_path = $folders[0] . strtolower($module) .'/'; |
|
83 | - } |
|
80 | + if (is_array($folders)) |
|
81 | + { |
|
82 | + $this->module_path = $folders[0] . strtolower($module) .'/'; |
|
83 | + } |
|
84 | 84 | } |
85 | 85 | |
86 | - // Should we overwrite files? |
|
87 | - if (CLI::option('overwrite')) |
|
88 | - { |
|
89 | - $this->overwrite = true; |
|
90 | - } |
|
91 | - } |
|
86 | + // Should we overwrite files? |
|
87 | + if (CLI::option('overwrite')) |
|
88 | + { |
|
89 | + $this->overwrite = true; |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - //-------------------------------------------------------------------- |
|
93 | + //-------------------------------------------------------------------- |
|
94 | 94 | |
95 | 95 | |
96 | - /** |
|
97 | - * The method called by the main generator script. This must be |
|
98 | - * overridden by child classes to implement the actual logic used. |
|
99 | - * |
|
100 | - * @param array $segments |
|
101 | - * @param bool $quiet If true, models should accept default values. |
|
102 | - * @return mixed |
|
103 | - */ |
|
104 | - abstract function run($segments=[], $quiet=false); |
|
96 | + /** |
|
97 | + * The method called by the main generator script. This must be |
|
98 | + * overridden by child classes to implement the actual logic used. |
|
99 | + * |
|
100 | + * @param array $segments |
|
101 | + * @param bool $quiet If true, models should accept default values. |
|
102 | + * @return mixed |
|
103 | + */ |
|
104 | + abstract function run($segments=[], $quiet=false); |
|
105 | 105 | |
106 | - //-------------------------------------------------------------------- |
|
106 | + //-------------------------------------------------------------------- |
|
107 | 107 | |
108 | 108 | |
109 | - /** |
|
110 | - * Creates a file at the specified path with the given contents. |
|
111 | - * |
|
112 | - * @param $path |
|
113 | - * @param null $contents |
|
114 | - * |
|
115 | - * @return bool |
|
116 | - */ |
|
117 | - public function createFile($path, $contents=null, $overwrite=false, $perms=0644) |
|
118 | - { |
|
119 | - $path = $this->sandbox($path); |
|
109 | + /** |
|
110 | + * Creates a file at the specified path with the given contents. |
|
111 | + * |
|
112 | + * @param $path |
|
113 | + * @param null $contents |
|
114 | + * |
|
115 | + * @return bool |
|
116 | + */ |
|
117 | + public function createFile($path, $contents=null, $overwrite=false, $perms=0644) |
|
118 | + { |
|
119 | + $path = $this->sandbox($path); |
|
120 | 120 | |
121 | - $file_exists = is_file($path); |
|
121 | + $file_exists = is_file($path); |
|
122 | 122 | |
123 | - // Does file already exist? |
|
124 | - if ($file_exists) |
|
125 | - { |
|
126 | - if (! $overwrite) { |
|
127 | - CLI::write( CLI::color("\t". strtolower(lang('exists')) .": ", 'blue') . str_replace(APPPATH, '', $path ) ); |
|
128 | - return true; |
|
129 | - } |
|
123 | + // Does file already exist? |
|
124 | + if ($file_exists) |
|
125 | + { |
|
126 | + if (! $overwrite) { |
|
127 | + CLI::write( CLI::color("\t". strtolower(lang('exists')) .": ", 'blue') . str_replace(APPPATH, '', $path ) ); |
|
128 | + return true; |
|
129 | + } |
|
130 | 130 | |
131 | - unlink($path); |
|
132 | - } |
|
131 | + unlink($path); |
|
132 | + } |
|
133 | 133 | |
134 | - // Do we need to create the directory? |
|
135 | - $segments = explode('/', $path); |
|
134 | + // Do we need to create the directory? |
|
135 | + $segments = explode('/', $path); |
|
136 | 136 | array_pop($segments); |
137 | 137 | $folder = implode('/', $segments); |
138 | 138 | |
139 | - if (! is_dir($folder)) |
|
140 | - { |
|
141 | - $this->createDirectory($folder); |
|
142 | - } |
|
139 | + if (! is_dir($folder)) |
|
140 | + { |
|
141 | + $this->createDirectory($folder); |
|
142 | + } |
|
143 | 143 | |
144 | - get_instance()->load->helper('file'); |
|
144 | + get_instance()->load->helper('file'); |
|
145 | 145 | |
146 | - if (! write_file($path, $contents)) |
|
147 | - { |
|
148 | - throw new \RuntimeException( sprintf( lang('errors.writing_file'), $path) ); |
|
149 | - } |
|
146 | + if (! write_file($path, $contents)) |
|
147 | + { |
|
148 | + throw new \RuntimeException( sprintf( lang('errors.writing_file'), $path) ); |
|
149 | + } |
|
150 | 150 | |
151 | - chmod($path, $perms); |
|
151 | + chmod($path, $perms); |
|
152 | 152 | |
153 | - if ($overwrite && $file_exists) |
|
154 | - { |
|
155 | - CLI::write( CLI::color("\t". strtolower( lang('overwrote') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
156 | - } |
|
157 | - else |
|
158 | - { |
|
159 | - CLI::write( CLI::color("\t". strtolower( lang('created') ) ." ", 'yellow') . str_replace(APPPATH, '', $path ) ); |
|
160 | - } |
|
153 | + if ($overwrite && $file_exists) |
|
154 | + { |
|
155 | + CLI::write( CLI::color("\t". strtolower( lang('overwrote') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
156 | + } |
|
157 | + else |
|
158 | + { |
|
159 | + CLI::write( CLI::color("\t". strtolower( lang('created') ) ." ", 'yellow') . str_replace(APPPATH, '', $path ) ); |
|
160 | + } |
|
161 | 161 | |
162 | - return $this; |
|
163 | - } |
|
162 | + return $this; |
|
163 | + } |
|
164 | 164 | |
165 | - //-------------------------------------------------------------------- |
|
166 | - |
|
167 | - /** |
|
168 | - * Creates a new directory at the specified path. |
|
169 | - * |
|
170 | - * @param $path |
|
171 | - * @param int|string $perms |
|
172 | - * |
|
173 | - * @return bool |
|
174 | - */ |
|
175 | - public function createDirectory($path, $perms=0755) |
|
176 | - { |
|
177 | - $path = $this->sandbox($path); |
|
178 | - |
|
179 | - if (is_dir($path)) |
|
180 | - { |
|
181 | - return $this; |
|
182 | - } |
|
183 | - |
|
184 | - if (! mkdir($path, $perms, true) ) |
|
185 | - { |
|
186 | - throw new \RuntimeException( sprintf( lang('errors.creating_dir'), $path) ); |
|
187 | - } |
|
188 | - |
|
189 | - return $this; |
|
190 | - } |
|
191 | - |
|
192 | - //-------------------------------------------------------------------- |
|
193 | - |
|
194 | - /** |
|
195 | - * Copies a file from the current template group to the destination. |
|
196 | - * |
|
197 | - * @param $source |
|
198 | - * @param $destination |
|
199 | - * @param bool $overwrite |
|
200 | - * |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function copyFile($source, $destination, $overwrite=false) |
|
204 | - { |
|
205 | - $source = $this->sandbox($source); |
|
206 | - |
|
207 | - if (! file_exists($source)) |
|
208 | - { |
|
209 | - return null; |
|
210 | - } |
|
211 | - |
|
212 | - $content = file_get_contents($source); |
|
213 | - |
|
214 | - return $this->createFile($destination, $content, $overwrite); |
|
215 | - } |
|
216 | - |
|
217 | - //-------------------------------------------------------------------- |
|
218 | - |
|
219 | - /** |
|
220 | - * Attempts to locate a template within the current template group, |
|
221 | - * parses it with the passed in data, and writes to the new location. |
|
222 | - * |
|
223 | - * @param $template |
|
224 | - * @param $destination |
|
225 | - * @param array $data |
|
226 | - * @param bool $overwrite |
|
227 | - * |
|
228 | - * @return $this |
|
229 | - */ |
|
230 | - public function copyTemplate($template, $destination, $data=[], $overwrite=false) |
|
231 | - { |
|
232 | - if (! is_array($data)) |
|
233 | - { |
|
234 | - $data = array($data); |
|
235 | - } |
|
236 | - |
|
237 | - $content = $this->render($template, $data); |
|
238 | - |
|
239 | - return $this->createFile($destination, $content, $overwrite); |
|
240 | - } |
|
241 | - |
|
242 | - //-------------------------------------------------------------------- |
|
243 | - |
|
244 | - |
|
245 | - /** |
|
246 | - * Injects a block of code into an existing file. Using options |
|
247 | - * you can specify where the code should be inserted. Available options |
|
248 | - * are: |
|
249 | - * prepend - Place at beginning of file |
|
250 | - * append - Place at end of file |
|
251 | - * before => '' - Insert just prior to this line of text (don't forget the line ending "\n") |
|
252 | - * after => '' - Insert just after this line of text (don't forget the line ending "\n") |
|
253 | - * replace => '' - a simple string to be replaced. All locations will be replaced. |
|
254 | - * regex => '' - a pregex pattern to use to replace all locations. |
|
255 | - * |
|
256 | - * @param $path |
|
257 | - * @param $content |
|
258 | - * @param array $options |
|
259 | - * |
|
260 | - * @return $this |
|
261 | - */ |
|
262 | - public function injectIntoFile($path, $content, $options='append') |
|
263 | - { |
|
264 | - $kit = new FileKit(); |
|
265 | - |
|
266 | - if (is_string($options)) |
|
267 | - { |
|
268 | - $action = $options; |
|
269 | - } |
|
270 | - else if (is_array($options) && count($options)) |
|
271 | - { |
|
272 | - $keys = array_keys($options); |
|
273 | - $action = array_shift( $keys ); |
|
274 | - $param = $options[$action]; |
|
275 | - } |
|
276 | - |
|
277 | - switch ( strtolower($action) ) |
|
278 | - { |
|
279 | - case 'prepend': |
|
280 | - $success = $kit->prepend($path, $content); |
|
281 | - break; |
|
282 | - case 'before': |
|
283 | - $success = $kit->before($path, $param, $content); |
|
284 | - break; |
|
285 | - case 'after': |
|
286 | - $success = $kit->after($path, $param, $content); |
|
287 | - break; |
|
288 | - case 'replace': |
|
289 | - $success = $kit->replaceIn($path, $param, $content); |
|
290 | - break; |
|
291 | - case 'regex': |
|
292 | - $success = $kit->replaceWithRegex($path, $param, $content); |
|
293 | - break; |
|
294 | - case 'append': |
|
295 | - default: |
|
296 | - $success = $kit->append($path, $content); |
|
297 | - break; |
|
298 | - } |
|
299 | - |
|
300 | - if ($success) |
|
301 | - { |
|
302 | - CLI::write( CLI::color("\t". strtolower( lang('modified') ) ." ", 'cyan') . str_replace(APPPATH, '', $path ) ); |
|
303 | - } |
|
304 | - else |
|
305 | - { |
|
306 | - CLI::write( CLI::color("\t". strtolower( lang('error') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
307 | - } |
|
308 | - |
|
309 | - return $this; |
|
310 | - } |
|
165 | + //-------------------------------------------------------------------- |
|
166 | + |
|
167 | + /** |
|
168 | + * Creates a new directory at the specified path. |
|
169 | + * |
|
170 | + * @param $path |
|
171 | + * @param int|string $perms |
|
172 | + * |
|
173 | + * @return bool |
|
174 | + */ |
|
175 | + public function createDirectory($path, $perms=0755) |
|
176 | + { |
|
177 | + $path = $this->sandbox($path); |
|
178 | + |
|
179 | + if (is_dir($path)) |
|
180 | + { |
|
181 | + return $this; |
|
182 | + } |
|
183 | + |
|
184 | + if (! mkdir($path, $perms, true) ) |
|
185 | + { |
|
186 | + throw new \RuntimeException( sprintf( lang('errors.creating_dir'), $path) ); |
|
187 | + } |
|
188 | + |
|
189 | + return $this; |
|
190 | + } |
|
191 | + |
|
192 | + //-------------------------------------------------------------------- |
|
193 | + |
|
194 | + /** |
|
195 | + * Copies a file from the current template group to the destination. |
|
196 | + * |
|
197 | + * @param $source |
|
198 | + * @param $destination |
|
199 | + * @param bool $overwrite |
|
200 | + * |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function copyFile($source, $destination, $overwrite=false) |
|
204 | + { |
|
205 | + $source = $this->sandbox($source); |
|
206 | + |
|
207 | + if (! file_exists($source)) |
|
208 | + { |
|
209 | + return null; |
|
210 | + } |
|
211 | + |
|
212 | + $content = file_get_contents($source); |
|
213 | + |
|
214 | + return $this->createFile($destination, $content, $overwrite); |
|
215 | + } |
|
216 | + |
|
217 | + //-------------------------------------------------------------------- |
|
218 | + |
|
219 | + /** |
|
220 | + * Attempts to locate a template within the current template group, |
|
221 | + * parses it with the passed in data, and writes to the new location. |
|
222 | + * |
|
223 | + * @param $template |
|
224 | + * @param $destination |
|
225 | + * @param array $data |
|
226 | + * @param bool $overwrite |
|
227 | + * |
|
228 | + * @return $this |
|
229 | + */ |
|
230 | + public function copyTemplate($template, $destination, $data=[], $overwrite=false) |
|
231 | + { |
|
232 | + if (! is_array($data)) |
|
233 | + { |
|
234 | + $data = array($data); |
|
235 | + } |
|
236 | + |
|
237 | + $content = $this->render($template, $data); |
|
238 | + |
|
239 | + return $this->createFile($destination, $content, $overwrite); |
|
240 | + } |
|
241 | + |
|
242 | + //-------------------------------------------------------------------- |
|
243 | + |
|
244 | + |
|
245 | + /** |
|
246 | + * Injects a block of code into an existing file. Using options |
|
247 | + * you can specify where the code should be inserted. Available options |
|
248 | + * are: |
|
249 | + * prepend - Place at beginning of file |
|
250 | + * append - Place at end of file |
|
251 | + * before => '' - Insert just prior to this line of text (don't forget the line ending "\n") |
|
252 | + * after => '' - Insert just after this line of text (don't forget the line ending "\n") |
|
253 | + * replace => '' - a simple string to be replaced. All locations will be replaced. |
|
254 | + * regex => '' - a pregex pattern to use to replace all locations. |
|
255 | + * |
|
256 | + * @param $path |
|
257 | + * @param $content |
|
258 | + * @param array $options |
|
259 | + * |
|
260 | + * @return $this |
|
261 | + */ |
|
262 | + public function injectIntoFile($path, $content, $options='append') |
|
263 | + { |
|
264 | + $kit = new FileKit(); |
|
265 | + |
|
266 | + if (is_string($options)) |
|
267 | + { |
|
268 | + $action = $options; |
|
269 | + } |
|
270 | + else if (is_array($options) && count($options)) |
|
271 | + { |
|
272 | + $keys = array_keys($options); |
|
273 | + $action = array_shift( $keys ); |
|
274 | + $param = $options[$action]; |
|
275 | + } |
|
276 | + |
|
277 | + switch ( strtolower($action) ) |
|
278 | + { |
|
279 | + case 'prepend': |
|
280 | + $success = $kit->prepend($path, $content); |
|
281 | + break; |
|
282 | + case 'before': |
|
283 | + $success = $kit->before($path, $param, $content); |
|
284 | + break; |
|
285 | + case 'after': |
|
286 | + $success = $kit->after($path, $param, $content); |
|
287 | + break; |
|
288 | + case 'replace': |
|
289 | + $success = $kit->replaceIn($path, $param, $content); |
|
290 | + break; |
|
291 | + case 'regex': |
|
292 | + $success = $kit->replaceWithRegex($path, $param, $content); |
|
293 | + break; |
|
294 | + case 'append': |
|
295 | + default: |
|
296 | + $success = $kit->append($path, $content); |
|
297 | + break; |
|
298 | + } |
|
299 | + |
|
300 | + if ($success) |
|
301 | + { |
|
302 | + CLI::write( CLI::color("\t". strtolower( lang('modified') ) ." ", 'cyan') . str_replace(APPPATH, '', $path ) ); |
|
303 | + } |
|
304 | + else |
|
305 | + { |
|
306 | + CLI::write( CLI::color("\t". strtolower( lang('error') ) ." ", 'light_red') . str_replace(APPPATH, '', $path ) ); |
|
307 | + } |
|
308 | + |
|
309 | + return $this; |
|
310 | + } |
|
311 | 311 | |
312 | - //-------------------------------------------------------------------- |
|
312 | + //-------------------------------------------------------------------- |
|
313 | 313 | |
314 | 314 | /** |
315 | 315 | * Runs another generator. The first parameter is the name of the |
@@ -320,130 +320,130 @@ discard block |
||
320 | 320 | * @param $command |
321 | 321 | * @param null $options |
322 | 322 | */ |
323 | - public function generate($command, $options = '', $quiet=false) |
|
324 | - { |
|
323 | + public function generate($command, $options = '', $quiet=false) |
|
324 | + { |
|
325 | 325 | $orig_options = CLI::optionString(); |
326 | - $options = $orig_options .' '. $options; |
|
327 | - |
|
328 | - if ($quiet === true) |
|
329 | - { |
|
330 | - $options .= ' -quiet'; |
|
331 | - } |
|
332 | - |
|
333 | - if ($this->overwrite === true) |
|
334 | - { |
|
335 | - $options .= ' -overwrite'; |
|
336 | - } |
|
337 | - |
|
338 | - passthru( "php sprint forge {$command} {$options}" ); |
|
339 | - } |
|
340 | - |
|
341 | - //-------------------------------------------------------------------- |
|
342 | - |
|
343 | - /** |
|
344 | - * Adds a new route to the application's route file. |
|
345 | - * |
|
346 | - * @param $left |
|
347 | - * @param $right |
|
348 | - * |
|
349 | - * @return \Myth\Forge\BaseGenerator |
|
350 | - */ |
|
351 | - public function route($left, $right, $options=[], $method='any') |
|
352 | - { |
|
353 | - $option_str = '['; |
|
354 | - |
|
355 | - foreach ($options as $key => $value) |
|
356 | - { |
|
357 | - $option_str .= ""; |
|
358 | - } |
|
359 | - |
|
360 | - $option_str .= ']'; |
|
361 | - |
|
362 | - $content = "\$routes->{$method}('{$left}', '{$right}', {$option_str});\n"; |
|
363 | - |
|
364 | - return $this->injectIntoFile(APPPATH .'config/routes.php', $content, ['after' => "// Auto-generated routes go here\n"]); |
|
365 | - } |
|
326 | + $options = $orig_options .' '. $options; |
|
327 | + |
|
328 | + if ($quiet === true) |
|
329 | + { |
|
330 | + $options .= ' -quiet'; |
|
331 | + } |
|
332 | + |
|
333 | + if ($this->overwrite === true) |
|
334 | + { |
|
335 | + $options .= ' -overwrite'; |
|
336 | + } |
|
337 | + |
|
338 | + passthru( "php sprint forge {$command} {$options}" ); |
|
339 | + } |
|
340 | + |
|
341 | + //-------------------------------------------------------------------- |
|
342 | + |
|
343 | + /** |
|
344 | + * Adds a new route to the application's route file. |
|
345 | + * |
|
346 | + * @param $left |
|
347 | + * @param $right |
|
348 | + * |
|
349 | + * @return \Myth\Forge\BaseGenerator |
|
350 | + */ |
|
351 | + public function route($left, $right, $options=[], $method='any') |
|
352 | + { |
|
353 | + $option_str = '['; |
|
354 | + |
|
355 | + foreach ($options as $key => $value) |
|
356 | + { |
|
357 | + $option_str .= ""; |
|
358 | + } |
|
359 | + |
|
360 | + $option_str .= ']'; |
|
361 | + |
|
362 | + $content = "\$routes->{$method}('{$left}', '{$right}', {$option_str});\n"; |
|
363 | + |
|
364 | + return $this->injectIntoFile(APPPATH .'config/routes.php', $content, ['after' => "// Auto-generated routes go here\n"]); |
|
365 | + } |
|
366 | 366 | |
367 | - //-------------------------------------------------------------------- |
|
367 | + //-------------------------------------------------------------------- |
|
368 | 368 | |
369 | - /** |
|
370 | - * Outputs the contents of the file in the template's source path. |
|
371 | - */ |
|
372 | - public function readme($file='readme.txt') |
|
373 | - { |
|
374 | - $name = str_replace('Generator', '', get_class($this)); |
|
369 | + /** |
|
370 | + * Outputs the contents of the file in the template's source path. |
|
371 | + */ |
|
372 | + public function readme($file='readme.txt') |
|
373 | + { |
|
374 | + $name = str_replace('Generator', '', get_class($this)); |
|
375 | 375 | |
376 | - $path = $this->locateGenerator($name); |
|
376 | + $path = $this->locateGenerator($name); |
|
377 | 377 | |
378 | - if (! file_exists($path . $file)) |
|
379 | - { |
|
380 | - CLI::error(sprintf( lang('forge.cant_find_readme'), $file) ); |
|
381 | - } |
|
378 | + if (! file_exists($path . $file)) |
|
379 | + { |
|
380 | + CLI::error(sprintf( lang('forge.cant_find_readme'), $file) ); |
|
381 | + } |
|
382 | 382 | |
383 | - $contents = file_get_contents($path . $file); |
|
383 | + $contents = file_get_contents($path . $file); |
|
384 | 384 | |
385 | - CLI::new_line(2); |
|
386 | - CLI::write( CLI::wrap($contents), 'green' ); |
|
387 | - CLI::new_line(); |
|
388 | - } |
|
385 | + CLI::new_line(2); |
|
386 | + CLI::write( CLI::wrap($contents), 'green' ); |
|
387 | + CLI::new_line(); |
|
388 | + } |
|
389 | 389 | |
390 | - //-------------------------------------------------------------------- |
|
390 | + //-------------------------------------------------------------------- |
|
391 | 391 | |
392 | - /** |
|
393 | - * Renders a single generator template. The file must be in a folder |
|
394 | - * under the template group named the same as $this->generator_name. |
|
395 | - * The file must have a '.tpl.php' file extension. |
|
396 | - * |
|
397 | - * @param $template_name |
|
398 | - * @param array $data |
|
399 | - * |
|
400 | - * @return string The rendered template |
|
401 | - */ |
|
402 | - public function render($template_name, $data=[], $folder=null) |
|
403 | - { |
|
404 | - if (empty($this->themer)) |
|
405 | - { |
|
406 | - $this->setupThemer(); |
|
407 | - } |
|
392 | + /** |
|
393 | + * Renders a single generator template. The file must be in a folder |
|
394 | + * under the template group named the same as $this->generator_name. |
|
395 | + * The file must have a '.tpl.php' file extension. |
|
396 | + * |
|
397 | + * @param $template_name |
|
398 | + * @param array $data |
|
399 | + * |
|
400 | + * @return string The rendered template |
|
401 | + */ |
|
402 | + public function render($template_name, $data=[], $folder=null) |
|
403 | + { |
|
404 | + if (empty($this->themer)) |
|
405 | + { |
|
406 | + $this->setupThemer(); |
|
407 | + } |
|
408 | 408 | |
409 | - $data['uikit'] = $this->loadUIKit(); |
|
409 | + $data['uikit'] = $this->loadUIKit(); |
|
410 | 410 | |
411 | - $output = null; |
|
411 | + $output = null; |
|
412 | 412 | |
413 | - $view = $template_name .'.tpl'; |
|
413 | + $view = $template_name .'.tpl'; |
|
414 | 414 | |
415 | - $groups = config_item('forge.collections'); |
|
415 | + $groups = config_item('forge.collections'); |
|
416 | 416 | |
417 | - $name = str_replace('Generator', '', get_class($this) ); |
|
417 | + $name = str_replace('Generator', '', get_class($this) ); |
|
418 | 418 | |
419 | - foreach ($groups as $group => $path) |
|
420 | - { |
|
421 | - $path = rtrim($path, '/ ') .'/'; |
|
422 | - $folders = scandir($path); |
|
419 | + foreach ($groups as $group => $path) |
|
420 | + { |
|
421 | + $path = rtrim($path, '/ ') .'/'; |
|
422 | + $folders = scandir($path); |
|
423 | 423 | |
424 | - if (! $i = array_search(ucfirst($name), $folders)) |
|
425 | - { |
|
426 | - continue; |
|
427 | - } |
|
424 | + if (! $i = array_search(ucfirst($name), $folders)) |
|
425 | + { |
|
426 | + continue; |
|
427 | + } |
|
428 | 428 | |
429 | - $view = $folders[$i] . '/'. $view; |
|
429 | + $view = $folders[$i] . '/'. $view; |
|
430 | 430 | |
431 | - if (realpath($path . $view .'.php')) |
|
432 | - { |
|
433 | - $output = $this->themer->display($group .':'. $view, $data); |
|
434 | - break; |
|
435 | - } |
|
436 | - } |
|
431 | + if (realpath($path . $view .'.php')) |
|
432 | + { |
|
433 | + $output = $this->themer->display($group .':'. $view, $data); |
|
434 | + break; |
|
435 | + } |
|
436 | + } |
|
437 | 437 | |
438 | - // To allow for including any PHP code in the templates, |
|
439 | - // replace any '@php' and '@=' tags with their correct PHP syntax. |
|
440 | - $output = str_replace('@php', '<?php', $output); |
|
441 | - $output = str_replace('@=', '<?=', $output); |
|
438 | + // To allow for including any PHP code in the templates, |
|
439 | + // replace any '@php' and '@=' tags with their correct PHP syntax. |
|
440 | + $output = str_replace('@php', '<?php', $output); |
|
441 | + $output = str_replace('@=', '<?=', $output); |
|
442 | 442 | |
443 | - return $output; |
|
444 | - } |
|
443 | + return $output; |
|
444 | + } |
|
445 | 445 | |
446 | - //-------------------------------------------------------------------- |
|
446 | + //-------------------------------------------------------------------- |
|
447 | 447 | |
448 | 448 | /** |
449 | 449 | * Forces a path to exist within the current application's folder. |
@@ -458,7 +458,7 @@ discard block |
||
458 | 458 | */ |
459 | 459 | public function sandbox($path) |
460 | 460 | { |
461 | - $path = $this->normalizePath($path); |
|
461 | + $path = $this->normalizePath($path); |
|
462 | 462 | |
463 | 463 | // If it's writing to BASEPATH - FIX IT |
464 | 464 | if (strpos($path, $this->normalizePath(BASEPATH) ) === 0) |
@@ -478,37 +478,37 @@ discard block |
||
478 | 478 | return $path; |
479 | 479 | } |
480 | 480 | |
481 | - return APPPATH . $path; |
|
481 | + return APPPATH . $path; |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | //-------------------------------------------------------------------- |
485 | 485 | |
486 | 486 | |
487 | 487 | |
488 | - //-------------------------------------------------------------------- |
|
489 | - // Private Methods |
|
490 | - //-------------------------------------------------------------------- |
|
488 | + //-------------------------------------------------------------------- |
|
489 | + // Private Methods |
|
490 | + //-------------------------------------------------------------------- |
|
491 | 491 | |
492 | - protected function setupThemer() |
|
493 | - { |
|
494 | - $themer_name = config_item('forge.themer'); |
|
492 | + protected function setupThemer() |
|
493 | + { |
|
494 | + $themer_name = config_item('forge.themer'); |
|
495 | 495 | |
496 | - if (! $themer_name) |
|
497 | - { |
|
498 | - throw new \RuntimeException( lang('forge.no_themer') ); |
|
499 | - } |
|
496 | + if (! $themer_name) |
|
497 | + { |
|
498 | + throw new \RuntimeException( lang('forge.no_themer') ); |
|
499 | + } |
|
500 | 500 | |
501 | - $this->themer = new $themer_name( get_instance() ); |
|
501 | + $this->themer = new $themer_name( get_instance() ); |
|
502 | 502 | |
503 | - // Register our paths with the themer |
|
504 | - $paths = config_item('forge.collections'); |
|
503 | + // Register our paths with the themer |
|
504 | + $paths = config_item('forge.collections'); |
|
505 | 505 | |
506 | - foreach ($paths as $key => $path) { |
|
507 | - $this->themer->addThemePath($key, $path); |
|
508 | - } |
|
509 | - } |
|
506 | + foreach ($paths as $key => $path) { |
|
507 | + $this->themer->addThemePath($key, $path); |
|
508 | + } |
|
509 | + } |
|
510 | 510 | |
511 | - //-------------------------------------------------------------------- |
|
511 | + //-------------------------------------------------------------------- |
|
512 | 512 | |
513 | 513 | public function loadUIKit() |
514 | 514 | { |
@@ -521,7 +521,7 @@ discard block |
||
521 | 521 | |
522 | 522 | $uikit = new $kit_name(); |
523 | 523 | |
524 | - return $uikit; |
|
524 | + return $uikit; |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | //-------------------------------------------------------------------- |
@@ -639,66 +639,66 @@ discard block |
||
639 | 639 | |
640 | 640 | //-------------------------------------------------------------------- |
641 | 641 | |
642 | - /** |
|
643 | - * Normalizes a path and cleans it up for healthy use within |
|
644 | - * realpath() and helps to mitigate changes between Windows and *nix |
|
645 | - * operating systems. |
|
646 | - * |
|
647 | - * Found at http://php.net/manual/en/function.realpath.php#112367 |
|
648 | - * |
|
649 | - * @param $path |
|
650 | - * |
|
651 | - * @return string |
|
652 | - */ |
|
653 | - protected function normalizePath($path) |
|
654 | - { |
|
655 | - // Array to build a new path from the good parts |
|
656 | - $parts = array(); |
|
657 | - |
|
658 | - // Replace backslashes with forward slashes |
|
659 | - $path = str_replace('\\', '/', $path); |
|
660 | - |
|
661 | - // Combine multiple slashes into a single slash |
|
662 | - $path = preg_replace('/\/+/', '/', $path); |
|
663 | - |
|
664 | - // Collect path segments |
|
665 | - $segments = explode('/', $path); |
|
666 | - |
|
667 | - // Initialize testing variable |
|
668 | - $test = ''; |
|
669 | - |
|
670 | - foreach($segments as $segment) |
|
671 | - { |
|
672 | - if($segment != '.') |
|
673 | - { |
|
674 | - $test = array_pop($parts); |
|
675 | - |
|
676 | - if(is_null($test)) |
|
677 | - { |
|
678 | - $parts[] = $segment; |
|
679 | - } |
|
680 | - else if ($segment == '..') |
|
681 | - { |
|
682 | - if ($test == '..') |
|
683 | - { |
|
684 | - $parts[] = $test; |
|
685 | - } |
|
686 | - |
|
687 | - if ($test == '..' || $test == '') |
|
688 | - { |
|
689 | - $parts[] = $segment; |
|
690 | - } |
|
691 | - } |
|
692 | - else |
|
693 | - { |
|
694 | - $parts[] = $test; |
|
695 | - $parts[] = $segment; |
|
696 | - } |
|
697 | - } |
|
698 | - } |
|
699 | - return implode('/', $parts); |
|
700 | - } |
|
701 | - |
|
702 | - //-------------------------------------------------------------------- |
|
642 | + /** |
|
643 | + * Normalizes a path and cleans it up for healthy use within |
|
644 | + * realpath() and helps to mitigate changes between Windows and *nix |
|
645 | + * operating systems. |
|
646 | + * |
|
647 | + * Found at http://php.net/manual/en/function.realpath.php#112367 |
|
648 | + * |
|
649 | + * @param $path |
|
650 | + * |
|
651 | + * @return string |
|
652 | + */ |
|
653 | + protected function normalizePath($path) |
|
654 | + { |
|
655 | + // Array to build a new path from the good parts |
|
656 | + $parts = array(); |
|
657 | + |
|
658 | + // Replace backslashes with forward slashes |
|
659 | + $path = str_replace('\\', '/', $path); |
|
660 | + |
|
661 | + // Combine multiple slashes into a single slash |
|
662 | + $path = preg_replace('/\/+/', '/', $path); |
|
663 | + |
|
664 | + // Collect path segments |
|
665 | + $segments = explode('/', $path); |
|
666 | + |
|
667 | + // Initialize testing variable |
|
668 | + $test = ''; |
|
669 | + |
|
670 | + foreach($segments as $segment) |
|
671 | + { |
|
672 | + if($segment != '.') |
|
673 | + { |
|
674 | + $test = array_pop($parts); |
|
675 | + |
|
676 | + if(is_null($test)) |
|
677 | + { |
|
678 | + $parts[] = $segment; |
|
679 | + } |
|
680 | + else if ($segment == '..') |
|
681 | + { |
|
682 | + if ($test == '..') |
|
683 | + { |
|
684 | + $parts[] = $test; |
|
685 | + } |
|
686 | + |
|
687 | + if ($test == '..' || $test == '') |
|
688 | + { |
|
689 | + $parts[] = $segment; |
|
690 | + } |
|
691 | + } |
|
692 | + else |
|
693 | + { |
|
694 | + $parts[] = $test; |
|
695 | + $parts[] = $segment; |
|
696 | + } |
|
697 | + } |
|
698 | + } |
|
699 | + return implode('/', $parts); |
|
700 | + } |
|
701 | + |
|
702 | + //-------------------------------------------------------------------- |
|
703 | 703 | |
704 | 704 | } |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | */ |
164 | 164 | public function detectAction($name) |
165 | 165 | { |
166 | - $segments = explode('_', $name); |
|
166 | + $segments = explode('_', $name); |
|
167 | 167 | |
168 | 168 | $action = trim(strtolower(array_shift($segments))); |
169 | 169 | |
@@ -236,10 +236,10 @@ discard block |
||
236 | 236 | */ |
237 | 237 | public function parseFields($str) |
238 | 238 | { |
239 | - if (empty($str)) |
|
240 | - { |
|
241 | - return; |
|
242 | - } |
|
239 | + if (empty($str)) |
|
240 | + { |
|
241 | + return; |
|
242 | + } |
|
243 | 243 | |
244 | 244 | $fields = []; |
245 | 245 | $segments = explode(' ', $str); |