Source for file ow.php

Documentation is available at ow.php


1 <?php
2 /**
3 * @author Jan H. Andersen <jha@ipwsystems.dk>
4 * @author Martin R. Larsen <mrl@ipwsystems.dk>
5 * @copyright {@link http://www.ipwsystems.dk/ IPW Systems a.s}
6 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
7 * @package METAjour
8 * $Id: ow.php,v 1.21 2005/02/16 05:02:53 jan Exp $
9 */
10 require_once('basic_user.php');
11 $owcache = array();
12
13 function owcacheget($key) {
14 global $owcache
15 return (isset($owcache[$key])) ? $owcache[$key] : false;
16 }
17
18 function owcacheput($key, $value) {
19 global $owcache
20 $owcache[$key] = $value;
21 }
22
23 function packData($datatypecols, $data) {
24 $arr = array();
25 foreach ($datatypecols as $cur) {
26 $arr[$cur['name']] = $data[$cur['name']];
27 }
28 return $arr;
29 }
30
31
32 /**
33 * Create new instance of datatype and read object
34 * @return object
35 */
36 function owRead($objectid, $cache = false) {
37 global $system_path
38 $uh =& getUserHandler();
39 $app = $uh->getAppName();
40 $cached = owcacheget('read' . $objectid);
41 if ($cached !== false) {
42 $row = $cached;
43 } else {
44 $db =& getdbconn();
45 $sql = "select class.* from object, class where class.datatype = object.type and object.objectid = '$objectid'";
46 $row =& $db->getrow($sql);
47 if ($cache) owcacheput('read' . $objectid, $row);
48 }
49
50 if (is_array($row) && !empty($row)) {
51 switch($row['type']) {
52 case 0:
53 if (!empty($app) && file_exists($system_path.'app/'.$app.'/core/'.$app.'_'.$row['datatype'].'class.php')) {
54 require_once($system_path.'app/'.$app.'/core/'.$app.'_'.$row['datatype'].'class.php');
55 $s = $app.'_'.$row['datatype'];
56 $result = new $s;
57 } else {
58 @include_once($system_path.'core/'.$row['datatype'].'class.php');
59 if (class_exists($row['datatype'])) {
60 $result = new $row['datatype'];
61 } else {
62 # maybe error-flag
63 return false;
64 }
65 }
66
67 if ($result->readobject($objectid)) {
68 return $result;
69 }
70 # maybe error-flag
71 return false;
72
73 case 1:
74 if ($row['basedatatype'] == '') $row['basedatatype'] = $row['datatype'];
75 require_once($system_path.'extension/'.$row['basedatatype']."/".$row['datatype'].".datatype.php");
76 $result = new $row['datatype'];
77 if ($result->readobject($objectid)) return $result;
78 return false;
79 }
80 } else {
81 #die('Fatal error: Trying to instantiate non-existing datatype');
82 # maybe error-flag
83 return false;
84 }
85 }
86
87 function owReadExpand($objectid, $readtree = true) {
88 if ($objectid == 0) return '';
89 $obj = owRead($objectid);
90 if (!$obj) return array();
91 $mycols = owDatatypeCols($obj->gettype());
92 foreach ($mycols as $cur) {
93 /*
94 * @todo: take care of UI_RELATION_MULTIPLE too
95 *
96 * Note that adding new UI-types with relations to other objects
97 * has to be added here for proper expansion of objects
98 */
99 if ($cur['inputtype'] == UI_RELATION || $cur['inputtype'] == UI_LISTDIALOG) {
100 $obj->elements[0][$cur['name']] = owReadExpand($obj->elements[0][$cur['name']], false);
101 }
102 }
103 if ($obj->getParentId() != 0 && $readtree)
104 $obj->elements[0]['p'] = owReadExpand($obj->getParentId(),false);
105 if ($obj->getType() == "case" && $obj->hasChild() && $readtree) {
106 $c = array();
107
108 $childs = owNew('caseaction');
109 $childs->listobjects($obj->getObjectId());
110 if (!empty($childs->elements)) {
111 foreach ($childs->elements as $elem) {
112 $c[] = owReadExpand($elem['objectid'], false);
113 }
114 }
115 $childs = owNew('casenote');
116 $childs->listobjects($obj->getObjectId());
117 if (!empty($childs->elements)) {
118 foreach ($childs->elements as $elem) {
119 $c[] = owReadExpand($elem['objectid'], false);
120
121 }
122 }
123 $obj->elements[0]['c'] = $c;
124 }
125
126 if ($obj->getType() != "case" && $obj->hasChild() && $readtree) {
127 $c = array();
128
129 $childs = owNew($obj->getSubType());
130 $childs->listobjects($obj->getObjectId());
131 if (!empty($childs->elements)) {
132 foreach ($childs->elements as $elem) {
133 $c[] = owReadExpand($elem['objectid'], false);
134 }
135 }
136 $obj->elements[0]['c'] = $c;
137 }
138
139
140 return $obj->elements[0];
141 }
142
143 function owReadTextual($objectid) {
144 if ($objectid == 0) return array();
145 $obj = owRead($objectid, false);
146 if (!$obj) return array();
147 $mycols = owDatatypeColsDesc($obj->gettype());
148 $arr = array();
149 $i = 0;
150 foreach ($mycols as $cur) {
151 if ($cur['inputtype'] == UI_RELATION || $cur['inputtype'] == UI_LISTDIALOG) {
152 $arr[$i]['name'] = $cur['name'];
153 $arr[$i]['fieldrep'] = owReadName($obj->elements[0][$cur['name']]);
154 $arr[$i]['fieldvalue'] = $obj->elements[0][$cur['name']];
155 }
156 elseif ($cur['inputtype'] == UI_COMBO) {
157 $arr[$i]['name'] = $cur['name'];
158 $arr[$i]['fieldrep'] = $cur['comboarray'][$obj->elements[0][$cur['name']]];
159 }
160 elseif ($cur['inputtype'] == UI_COMBO_MULTIPLE) {
161 if (is_array($obj->elements[0][$cur['name']])) {
162 foreach ($obj->elements[0][$cur['name']] as $c) {
163 $arr[$i]['name'] = $cur['name'];
164 //$arr[$i]['fieldrep'] = $cur['comboarray'][$obj->elements[0][$cur['name']][$c]];
165 $arr[$i]['fieldrep'] = $cur['comboarray'][$c];
166 $i++;
167 }
168 }
169 }
170 elseif ($cur['inputtype'] == UI_RELATION_MULTIPLE) {
171 if (is_array($obj->elements[0][$cur['name']])) {
172 foreach ($obj->elements[0][$cur['name']] as $c) {
173 $arr[$i]['name'] = $cur['name'];
174 //$arr[$i]['fieldrep'] = owReadName($obj->elements[0][$cur['name']][$i]);
175 //$arr[$i]['fieldvalue'] = $obj->elements[0][$cur['name']][$c];
176 $arr[$i]['fieldrep'] = owReadName($c);
177 $arr[$i]['fieldvalue'] = $c;
178 $i++;
179 }
180 }
181 }
182 else {
183 $arr[$i]['name'] = $cur['name'];
184 $arr[$i]['fieldrep'] = $obj->elements[0][$cur['name']];
185 }
186 $i++;
187 }
188
189 return $arr;
190 }
191
192 function owReadName($objectid) {
193 if ($objectid == 0) return '';
194 $obj = owRead($objectid);
195 if ($obj) return $obj->elements[0]['name'];
196 return '';
197 }
198
199 function owReadCol($objectid, $colname) {
200 if ($objectid == 0) return '';
201 $obj = owRead($objectid);
202 if ($obj) return $obj->elements[0][$colname];
203 return '';
204 }
205
206 function owGetDatatype($objectid) {
207 $db =& getdbconn();
208 return $db->getone("select type from object where objectid = '$objectid'");
209 }
210
211 /**
212 * Create new instance of datatype
213 * @return object
214 */
215 function owNew($datatype) {
216 global $system_path
217 $uh =& getUserHandler();
218 $app = $uh->getAppName();
219 $cached = owcacheget('create' . $datatype);
220 if ($cached !== false) {
221 $row = $cached;
222 } else {
223 $db =& getdbconn();
224 $row =& $db->getrow("select type, basedatatype from class where datatype = '$datatype'");
225 owcacheput('create' . $datatype, $row);
226 }
227
228 if (is_array($row)) {
229 switch($row['type']) {
230 case 0:
231 if (!empty($app) && file_exists($system_path.'app/'.$app.'/core/'.$app.'_'.$datatype.'class.php')) {
232 require_once($system_path.'app/'.$app.'/core/'.$app.'_'.$datatype.'class.php');
233 $s = $app.'_'.$datatype;
234 return new $s;
235 } else {
236 require_once($system_path.'core/'.$datatype.'class.php');
237 return new $datatype;
238 }
239
240 case 1:
241 if ($row['basedatatype'] == '') $row['basedatatype'] = $datatype;
242 require_once($system_path.'extension/'.$row['basedatatype']."/".$datatype.".datatype.php");
243 return new $datatype;
244 }
245 } else {
246 die('Fatal error! Trying to instantiate non-existing datatype: '. $datatype);
247 }
248 }
249
250 function owGetBasedatatype($datatype) {
251 $db =& getdbconn();
252 $col = & $db->getone("select basedatatype from class where datatype = '$datatype'");
253 if ($col['basedatatype'] == '') $col['basedatatype'] = $datatype;
254 return $col['basedatatype'];
255 }
256
257 function owIsExtension($datatype) {
258 global $system_path
259 # returns true if $extension contains an extension class file
260 # the file_exists check will later be replaced by information in the class table
261 return file_exists($system_path."extension/".owGetBasedatatype($datatype) . "/" . $datatype . ".class.php");
262 }
263
264 function owIsExtendedDatatype($datatype) {
265 global $system_path
266 # returns true if $extension contains a datatype definition file
267 # the file_exists check will later be replaced by information in the class table
268 return file_exists($system_path."extension/".owGetBasedatatype($datatype) . "/" . $datatype . ".datatype.php");
269 }
270
271 function owListCore($all=false) {
272 $uh =& getUserHandler();
273 $site = $uh->getSite();
274 $app = $uh->getAppName();
275 $db =& getdbconn();
276 if ($all) {
277 $col = & $db->getcol("select datatype from class where type=0 and (site='0' OR site='$site') order by datatype");
278 } else {
279 $col = & $db->getcol("select datatype from class where type=0 and (app='' OR app='$app') and (site='0' OR site='$site') order by datatype");
280 }
281 return $col;
282 }
283
284 function owListExtensions($all=false) {
285 $uh =& getUserHandler();
286 $site = $uh->getSite();
287 $app = $uh->getAppName();
288 $db =& getdbconn();
289 if ($all) {
290 $col = & $db->getcol("select datatype from class where type=1 and (site='0' OR site='$site') order by datatype");
291 } else {
292 $col = & $db->getcol("select datatype from class where type=1 and (app='' OR app='$app') and (site='0' OR site='$site') order by datatype");
293 }
294 return $col;
295 }
296
297 function owListSystem() {
298 $uh =& getUserHandler();
299 $site = $uh->getSite();
300 $app = $uh->getAppName();
301 $db =& getdbconn();
302 $col = & $db->getcol("select datatype from class where type=5 and (app='' OR app='$app') and (site='0' OR site='$site') order by datatype");
303 return $col;
304 }
305
306 function owGetLanguages() {
307 $db =& getdbconn();
308 $col = & $db->getcol("select distinct language from object where site = '".$_SESSION['site']."' AND deleted = 0 AND language <> ''");
309 return $col;
310 }
311
312 function locateLangFiles($file) {
313 $uh =& getUserHandler();
314 $app = $uh->getAppName();
315 $language = $uh->getGuiLanguage();
316 $deflang = 'da';
317
318 $arr = array();
319
320 if ( file_exists(dirname(__FILE__).'/lang/'.$file.'.'.$language.'.php')) {
321 $arr[] = 'lang/'.$file.'.'.$language.'.php';
322 } elseif (file_exists(dirname(__FILE__).'/lang/'.$file.'.'.$deflang.'.php')) {
323 $arr[] = 'lang/'.$file.'.'.$deflang.'.php';
324 }
325 if (!empty($app)) {
326 if ( file_exists(dirname(__FILE__).'/app/'.$app.'/lang/'.$app.'_'.$file.'.'.$language.'.php')) {
327 $arr[] = 'app/'.$app.'/lang/'.$app.'_'.$file.'.'.$language.'.php';
328 } elseif (file_exists(dirname(__FILE__).'/app/'.$app.'/lang/'.$app.'_'.$file.'.'.$deflang.'.php')) {
329 $arr[] = 'app/'.$app.'/lang/'.$app.'_'.$file.'.'.$deflang.'.php';
330 }
331 }
332 if (owIsExtendedDatatype($file)) {
333 $basedatatype = owGetBasedatatype($file);
334 if ( file_exists(dirname(__FILE__).'/extension/'.$basedatatype.'/lang/'.$file.'.'.$language.'.php')) {
335 $arr[] = 'extension/'.$basedatatype.'/lang/'.$file.'.'.$language.'.php';
336 } elseif (file_exists(dirname(__FILE__).'/extension/'.$basedatatype.'/lang/'.$file.'.'.$deflang.'.php')) {
337 $arr[] = 'extension/'.$basedatatype.'/lang/'.$file.'.'.$deflang.'.php';
338 }
339 }
340 return $arr;
341 }
342
343 /**
344 * @return array of user defined columns for $datatype
345 * The array includes the descriptions of the fields (label)
346 ***/
347 function owDatatypeExtraCols($datatype) {
348 $arr = array();
349 $edobj = owNew('extradata');
350 $id = $edobj->locateByName($datatype);
351 if ($id) {
352 $edobj->readObject($id);
353 $i = 0;
354 if (is_array($edobj->elements[0]['fieldname'])) {
355 foreach ($edobj->elements[0]['fieldname'] as $val) {
356 $arr[$val]['name'] = $val;
357 $arr[$val]['inputtype'] = $edobj->elements[0]['fieldtype'][$i];
358 $arr[$val]['label'] = $edobj->elements[0]['fielddescription'][$i];
359 $i++;
360 }
361 }
362 }
363 return $arr;
364 }
365
366 /**
367 * @return array of private columns for $datatype
368 * The array does not include the descriptions of the fields
369 ***/
370 function owDatatypePrivCols($datatype) {
371 $obj = owNew($datatype);
372 $arr = array();
373 if ($obj) {
374 $obj->initLayout();
375 $tarr = $obj->getColumns();
376 foreach ($tarr as $cur) {
377 $arr[$cur['name']] = $cur;
378 }
379 }
380 return $arr;
381 }
382
383 /**
384 * @return array of all columns for $datatype
385 * The array does not include the descriptions of the fields (except for the user defined columns)
386 ***/
387 function owDatatypeCols($datatype) {
388 return array_merge(owDatatypePrivCols($datatype),owDatatypeExtraCols($datatype));
389 }
390
391 function owLabel($datatype,$colname) {
392 global $OWLANG
393 if (!isset($OWLANG[$datatype])) owDatatypeColsDesc($datatype);
394
395 return $OWLANG[$datatype][$colname]['label'];
396 }
397
398 function owDatatypeColsDesc($datatype) {
399 global $OWLANG
400 $uh =& getUserHandler();
401 $app = $uh->getAppName();
402 if (isset($OWLANG[$datatype])) {
403 return $OWLANG[$datatype];
404 } else {
405 $arr = owDatatypePrivCols($datatype);
406 $langfiles = locateLangFiles($datatype);
407 $LANG = array();
408 foreach ($langfiles as $langfile) {
409 include($langfile);
410 }
411
412 foreach ($arr as $cur) {
413 if ($cur['inputtype'] == UI_COMBO || $cur['inputtype'] == UI_COMBO_MULTIPLE) {
414 $arr[$cur['name']]['comboarray'] = $LANG['option_'.$cur['name']];
415 }
416 if (isset($LANG['label_'.$cur['name']])) {
417 $arr[$cur['name']]['label'] = $LANG['label_'.$cur['name']];
418 } else {
419 /*
420 AUTOMATIC LANG WRITING DISABLED IN DIST-version
421 */
422 /*if ($app != "") {
423 $f = @fopen('app/'.$app.'/lang/'.$app.'_'.$datatype.'.da.php','a');
424 } else {
425 $f = @fopen('lang/'.$datatype.'.da.php','a');
426 }
427 if ($f !== false) {
428 fwrite($f,"<?php\n\$LANG['label_".$cur['name']."'] = '".$cur['name']."';\n?>");
429 fclose($f);
430 }*/
431 $arr[$cur['name']]['label'] = $cur['name'];
432 }
433 }
434
435 $result = array_merge($arr,owDatatypeExtraCols($datatype));
436 $OWLANG[$datatype] = $result;
437 return $result;
438 }
439 }
440
441 /**
442 * @return string descriptive name of datatype
443 ***/
444 function owDatatypeDesc($datatype) {
445 /**
446 * @todo handle extension language files and apps
447 */
448 $uh =& getUserHandler();
449 $app = $uh->getAppName();
450
451 $language = strtolower($uh->getGuiLanguage());
452 $deflang = 'en';
453 $db =& getdbconn();
454 $row = $db->getrow("select name from classname where datatype = '$datatype' AND language = '$language'");
455 if (count($row)) {
456 return $row['name'];
457 }
458
459 if (!owIsExtendedDatatype($datatype)) {
460 if ( file_exists(dirname(__FILE__).'/lang/'.$datatype.'.'.$language.'.php')) {
461 include('lang/'.$datatype.'.'.$language.'.php');
462 if (isset($LANG['name'])) {
463 $result = $LANG['name'];
464 $db->execute("insert into classname values ('$datatype','$language','$result')");
465 return $result;
466 }
467 }
468 if (!empty($app)) {
469 if ( file_exists(dirname(__FILE__).'/app/'.$app.'/lang/'.$app.'_'.$datatype.'.'.$language.'.php')) {
470 include('app/'.$app.'/lang/'.$app.'_'.$datatype.'.'.$language.'.php');
471 if (isset($LANG['name'])) {
472 $result = $LANG['name'];
473 $db->execute("insert into classname values ('$datatype','$language','$result')");
474 return $result;
475 }
476 }
477 }
478 } else {
479 if ( file_exists(dirname(__FILE__).'/extension/'.owGetBasedatatype($datatype).'/lang/'.$datatype.'.'.$language.'.php')) {
480 include('extension/'.owGetBasedatatype($datatype).'/lang/'.$datatype.'.'.$language.'.php');
481 if (isset($LANG['name'])) {
482 $result = $LANG['name'];
483 $db->execute("insert into classname values ('$datatype','$language','$result')");
484 return $result;
485 }
486 }
487 }
488
489 $row = $db->getrow("select name from classname where datatype = '$datatype' AND language = '$deflang'");
490 if (count($row)) {
491 return $row['name'];
492 }
493 if (!owIsExtendedDatatype($datatype)) {
494 if ( file_exists(dirname(__FILE__).'/lang/'.$datatype.'.'.$deflang.'.php')) {
495 include('lang/'.$datatype.'.'.$deflang.'.php');
496 if (isset($LANG['name'])) {
497 $result = $LANG['name'];
498 $db->execute("insert into classname values ('$datatype','$deflang','$result')");
499 return $result;
500 }
501 }
502 if (!empty($app)) {
503 if ( file_exists(dirname(__FILE__).'/app/'.$app.'/lang/'.$app.'_'.$datatype.'.'.$deflang.'.php')) {
504 include('app/'.$app.'/lang/'.$app.'_'.$datatype.'.'.$deflang.'.php');
505 if (isset($LANG['name'])) {
506 $result = $LANG['name'];
507 $db->execute("insert into classname values ('$datatype','$deflang','$result')");
508 return $result;
509 }
510 }
511 }
512 } else {
513 if ( file_exists(dirname(__FILE__).'/extension/'.owGetBasedatatype($datatype).'/lang/'.$datatype.'.'.$deflang.'.php')) {
514 include('extension/'.owGetBasedatatype($datatype).'/lang/'.$datatype.'.'.$deflang.'.php');
515 if (isset($LANG['name'])) {
516 $result = $LANG['name'];
517 $db->execute("insert into classname values ('$datatype','$deflang','$result')");
518 return $result;
519 }
520 }
521 }
522 return $datatype;
523 }
524
525 function owGetApps() {
526 $uh =& getUserHandler();
527 $path = $uh->getSystemPath().'app/*';
528 $matches = glob($path);
529 $arr = array();
530 $arr[0]['app'] = 'metajour';
531 $arr[0]['name'] = 'IPW METAjour';
532 foreach ($matches as $match) {
533 $base = basename($match);
534 if ($base != 'CVS') {
535 $cnt = sizeof($arr);
536 $arr[$cnt]['app'] = $base;
537 $arr[$cnt]['name'] = $base;
538 $namefile = $uh->getSystemPath().'app/'.$base.'/lang/appname.da.php';
539 if (file_exists($namefile)) {
540 include($namefile);
541 $arr[$cnt]['name'] = $LANG['appname'];
542 }
543 }
544 }
545 return $arr;
546 }
547
548
549 function owReIndexAll</