1: <?php
2:
3: /**
4: * This file contains the inuse collection and item class.
5: *
6: * @package Core
7: * @subpackage GenericDB_Model
8: * @author Timo Hummel
9: * @copyright four for business AG <www.4fb.de>
10: * @license http://www.contenido.org/license/LIZENZ.txt
11: * @link http://www.4fb.de
12: * @link http://www.contenido.org
13: */
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: /**
18: * Class InUse Class for In-Use management
19: *
20: * @package Core
21: * @subpackage GenericDB_Model
22: */
23: class cApiInUseCollection extends ItemCollection {
24: /**
25: * Constructor to create an instance of this class.
26: *
27: * @throws cInvalidArgumentException
28: */
29: public function __construct() {
30: global $cfg;
31: parent::__construct($cfg['tab']['inuse'], 'idinuse');
32: $this->_setItemClass('cApiInUse');
33: }
34:
35: /**
36: * Marks a specific object as "in use". Note that items are released when
37: * the session is destroyed. Currently, the following types are defined and
38: * approved as internal CONTENIDO standard: - article - module - layout -
39: * template
40: *
41: * @param string $type
42: * Specifies the type to mark.
43: * @param mixed $objectid
44: * Specifies the object ID
45: * @param string $session
46: * Specifies the session for which the "in use" mark is valid
47: * @param string $user
48: * Specifies the user which requested the in-use flag
49: *
50: * @return cApiInUse|NULL
51: * @throws cDbException
52: * @throws cException
53: * @throws cInvalidArgumentException
54: */
55: public function markInUse($type, $objectid, $session, $user) {
56: $type = $type;
57: $objectid = $objectid;
58: $session = $session;
59: $user = $user;
60:
61: $this->select("type='" . $this->escape($type) . "' AND objectid='" . $this->escape($objectid) . "'");
62:
63: $newitem = NULL;
64: if (!$this->next()) {
65: $newitem = $this->createNewItem();
66: $newitem->set('type', $type);
67: $newitem->set('objectid', $objectid);
68: $newitem->set('session', $session);
69: $newitem->set('userid', $user);
70: $newitem->set('timestamp', time());
71: $newitem->store();
72: }
73: return $newitem;
74: }
75:
76: /**
77: * Removes the "in use" mark from a specific object.
78: *
79: * @param string $type
80: * Specifies the type to de-mark.
81: * @param mixed $objectid
82: * Specifies the object ID
83: * @param string $session
84: * Specifies the session for which the "in use" mark is valid
85: *
86: * @throws cDbException
87: * @throws cException
88: * @throws cInvalidArgumentException
89: */
90: public function removeMark($type, $objectid, $session) {
91: $type = $this->escape($type);
92: $objectid = $this->escape($objectid);
93: $session = $this->escape($session);
94:
95: $this->select("type='" . $type . "' AND objectid='" . $objectid . "' AND session='" . $session . "'");
96:
97: if (($obj = $this->next()) !== false) {
98: // Remove entry
99: $this->delete($obj->get('idinuse'));
100: unset($obj);
101: }
102: }
103:
104: /**
105: * Removes all marks for a specific type and session
106: *
107: * @param string $type
108: * Specifies the type to de-mark.
109: * @param string $session
110: * Specifies the session for which the "in use" mark is valid
111: *
112: * @throws cDbException
113: * @throws cException
114: * @throws cInvalidArgumentException
115: */
116: public function removeTypeMarks($type, $session) {
117: $type = $this->escape($type);
118: $session = $this->escape($session);
119:
120: $this->select("type='" . $type . "' AND session='" . $session . "'");
121:
122: while (($obj = $this->next()) !== false) {
123: // Remove entry
124: $this->delete($obj->get('idinuse'));
125: unset($obj);
126: }
127: }
128:
129: /**
130: * Removes the mark for a specific item
131: *
132: * @param string $type
133: * Specifies the type to de-mark.
134: * @param string $itemid
135: * Specifies the item
136: *
137: * @throws cDbException
138: * @throws cException
139: * @throws cInvalidArgumentException
140: */
141: public function removeItemMarks($type, $itemid) {
142: $type = $this->escape($type);
143: $itemid = $this->escape($itemid);
144:
145: $this->select("type='" . $type . "' AND objectid='" . $itemid . "'");
146:
147: while (($obj = $this->next()) !== false) {
148: // Remove entry
149: $this->delete($obj->get('idinuse'));
150: unset($obj);
151: }
152: }
153:
154: /**
155: * Removes all in-use marks for a specific userId
156: *
157: * @param string $userId
158: * Specifies the user
159: *
160: * @throws cDbException
161: * @throws cException
162: * @throws cInvalidArgumentException
163: */
164: public function removeUserMarks($userId) {
165: $userId = $this->escape($userId);
166: $this->select("userid='" . $userId . "'");
167:
168: while (($obj = $this->next()) !== false) {
169: // Remove entry
170: $this->delete($obj->get('idinuse'));
171: unset($obj);
172: }
173: }
174:
175: /**
176: * Removes all inuse entries which are older than the inuse timeout
177: *
178: * @throws cDbException
179: * @throws cException
180: */
181: public function removeOldMarks() {
182: $cfg = cRegistry::getConfig();
183: $expire = time() - $cfg['inuse']['lifetime'];
184:
185: $this->select("timestamp < " . $expire);
186:
187: while (($obj = $this->next()) !== false) {
188: // Remove entry
189: $this->delete($obj->get('idinuse'));
190: unset($obj);
191: }
192: }
193:
194: /**
195: * Removes all in-use marks for a specific session.
196: *
197: * @param string $session
198: * Specifies the session for which the "in use" marks should be removed
199: *
200: * @throws cDbException
201: * @throws cException
202: * @throws cInvalidArgumentException
203: */
204: public function removeSessionMarks($session) {
205: $session = $this->escape($session);
206: $this->select("session='" . $session . "'");
207:
208: while (($obj = $this->next()) !== false) {
209: // Remove entry
210: $this->delete($obj->get('idinuse'));
211: unset($obj);
212: }
213: }
214:
215: /**
216: * Checks if a specific item is marked
217: *
218: * @param string $type
219: * Specifies the type to de-mark.
220: * @param mixed $objectid
221: * Specifies the object ID
222: * @return cApiInUse bool
223: * false if it's not in use or returns the object if it is.
224: * @throws cDbException
225: * @throws cException
226: */
227: public function checkMark($type, $objectid) {
228: $type = $this->escape($type);
229: $objectid = $this->escape($objectid);
230:
231: $this->select("type='" . $type . "' AND objectid='" . $objectid . "'");
232:
233: if (($obj = $this->next()) !== false) {
234: return $obj;
235: } else {
236: return false;
237: }
238: }
239:
240: /**
241: * Checks and marks if not marked. Example: Check for "idmod", also return a
242: * lock message: list($inUse, $message) = $col->checkAndMark("idmod",
243: * $idmod, true, i18n("Module is in use by %s (%s)")); Example 2: Check for
244: * "idmod", don't return a lock message $inUse = $col->checkAndMark("idmod",
245: * $idmod);
246: *
247: * @param string $type
248: * Specifies the type to de-mark.
249: * @param mixed $objectid
250: * Specifies the object ID
251: * @param bool $returnWarning [optional]
252: * If true, also returns an error message if in use
253: * @param string $warningTemplate [optional]
254: * String to fill with the template (%s as placeholder, first %s is
255: * the username, second is the real name)
256: * @param bool $allowOverride [optional]
257: * True if the user can override the lock
258: * @param string $location [optional]
259: * Value to append to the override lock button
260: * @return bool array
261: * returnWarning is false, returns a bool value wether the object
262: * is locked. If returnWarning is true, returns a 2-item array
263: * (bool inUse, string errormessage).
264: * @throws cDbException
265: * @throws cException
266: * @throws cInvalidArgumentException
267: */
268: public function checkAndMark($type, $objectid, $returnWarning = false, $warningTemplate = '', $allowOverride = false, $location = '') {
269: global $sess, $auth, $notification, $area, $frame, $perm;
270:
271: if ((($obj = $this->checkMark($type, $objectid)) === false) || ($auth->auth['uid'] == $obj->get('userid'))) {
272: $this->markInUse($type, $objectid, $sess->id, $auth->auth['uid']);
273: $inUse = false;
274: $disabled = '';
275: $noti = '';
276: } else {
277: if ($returnWarning == true) {
278: $vuser = new cApiUser($obj->get('userid'));
279: $inUseUser = $vuser->getField('username');
280: $inUseUserRealName = $vuser->getField('realname');
281:
282: $message = sprintf($warningTemplate, $inUseUser, $inUseUserRealName);
283:
284: if ($allowOverride == true && ($auth->auth['uid'] == $obj->get('userid') || $perm->have_perm())) {
285: $alt = i18n("Click here if you want to override the lock");
286:
287: $link = $sess->url($location . "&overridetype=" . $type . "&overrideid=" . $objectid);
288:
289: $warnmessage = i18n("Do you really want to override the lock?");
290: $script = "javascript:if (window.confirm('" . $warnmessage . "') == true) { window.location.href = '" . $link . "';}";
291: $override = '<br><br><a alt="' . $alt . '" title="' . $alt . '" href="' . $script . '" class="standard">[' . i18n("Override lock") . ']</a> <a href="javascript://" class="standard" onclick="elem = document.getElementById(\'contenido_notification\'); elem.style.display=\'none\'">[' . i18n("Hide notification") . ']</a>';
292: } else {
293: $override = '';
294: }
295:
296: if (!is_object($notification)) {
297: $notification = new cGuiNotification();
298: }
299:
300: $noti = $notification->returnMessageBox('warning', $message . $override, 0);
301: $inUse = true;
302: }
303: }
304:
305: if ($returnWarning == true) {
306: return array($inUse, $noti);
307: } else {
308: return $inUse;
309: }
310: }
311:
312: }
313:
314: /**
315: * Class cApiInUse Class for a single in-use item
316: *
317: * @package Core
318: * @subpackage GenericDB_Model
319: */
320: class cApiInUse extends Item
321: {
322: /**
323: * Constructor to create an instance of this class.
324: *
325: * @param mixed $mId [optional]
326: * Specifies the ID of item to load
327: *
328: * @throws cDbException
329: * @throws cException
330: */
331: public function __construct($mId = false) {
332: global $cfg;
333: parent::__construct($cfg['tab']['inuse'], 'idinuse');
334: if ($mId !== false) {
335: $this->loadByPrimaryKey($mId);
336: }
337: }
338:
339: }
340: