1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Sites;
13
14 use ICanBoogie\Errors;
15 use ICanBoogie\I18n\FormattedString;
16
17 use Icybee\Modules\Sites\Site;
18
19 20 21
22 class StatusOperation extends \ICanBoogie\Operation
23 {
24 protected function get_controls()
25 {
26 return array
27 (
28 self::CONTROL_PERMISSION => Module::PERMISSION_ADMINISTER
29 )
30
31 + parent::get_controls();
32 }
33
34 protected function validate(Errors $errors)
35 {
36 if ($this->request->is_put)
37 {
38 $status = $this->request['status'];
39
40 if ($status === null || !in_array($status, array(Site::STATUS_OK, Site::STATUS_UNAVAILABLE, Site::STATUS_UNAUTHORIZED, Site::STATUS_NOT_FOUND)))
41 {
42 throw new \InvalidArgumentException('Invalid status value.');
43 }
44 }
45
46 return true;
47 }
48
49 protected function process()
50 {
51 static $status_names = array
52 (
53 Site::STATUS_OK => 'ok (online)',
54 Site::STATUS_UNAVAILABLE => 'unavailable',
55 Site::STATUS_UNAUTHORIZED => 'unauthorized',
56 Site::STATUS_NOT_FOUND => 'not found (offline)'
57 );
58
59 if ($this->request->is_put)
60 {
61 $status = $this->request['status'];
62
63 $record = $this->record;
64 $record->status = $status;
65 $record->save();
66
67 $this->response->message = new FormattedString('The site %title is now ' . $status_names[$status] . '.', array('title' => $record->title));
68 }
69
70 return $this->record->status;
71 }
72 }