-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveRoom.module
94 lines (66 loc) · 2.56 KB
/
SaveRoom.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php namespace ProcessWire;
/**
* Save Room Module
*
* Adds hooks to handle all the necessary steps that are needed to
* save the Room template
*
* @attention: Please look into SaveSensor.module for more detailed comments.
*/
class SaveRoom extends WireData implements Module {
/**
* getModuleInfo required by module interface
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Save actions for Room template',
'version' => "0.1.1",
'summary' => 'Simple module to manage save actions on template Room.',
'singular' => true,
'autoload' => true,
//'icon' => 'smile-o',
);
}
/**
* Initialize the module
*/
public function init() {
// add a hook after the $pages->save, to issue hook every time a page is saved
$this->pages->addHookAfter('saveReady', $this, 'calculateRoom');
}
/**
* Hook for saving template Room
*
* @param HookEvent $event
*/
public function calculateRoom($event) {
$page = $event->arguments(0);
$settings= wire('pages')->get('/settings/');
// return if not relevant
if($page->template->name !== 'Room') return;
// ////////////////////
// set default values
if (empty($page->height)) $page->height = $settings->height;
if (empty($page->co2_alarm_level)) $page->co2_alarm_level = $settings->co2_alarm_level;
if (empty($page->co2_warn_level)) $page->co2_warn_level = $settings->co2_warn_level;
if (empty($page->temp_max_level)) $page->temp_max_level = $settings->temp_max_level;
if (empty($page->temp_min_level)) $page->temp_min_level = $settings->temp_min_level;
// ////////////////////
// Do Checks
if ($page->co2_alarm_level < $page->co2_warn_level){
$this->error("CO2 alarm level is bigger than CO2 warn level");
}
if ($page->person_curren < $page->person_max){
$this->error("Too many people in the room");
}
// ////////////////////
// calculate fields
// As cubic meters is a Decimal field it cuts of the unneeded digits automatically.
$page->cubic_meters = $page->square_meters * $page->height;
// If there are no sensors , Instructors , you cannot activate the room
if ($page->room_instructor->count() == 0) $page->active = 0;
if ($page->room_sensors->count() == 0) $page->active = 0;
if (!$page->active) $this->warning("ATTENTION: Room not active! You need an instructor and a sensor");
}
}