1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee;
13
14 use ICanBoogie\AuthenticationRequired;
15 use ICanBoogie\I18n;
16
17 18 19 20 21 22
23 class Module extends \ICanBoogie\Module
24 {
25 const OPERATION_CONFIG = 'config';
26
27 28 29 30 31 32 33 34 35 36
37 protected function lazy_get_views()
38 {
39 return array();
40 }
41
42 public function getBlock($name)
43 {
44 global $core;
45
46 $args = func_get_args();
47
48 $class_name = $this->resolve_block_class($name);
49
50 if ($class_name)
51 {
52 array_shift($args);
53
54 I18n::push_scope($this->flat_id);
55 I18n::push_scope($this->flat_id . '.' . $name);
56
57 try
58 {
59 $block = new $class_name($this, array(), $args);
60
61
62 }
63 catch (\ICanBoogie\SecurityException $e)
64 {
65 I18n::pop_scope();
66 I18n::pop_scope();
67
68 throw $e;
69 }
70 catch (\Exception $e)
71 {
72 $block = \ICanBoogie\Debug::format_alert($e);
73 }
74
75 I18n::pop_scope();
76 I18n::pop_scope();
77
78 return $block;
79 }
80
81
82
83 return call_user_func_array((PHP_MAJOR_VERSION > 5 || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION > 2)) ? 'parent::' . __FUNCTION__ : array($this, 'parent::' . __FUNCTION__), $args);
84 }
85
86 protected function resolve_block_class($name)
87 {
88 $module = $this;
89 $class_name = \ICanBoogie\camelize(\ICanBoogie\underscore($name)) . 'Block';
90
91 while ($module)
92 {
93 $try = $module->descriptor[self::T_NAMESPACE] . '\\' . $class_name;
94
95 if (class_exists($try, true))
96 {
97 return $try;
98 }
99
100 $module = $module->parent;
101 }
102 }
103
104 private function create_activerecord_lock_name($key)
105 {
106 return "activerecord_locks.$this->flat_id.$key";
107 }
108
109 110 111 112 113 114 115 116
117 public function lock_entry($key, &$lock=null)
118 {
119 global $core;
120
121 $user_id = $core->user_id;
122
123 if (!$user_id)
124 {
125 throw new AuthenticationRequired();
126 }
127
128 if (!$key)
129 {
130 throw new \InvalidArgumentException('The record key is required.');
131 }
132
133
134
135
136 $registry = $core->registry;
137
138 $lock_name = $this->create_activerecord_lock_name($key);
139 $lock = json_decode($registry[$lock_name], true);
140 $lock_uid = $user_id;
141 $lock_until = null;
142
143 $now = time();
144 $until = date('Y-m-d H:i:s', $now + 2 * 60);
145
146 if ($lock)
147 {
148 $lock_uid = $lock['uid'];
149 $lock_until = $lock['until'];
150
151 if ($now > strtotime($lock_until))
152 {
153
154
155
156
157 $lock_uid = $user_id;
158 }
159 else if ($lock_uid != $user_id)
160 {
161 return false;
162 }
163 }
164
165 $lock = array
166 (
167 'uid' => $lock_uid,
168 'until' => $until
169 );
170
171 $registry[$lock_name] = json_encode($lock);
172
173 return true;
174 }
175
176 public function unlock_entry($key)
177 {
178 global $core;
179
180 $registry = $core->registry;
181
182 $lock_name = $this->create_activerecord_lock_name($key);
183 $lock = json_decode($registry[$lock_name], true);
184
185 if (!$lock)
186 {
187 return;
188 }
189
190 if ($lock['uid'] != $core->user_id)
191 {
192 return false;
193 }
194
195 unset($registry[$lock_name]);
196
197 return true;
198 }
199 }