/[cvs]/nfo/php/libs/org.netfrag.elib/_unused/Kopie von objects.php.inc
ViewVC logotype

Annotation of /nfo/php/libs/org.netfrag.elib/_unused/Kopie von objects.php.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Wed Jan 23 17:40:38 2002 UTC (22 years, 5 months ago) by cvsjoko
Branch point for: nfo, MAIN
Initial revision

1 cvsjoko 1.1 <?
2    
3     // ==============================================================
4     class DSAbstractObject {
5    
6     // the type of this object
7     var $type;
8    
9     // the name of the object-key
10     var $objectkey_name;
11    
12     // the (virtual) attributes
13     var $attributes;
14    
15     // references to other/new objects
16     // a reference to a "DSSession"-object which will be passed in "from outside" by the "DSEngine"
17     var $ref_session;
18     // a "DataQuery"-object instantiated in the constructor later
19     var $ref_dataquery;
20     // a reference to a "PDLModelObject"-object, which will be initialized in the constructor from "$ref_session"
21     var $ref_pdlModelObject;
22     // a reference to a list (array) of "PDLModelObject"-attributes ("PDLModelObjectAttribute"s)
23     var $ref_pdlModelObjectAttributes;
24    
25     // miscellanious other variables
26    
27     // some state-information
28     var $bool_unbound; // no binding to database yet
29     var $bool_created; // created for being a new object
30    
31     var $pdlEventName;
32     var $pdlEventName_CurrentObjectOperation;
33    
34     // metainfo about this object
35     //var $metainfo;
36    
37     // ---------------------------------------------------------------------------------------
38     function DSAbstractObject($type, $eventIdentifier, &$ref_session) {
39    
40     // initialize most important information for an object :-)
41     $this->type = $type;
42     $this->ref_session =& $ref_session;
43     $this->ref_pdlModelObject =& $this->ref_session->pdlengine->getPDLModelObject($type);
44     $this->ref_pdlModelObjectAttributes =& $this->ref_pdlModelObject->attributes;
45     $this->objectkey_name = $this->ref_pdlModelObject->objectkey;
46    
47     $this->_initAttributes();
48    
49     // initialize object-state (-> "this is a new object")
50     $this->bool_unbound = 1;
51     $this->bool_created = 1;
52    
53     $this->pdlEventName = 'retrieve ' . $eventIdentifier;
54     $this->pdlEventName_CurrentObjectOperation = '';
55    
56     // create a new "DataQuery"-object and store reference
57     $this->ref_dataquery =& new DataQuery();
58    
59     }
60    
61    
62     // ---------------------------------------------------------------------------------------
63     function _initAttributes() {
64     $attribute_names = array_keys($this->ref_pdlModelObjectAttributes);
65     $this->attributes = createHash($attribute_names);
66     }
67    
68     // ---------------------------------------------------------------------------------------
69     function _initializeObjectOperation($eventname) {
70    
71     // set class-variables
72     $this->pdlEventName_CurrentObjectOperation = $eventname;
73    
74     // get pdl for current event from "PDLModelObject"
75     if (!isset($this->ref_pdlModelObject->events[$eventname])) {
76     trigger_error("DSDataObject->load(): Could not find PDLModelEvent \"$eventname\" for object-type \"$this->type\"", E_USER_ERROR);
77     }
78     $unreadySql = $this->ref_pdlModelObject->events[$eventname]->get_unreadySql();
79    
80     // initialize "DataQuery"s map-part with mapping-informations
81     $this->ref_dataquery->setMap($this->ref_pdlModelObjectAttributes);
82    
83     // initialize "DataQuery"s sql-part with the "unready" sql-statement
84     $this->ref_dataquery->setQuery($unreadySql);
85    
86     }
87    
88     // ---------------------------------------------------------------------------------------
89     function _load_startup() {
90    
91     // if this object is already bound, we will skip loading it again
92     // TODO: if not "bool_dirty"
93     if (!$this->bool_unbound) { return; }
94    
95     // set object-state
96     $this->bool_unbound = 0;
97     $this->bool_created = 0;
98    
99     $this->_initializeObjectOperation($this->pdlEventName);
100    
101     }
102    
103     // ---------------------------------------------------------------------------------------
104     function _save_startup() {
105    
106     // - - - - - - - - - - - - - - - - - - - - - - - - - - -
107     // determine pdl-event
108     $eventname = '';
109     // actual insert
110     if ($this->bool_unbound && $this->bool_created) {
111     $eventname = 'add';
112     }
113     // actual update
114     if (!$this->bool_unbound && !$this->bool_created) {
115     $eventname = 'update';
116     }
117    
118     // if this object is already bound, we will skip loading it again
119     // TODO: if not "bool_dirty"
120     //if (!$this->bool_unbound) { return; }
121    
122     // set object-state
123     //$this->bool_unbound = 0;
124     //$this->bool_created = 0;
125    
126     $this->_initializeObjectOperation($eventname);
127    
128     }
129    
130     // ---------------------------------------------------------------------------------------
131     function _delete_startup() {
132     $eventname = 'remove';
133     $this->_initializeObjectOperation($eventname);
134     }
135    
136    
137     // ---------------------------------------------------------------------------------------
138     // declares this object as "unbound" (not loaded)
139     function reset() {
140     $this->bool_unbound = 1;
141     }
142    
143    
144     }
145    
146    
147    
148     // ==============================================================
149     class DSDataObject extends DSAbstractObject {
150    
151    
152     // ---------------------------------------------------------------------------------------
153     function DSDataObject($type, &$ref_session) {
154    
155     // debugging
156     slog("DSDataObject: new DSDataObject($type)");
157     parent::DSAbstractObject($type, 'single', &$ref_session);
158    
159     }
160    
161    
162     // ---------------------------------------------------------------------------------------
163     function load() {
164    
165     $this->_load_startup();
166    
167     // validate attributes to assure a good object-operation
168     if (!$this->_validateAttributesForObjectOperation('load')) {
169     print "<font color=\"red\">DSDataObject->_validateAttributesForObjectOperation('load') for \"{$this->type}\" failed.</font><br>";
170     }
171    
172     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
173     // add criteria-information to the "DataQuery" to limit the result to this single object/entry
174     // HACK!!! move this functionality (':') to "...->addCriteria?)
175     $this->ref_dataquery->addCriteria(':' . $this->objectkey_name . ' = ' . $this->getObjectKey());
176    
177    
178     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
179     // retrieve data
180    
181     // execute the "DataQuery"
182     $this->ref_dataquery->query();
183    
184     // get next (first) item of result(s)
185     if ($this->ref_dataquery->next()) {
186    
187     // initialize attributes with loaded values
188     $attributes_loaded =& $this->ref_dataquery->getResult();
189    
190     // fill ourself with attributes
191     $this->fill($attributes_loaded);
192    
193     return 1;
194     }
195    
196     }
197    
198    
199     // ---------------------------------------------------------------------------------------
200     function save() {
201    
202     $this->_save_startup();
203    
204     // catch all vars to save this object
205     $this->save_catchAllVars();
206    
207     // validate attributes to assure a good object-operation
208     if (!$this->_validateAttributesForObjectOperation('save')) {
209     print "<font color=\"red\">DSDataObject->_validateAttributesForObjectOperation('save') for \"{$this->type}\" failed.</font><br>";
210     }
211    
212     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
213     // add criteria-information to the "DataQuery" to limit the result to this single object/entry
214     // HACK!!! move this functionality (':') to "...->addCriteria?)
215     //$this->ref_dataquery->addCriteria(':' . $this->objectkey_name . ' = ' . $objectkey);
216    
217     //print $this->ref_dataquery->getSql() . "<br>";
218     //print "save<br>";
219     //exit;
220    
221     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222     // save data
223    
224     // execute the "DataQuery"
225     $this->ref_dataquery->setData($this->attributes);
226     $this->ref_dataquery->query();
227    
228     // TODO: review this mechanism!
229     // patch object-key if this was a create-operation, because new object-keys are provided by the underlying rdbms here
230     if ($this->pdlEventName_CurrentObjectOperation == 'add') {
231     $this->setObjectKey($this->ref_dataquery->lastInsertId);
232     }
233    
234     }
235    
236     function save_catchAllVars() {
237    
238     global $HTTP_GET_VARS;
239    
240     while(list($key, $value) = each($this->attributes)) {
241     if (isset($HTTP_GET_VARS[$key])) {
242     $this->attributes[$key] = $HTTP_GET_VARS[$key];
243     }
244     }
245    
246     }
247    
248    
249     // ---------------------------------------------------------------------------------------
250     function delete() {
251    
252     $this->_delete_startup();
253     $this->ref_dataquery->query();
254    
255     /*
256     $eventname = 'remove';
257    
258     if (!isset($this->pdlModelObject->events[$eventname])) {
259     trigger_error("could not find PDLModelObjectEvent \"$eventname\"", E_USER_ERROR);
260     }
261     $almost_sql = $this->pdlModelObject->events[$eventname]->getEventPdl();
262     $almost_sql .= ';';
263    
264     $almost_sql = str_replace(':' . $this->getID_name(), "'" . $this->getID() . "'", $almost_sql);
265    
266     $sql = $almost_sql;
267    
268     if ($sql) {
269     $this->dataquery->setSQL($sql);
270     $this->dataquery->query();
271     }
272     */
273    
274     }
275    
276     // ---------------------------------------------------------------------------------------
277     function fill($attributes_new) {
278    
279     // save all attribute-names in case the new ones are incomplete
280     $attribute_names = array_keys($this->attributes);
281    
282     // overwrite original attributes with new ones
283     $this->attributes = array_merge(createHash($attribute_names), $attributes_new);
284    
285     // set object-state
286     switch($this->getObjectKey()) {
287     case '<undefined>':
288     break;
289     case '<new>':
290     break;
291     case '':
292     break;
293     // yeah, the object-key is set to a valid value, so we declare this object as "bound"
294     default:
295     $this->bool_unbound = 0;
296     break;
297     }
298    
299     }
300    
301    
302     function setMetaInfo($metainfo) {
303     $this->metainfo = $metainfo;
304     }
305    
306     function unbind() {
307     $this->unbound = 1;
308     }
309    
310     // ---------------------------------------------------------------------------------------
311     function _validateAttributesForObjectOperation($operation_mode) {
312    
313     /*
314     object-key-juggling, we must handle a few things:
315     - does this object has got object-keys or not?
316     - and if it has - are they valid and stuff?
317     */
318    
319     switch($operation_mode) {
320    
321     // - - - - - - - - - - - -
322     case 'load':
323     //if ($this->hasObjectKey()) {
324     $objectkey = $this->getObjectKey();
325     // define here what shall happen if an object with key "<undefined>" gets a load-request
326     if ($objectkey == '<undefined>') {
327     print "<font color=\"red\">Could not load object with object-type \"{$this->type}\", because the object-key \"{$this->objectkey_name}\" was \"undefined\".</font><br>";
328     return;
329     }
330     // define here what shall happen if an object with key "<new>" gets a load-request
331     if ($objectkey == '<new>') {
332     print "<font color=\"red\">Could not load object with object-type \"{$this->type}\", because the object-key \"{$this->objectkey_name}\" was \"new\".</font><br>";
333     return;
334     }
335     //}
336     break;
337    
338     // - - - - - - - - - - - -
339     case 'save':
340     if ($this->hasObjectKey()) {
341     $objectkey = $this->getObjectKey();
342     // define here what shall happen if an object with key "<undefined>" gets a save-request
343     if ($objectkey == '<undefined>') {
344     print "<font color=\"red\">Could not save object with object-type \"{$this->type}\", because the object-key \"{$this->objectkey_name}\" was \"undefined\".</font><br>";
345     return;
346     }
347     // define here what shall happen if an object with key "<new>" gets a save-request
348     if ($objectkey == '<new>') {
349     //print "<font color=\"red\">Could not save object with object-type \"{$this->type}\", because the object-key \"{$this->objectkey_name}\" was \"new\".</font><br>";
350     //return;
351     // overwrite the value of the objectkey-attribute
352     $this->setObjectKey('null');
353     }
354     }
355    
356     break;
357    
358     }
359    
360     return 1;
361    
362     }
363    
364     // ---------------------------------------------------------------------------------------
365     function _checkAttributeAccess($attribute_name, $access_mode) {
366    
367     /*
368     remarks for attribute-access-checking: is the following mechanism okay?
369     - always (maybe first): check for attribute-existance
370     - read-mode:
371     - if the object is new (objectkey == '<new>'), return no value
372     - write-mode:
373     - if the object has no object-key: continue work
374     - if the object has got one or more object-key(s):
375     - if the object is bound: continue work
376     - if the object is not bound: check if all object-keys are set first before accessing other attributes
377     */
378    
379     if (!$this->_existsAttribute($attribute_name)) {
380     print "<font color=\"red\">Could not access attribute \"$attribute_name\" (operation: \"$access_mode\") in object \"{$this->type}\". Attribute does not exist.</font><br>";
381     return 0;
382     }
383    
384    
385     switch ($access_mode) {
386    
387     // - - - - - - - - - - - -
388     case 'read':
389     // is this a new object?
390     if ( $this->getObjectKey() == '<new>' ) {
391     //return $this->_getAttribute($attribute_name);
392     return 0;
393     }
394     break;
395    
396     // - - - - - - - - - - - -
397     case 'write':
398     // if this object is unbound, just allow access to the object-key(s)
399     // this is needed to load a single object in order to set the object-id for loading
400    
401     // check for object-key
402     if ($this->objectkey_name) {
403     if ($this->bool_unbound) {
404     if ($attribute_name != $this->objectkey_name) {
405     print "<font color=\"red\">Could not access attribute \"$attribute_name\" for setting in object-type \"{$this->type}\", because the object is unbound, not new, ";
406     print "and \"$attribute_name\" is not the object-key.</font><br>";
407     return 0;
408     }
409     }
410     } else {
411     // object has got no object-key: continue work
412     }
413    
414     break;
415     }
416    
417     return 1;
418     }
419    
420     // ---------------------------------------------------------------------------------------
421     function get($attribute_name) {
422    
423     // debugging
424     slog("DSDataObject: get($attribute_name)");
425    
426     // attribute-access!?
427     if (!$this->_checkAttributeAccess($attribute_name, 'read')) { return; }
428    
429     // is this object unbound? if yes, do an implicit load!
430     if ($this->bool_unbound) {
431     $this->load();
432     }
433    
434     // TODO: other DataCollections (for "Associations"-functionality)
435     //$retval = '';
436    
437     // HACK: this comparison doesn't ensure completely if an attribute-value is set or not ( imagine "0" (zero)!!! )
438     if ( $retval = $this->_getAttribute($attribute_name) ) {
439     return $retval;
440     }
441    
442     }
443    
444     // ---------------------------------------------------------------------------------------
445     function set($attribute_name, $value) {
446    
447     // attribute-access!?
448     if (!$this->_checkAttributeAccess($attribute_name, 'write')) { return; }
449    
450     slog("DSDataObject: set($attribute_name, $value)");
451     // TODO (MUST): other DataCollections
452    
453     if (isset($this->metainfo[$attribute_name])) {
454     switch ($this->metainfo[$attribute_name]['type']) {
455     case 'date':
456     // modify date from english/american format to german one
457     $value = join('-', array_reverse( split('.', $value) ));
458     break;
459     }
460     }
461    
462     $this->_setAttribute($attribute_name, $value);
463     }
464    
465     // ---------------------------------------------------------------------------------------
466     function getAll_old() {
467     slog("DSDataObject: getAll()");
468     return $this->attributes;
469     }
470    
471     function setAll_old($attributes) {
472     slog("DSDataObject: setAll($attributes)");
473     $this->attributes = $attributes;
474     }
475    
476    
477     // ---------------------------------------------------------------------------------------
478     // returns the attribute-name of the object-key
479     function getObjectKeyName() {
480     // get name of key
481     return $this->objectkey_name;
482     }
483    
484     // ---------------------------------------------------------------------------------------
485     // returns the attribute-value of the object-key
486     function getObjectKey() {
487    
488     // get value of key
489     $objectkey = $this->_getAttribute($this->objectkey_name);
490    
491     if ($objectkey) {
492     return $objectkey;
493     } else {
494     // HACK: !?
495     //return "<new>";
496     return "<undefined>";
497     }
498     }
499    
500     // ---------------------------------------------------------------------------------------
501     // sets the attribute-value of the object-key
502     function setObjectKey($value) {
503     // set value of key
504     $this->_setAttribute($this->objectkey_name, $value);
505     }
506    
507     // ---------------------------------------------------------------------------------------
508     // has this object an object-key?
509     function hasObjectKey() {
510     if ($this->objectkey_name) { return 1; }
511     }
512    
513     // ---------------------------------------------------------------------------------------
514     function _getAttribute($attribute_name) {
515     if (isset($this->attributes[$attribute_name])) {
516     return $this->attributes[$attribute_name];
517     }
518     }
519    
520     function _setAttribute($attribute_name, $value) {
521     $this->attributes[$attribute_name] = $value;
522     }
523    
524     function _existsAttribute($attribute_name) {
525     return isset($this->attributes[$attribute_name]);
526     }
527    
528    
529     }
530    
531    
532    
533    
534    
535     // ==============================================================
536     // implements a collection of "DSDataObject"s interacting with a DataQuery
537     class DSDataCollection extends DSAbstractObject {
538    
539     // - - - - - - - - - - - - - - - - - - - - - -
540     // data which influences the result of this "DSDataCollection"
541    
542     // filters we applied to this DataCollection (a hash of "DSFilter"-objects)
543     var $filters;
544     // the default concatenator for appending the "filters" to a query with already existing criteria-information
545     var $filterConcatenator;
546    
547     // sortorders we applied to this DataCollection (a hash of "DSSortOrder"-objects)
548     var $sortorders;
549    
550     // - - - - - - - - - - - - - - - - - - - - - -
551     // miscellanious data
552    
553     // paging?
554     var $page;
555    
556     // the number of results
557     var $resultcount;
558    
559     // var $data;
560     //var $metainfo;
561    
562    
563     // ---------------------------------------------------------------------------------------
564     function DSDataCollection($type, &$ref_session) {
565    
566     // call parent-constructor
567     parent::DSAbstractObject($type, 'all', &$ref_session);
568    
569     // initialize class-variables
570     $this->filters = array();
571     $this->filterConcatenator = 'AND';
572     $this->sortorders = array();
573     }
574    
575    
576     // ---------------------------------------------------------------------------------------
577     // creates a new "DSFilter"-object and adds reference to local "filters"-hash
578     function &addFilter($filterstring, $logicalOperator = 'AND') {
579     slog("DSDataCollection: &addFilter($filterstring)");
580     $r_filter = &new DSFilter($filterstring, $logicalOperator);
581     $this->filters[] =& $r_filter;
582     return $r_filter;
583     }
584    
585     // ---------------------------------------------------------------------------------------
586     // clears all filters from local "filters"-hash
587     function clearFilters() {
588     slog("DSDataCollection: clearFilters()");
589     $this->filters = array();
590     }
591    
592    
593     // ---------------------------------------------------------------------------------------
594     // creates a new "DSSortOrder"-object and adds reference to local "sortorders"-hash
595     function &addSortOrder($column, $mode) {
596     slog("DSDataCollection: &addSortOrder($column, $mode)");
597     $r_order =& new DSSortOrder($column, $mode);
598     array_push($this->sortorders, &$r_order);
599     return $r_order;
600     }
601    
602    
603     // ---------------------------------------------------------------------------------------
604     // limits DataCollection to a certain "page" (for paging of results)
605     function &setPage($pagenumber, $pagesize) {
606     // debugging
607     slog("DSDataCollection: &setPage($pagenumber, $pagesize)");
608     $this->page['number'] = $pagenumber;
609     $this->page['size'] = $pagesize;
610     }
611    
612    
613     // ---------------------------------------------------------------------------------------
614     // loads data into this "DSDataCollection"
615     function load() {
616    
617     $this->_load_startup();
618    
619    
620     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
621     // 1. process filters
622     // $this->filterConcatenator
623     if (isset($this->filters) && count($this->filters)) {
624     reset($this->filters);
625     while($r_filter =& current($this->filters)) {
626     if ($strFilter = $r_filter->getParsed()) {
627     //$almost_sql_FilterPart .= $strParsed . $sql_concat;
628     //$this->ref_dataquery->addCriteria(':' . $this->objectkey_name . ' = ' . $this->getObjectKey());
629     $this->ref_dataquery->addCriteria($strFilter, $r_filter->logicalOperator);
630     }
631     next($this->filters);
632     }
633     // clear all filters
634     $this->filters = array();
635     }
636    
637    
638     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
639     // 2. process orders
640     if (isset($this->sortorders) && count($this->sortorders)) {
641     reset($this->sortorders);
642     while($r_order =& current($this->sortorders)) {
643     $this->ref_dataquery->addOrder($r_order->getAttribute(), $r_order->getOrderAscDesc());
644     next($this->sortorders);
645     }
646     }
647    
648    
649     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
650     // 3. limit?
651     if ( isset($this->page['number']) && isset($this->page['size']) ) {
652     $nr = $this->page['number'];
653     $size = $this->page['size'];
654     $offset = $nr * $size;
655     //$almost_sql .= " LIMIT $offset, $size";
656     $strLimit = "$offset, $size";
657     $this->ref_dataquery->addLimit($strLimit);
658     }
659    
660    
661     // fetch data
662     $this->ref_dataquery->query();
663     $this->resultcount = $this->ref_dataquery->getRowCount();
664     $this->ref_dataquery->retrieveMetaInfo();
665     //$this->metainfo = $this->dataquery->metainfo;
666    
667     }
668    
669    
670     // ---------------------------------------------------------------------------------------
671     // iterates to the next "DSDataObject" in the "DSDataCollection"-result and returns it
672     function &next() {
673    
674     // debugging
675     slog("DSDataCollection: &next()");
676    
677     // do an implicit load if we are "unbound"
678     if ($this->bool_unbound) {
679     $this->load();
680     }
681    
682     // get next item from a "DataQuery"-result
683     if ($this->ref_dataquery->next()) {
684     $result = $this->ref_dataquery->getResult();
685     //$obj =& $this->session->Map2Obj($row, $this->pdlModelObject->getObjectType() );
686     //$obj =& $this->session->Map2Obj($row, $this->objecttype_full, $this->pdlEventName, $this->metainfo );
687     //$obj->setMetaInfo($this->metainfo);
688     $obj =& $this->ref_session->getDataObject($this->type);
689     $obj->fill($result);
690     //$obj->attributes =& $result;
691     //$obj->bool_unbound = 0;
692     return $obj;
693     }
694     }
695    
696    
697     // ---------------------------------------------------------------------------------------
698     // returns the number of results
699     function getResultCount() {
700     return $this->resultcount;
701     }
702    
703     }
704    
705     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed