NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/Chameleon/trunk/Chameleon/Table/Table.widget.php

Revision 13 (checked in by jcleary, 17 years ago)

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * Table Widget class
4  *
5  * @project     CWC2
6  * @revision    $Id:
7  * @purpose     Driving Directions Table Widget class
8  * @author      DM Solutions Group (sfournier@dmsolutions.ca)
9  * @copyright
10  * <b>Copyright (c) 2002, DM Solutions Group Inc.</b>
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 include_once(dirname(__FILE__)."/../Widget.php");
30
31 /**
32  * Table Class
33  *
34  * @desc Table widget class
35  */
36 class Table extends CWCWidget
37 {
38     var $mszSharedResourceName = "";
39     var $mszHeader = "";
40     var $mszBody   = "";
41     var $mszFooter = "";
42     var $mszNextButton = "Next";
43     var $mszPrevButton = "Previous";
44     var $mnStart  = 0;
45     var $mnCount  = 10;
46     var $mszSortBy= "";
47     var $mbReverseSort = false;
48     var $maoSharedResources = array();
49     var $mszName = '';
50
51     // Array of from to replace into Header and footer
52     var $maszFrom = array();
53     var $maszTo   = array();
54     var $maszRegExFrom = array();
55     var $maszRegExTo   = array();
56     var $maCallbackRegEx = array();
57
58     // Array of from to replace into body
59     var $maszBodyFrom = array();
60     var $maszBodyTo   = array();
61     var $maszBodyRegExFrom = array();
62     var $maszBodyRegExTo   = array();
63     var $maFunction = array();
64
65
66
67     /**
68      * Table
69      *
70      * Constructor method for the Table widget.
71      */
72     function Table ()
73     {
74         // invoke constructor of parent
75         parent::CWCWidget();
76
77         // set the description for this widget
78         $this->szWidgetDescription = <<<EOT
79 The Table widget provides the functionality to display
80 shared resource as Table.
81 EOT;
82
83         $this->maAttributes["SHAREDRESOURCENAME"] = new StringAttribute( "SHAREDRESOURCENAME", true );
84         $this->maAttributes["SORTBY"] = new StringAttribute( "SORTBY", false );
85         $this->maAttributes["START"] = new IntegerAttribute( "START", false );
86         $this->maAttributes["COUNT"] = new IntegerAttribute( "COUNT", false );
87         $this->maAttributes["REVERSE"] = new BooleanAttribute( "REVERSE", false );
88         $this->maAttributes["NEXTBUTTON"] = new StringAttribute( "NEXTBUTTON", false );
89         $this->maAttributes["PREVBUTTON"] = new StringAttribute( "PREVBUTTON", false );
90         $this->maAttributes["NAME"] = new StringAttribute( "NAME", false );
91
92         $this->mnPriority = PRIORITY_HIGH;
93         $this->mnMaturityLevel = MATURITY_BETA;
94     }
95
96     function SortBy($a, $b)
97     {
98         // There can be more than one collumn to sort
99         $aszSortBy = explode("|", $this->mszSortBy);
100
101         $nRes = 0;
102
103         // For each sortby collumn vlaue
104         foreach($aszSortBy as $szSortBy)
105         {
106             $szSortBy = trim($szSortBy);
107
108             $nPos = strpos($szSortBy, "/");
109             if($nPos !== false)
110             {
111                 $szFlags = substr($szSortBy, $nPos+1);
112                 $szSortBy = substr($szSortBy, 0, $nPos);
113
114                 if(strpos($szFlags, "i") !== false)
115                 {
116                     $a[$szSortBy] = strtoupper($a[$szSortBy]);
117                     $b[$szSortBy] = strtoupper($b[$szSortBy]);
118                 }
119             }
120             else
121                 $szFlags = "";
122
123             if (!isset($a[$szSortBy]) || !isset($b[$szSortBy]))
124                 return 0;
125
126             if(is_numeric($a[$szSortBy]) && is_numeric($b[$szSortBy]) &&
127                $nRes == 0)
128                 if($a[$szSortBy] == $b[$szSortBy])
129                     $nRes = 0;
130                 else if($a[$szSortBy] < $b[$szSortBy])
131                     $nRes = -1;
132                 else
133                     $nRes = 1;
134             else
135                 $nRes = (($nRes == 0) ? strcmp($a[$szSortBy], $b[$szSortBy]) : $nRes);
136
137             if($szFlags != "")
138                 if(strpos($szFlags, ">") !== false)
139                     if ($nRes == 1) $nRes = -1;
140                     else if ($nRes == -1) $nRes = 1;
141         }
142
143         if ($this->mbReverseSort)
144         {
145             if ($nRes == 1) $nRes = -1;
146             else
147             if ($nRes == -1) $nRes = 1;
148         }
149         return $nRes;
150     }
151
152     function InitDefaults()
153     {
154         if (isset($this->maParams["SHAREDRESOURCENAME"]))
155             $this->mszSharedResourceName = $this->maParams["SHAREDRESOURCENAME"];
156
157         if (isset($this->maParams["START"]))
158             $this->mnStart = $this->maParams["START"];
159
160         if (isset($this->maParams["COUNT"]))
161             $this->mnCount = $this->maParams["COUNT"];
162
163         if (isset($this->maParams["SORTBY"]))
164             $this->mszSortBy = $this->maParams["SORTBY"];
165
166         if (isset($this->maParams["NEXTBUTTON"]))
167             $this->mszNextButton = $this->maParams["NEXTBUTTON"];
168
169         if (isset($this->maParams["PREVBUTTON"]))
170             $this->mszPrevButton = $this->maParams["PREVBUTTON"];
171
172         if (isset($this->maParams["NAME"]))
173             $this->mszName = $this->maParams["NAME"];
174
175         if (isset($this->maParams["REVERSE"]))
176         {
177             $this->mbReverseSort = $this->maParams["REVERSE"];
178
179             if (strcasecmp($this->mbReverseSort, "TRUE") == 0)
180                 $this->mbReverseSort = true;
181             else
182                 $this->mbReverseSort = false;
183         }
184
185         if (isset($this->maszContents))
186         {
187             if(isset($this->maszContents["SHAREDRESOURCE"]) &&
188                is_array($this->maszContents["SHAREDRESOURCE"]))
189             {
190                 $this->maoSharedResources =
191                     $this->maszContents["SHAREDRESOURCE"];
192             }
193         }
194
195         
196         // Get all templates
197         if (isset($this->maszContents['TEMPLATE']) &&
198             is_array( $this->maszContents['TEMPLATE'] ))
199         {
200             foreach ($this->maszContents['TEMPLATE'] as $oTemplate)
201             {
202                   if (isset($oTemplate['NAME']))
203                   {
204                       if (isset($oTemplate['CONTENT']) || isset($oTemplate['_CONTENTS']))
205                       {
206                           if (strcasecmp($oTemplate['NAME'], "HEADER") == 0)
207                           {
208                               $this->mszHeader = isset($oTemplate['CONTENT']) ? $oTemplate['CONTENT'] : $oTemplate['_CONTENTS'];
209                           }
210                           else
211                           {
212                               if (strcasecmp($oTemplate['NAME'], "BODY") == 0)
213                               {
214                                   $this->mszBody = isset($oTemplate['CONTENT']) ? $oTemplate['CONTENT'] : $oTemplate['_CONTENTS'];
215                               }
216                               else
217                               {
218                                 if (strcasecmp($oTemplate['NAME'], "FOOTER") == 0)
219                                 {
220                                     $this->mszFooter = isset($oTemplate['CONTENT']) ? $oTemplate['CONTENT'] : $oTemplate['_CONTENTS'];
221                                 }
222                               }
223                           }
224                       }
225
226                   }
227             }
228         }
229
230         return parent::InitDefaults();
231     }
232
233     /**
234      * DrawPublish
235      *
236      */
237     function DrawPublish ()
238     {
239
240         $szContent = "";
241
242         // Get the shared resource (one or more)
243         $aSharedResource =& $this->getTableSharedResource();
244
245         if(!is_array($aSharedResource))
246         {
247             return "";
248         }
249         
250         if (count($aSharedResource) == 0)
251         {
252             array_push($aSharedResource, array());
253         }
254
255         $aTmp = array_values($aSharedResource);
256         if (!is_array($aTmp[0]))
257             $aSharedResource = array($aSharedResource);
258
259         // Replace all collumn name in header to sort array
260         foreach($aSharedResource as $aFirstRow)
261         {
262             foreach($aFirstRow as $szKey => $szValue)
263             {
264                 array_push($this->maszFrom, "%$szKey%");
265                 array_push($this->maszTo, "<a href=\"javascript:SortBy_".$this->mszName."('$szKey')\">$szKey</a>");
266                 array_push($this->maszRegExFrom, "/\%$szKey(\|([^\%]*))?\%/");
267                 array_push($this->maszRegExTo, "<a href=\"javascript:SortBy_".$this->mszName."('$szKey')\">\\2</a>");
268             }
269             break;
270         }
271
272         // Check for next and previous page tag
273         array_push($this->maszFrom,"%NEXT_PAGE%","%PREV_PAGE%", "%NUMPAGE%", "%CURRENTPAGE%");
274         array_push($this->maszRegExFrom,"/\%NEXT_PAGE(\|([^%]*))?\%/",
275                    "/\%PREV_PAGE(\|([^%]*))?\%/");
276         if($this->mnCount == -1 || $this->mnStart+$this->mnCount >= count($aSharedResource))
277         {
278             array_push($this->maszTo, "");
279             array_push($this->maszRegExTo, "");
280         }
281         else
282         {
283             array_push($this->maszTo, "<a href=\"javascript:NextPage_".$this->mszName.
284                 "()\">".$this->mszNextButton."</a>");
285             array_push($this->maszRegExTo, "<a href=\"javascript:NextPage_".
286                        $this->mszName."()\">\\2</a>");
287         }
288         if($this->mnStart <= 0)
289         {
290             array_push($this->maszTo, "");
291             array_push($this->maszRegExTo, "");
292         }
293         else
294         {
295             array_push($this->maszTo, "<a href=\"javascript:PrevPage_".$this->mszName.
296                 "()\">".$this->mszPrevButton."</a>");
297             array_push($this->maszRegExTo, "<a href=\"javascript:PrevPage_".
298                        $this->mszName."()\">\\2</a>");
299         }
300         // Add NumPage
301         if ($this->mnCount == -1)
302             $nNumPage = 1;
303         else
304         {
305             $nNumPage = intval(count($aSharedResource) / $this->mnCount);
306         }
307         if(count($aSharedResource) % $this->mnCount) $nNumPage++;
308         array_push($this->maszTo, $nNumPage);
309
310         // Add CurrentPage
311         if ($this->mnCount == -1)
312             $nCurrentPage = 1;
313         else
314             $nCurrentPage = intval($this->mnStart / $this->mnCount)+1;
315
316         array_push($this->maszTo, $nCurrentPage);
317
318         $this->mszHeader = str_replace($this->maszFrom, $this->maszTo, $this->mszHeader);
319         $this->mszFooter = str_replace($this->maszFrom, $this->maszTo, $this->mszFooter);
320         $this->mszHeader = preg_replace($this->maszRegExFrom, $this->maszRegExTo, $this->mszHeader);
321         $this->mszFooter = preg_replace($this->maszRegExFrom, $this->maszRegExTo, $this->mszFooter);
322
323         // Set header
324         $szContent = "";
325
326         // Initialize a shared resource of what the table will display.
327         $this->maSharedResourceWidgets[$this->mszSharedResourceName."_IMAGE"] = array();
328
329         if ($this->mnCount == -1)
330             $nTmpCount = count($aSharedResource);
331         else
332             $nTmpCount = $this->mnCount;
333
334         // Parse Shared resource
335         for ($j=$this->mnStart; $j<$this->mnStart+$nTmpCount;$j++)
336         {
337             if (!isset($aSharedResource[$j]) || count($aSharedResource[$j]) == 0)
338                 break;
339
340             $this->maSharedResourceWidgets[$this->mszSharedResourceName."_IMAGE"][$j+1] = $aSharedResource[$j];
341
342             $szContent .= $this->processRow($j, $aSharedResource[$j]);   
343         }
344
345         if($szContent != "")
346             $szContent = $this->mszHeader.$szContent.$this->mszFooter;
347
348         return parent::DrawPublish() . $szContent;
349     }
350
351     function processRow($nIndex, $aszColumns)
352     {
353         // Run defined function on value in Table
354         array_push($this->maCallbackRegEx, "/\%URL(\|([^%]*))?\%/" );
355         array_push($this->maFunction, create_function('$matches', 'return urlencode($matches[2]);') );
356
357         $szTmpBody = $this->mszBody;
358
359         $szTmpBody = str_replace("%INDEX%", $nIndex+1, $szTmpBody);
360
361         foreach($aszColumns as $szKey => $szCol)
362         {
363             $szTmpBody = str_replace("%$szKey%", trim( $szCol ), $szTmpBody);         
364         }
365         $szTmpBody = $this->processIf($szTmpBody);
366
367         $szTmpBody = str_replace($this->maszBodyFrom, $this->maszBodyTo, $szTmpBody);
368         $szTmpBody = preg_replace($this->maszBodyRegExFrom, $this->maszBodyRegExTo, $szTmpBody);
369
370         $nCallback = count($this->maCallbackRegEx);
371         for ($i=0;$i<$nCallback;$i++)
372             $szTmpBody = preg_replace_callback($this->maCallbackRegEx[$i], $this->maFunction[$i], $szTmpBody);
373             
374         return $szTmpBody;
375     }
376
377     function processIf($szContent)
378     {
379         // Find position of the last if
380         $szContent = str_replace("\"", "&quot;", $szContent);
381
382         $nPosIf1 = $this->strlastpos($szContent, "\$if");
383
384         while ($nPosIf1 !== false)
385         {
386             $nPosIf2 = strpos($szContent, "\$", $nPosIf1+1);
387
388             $nPosIf3 = strpos($szContent, "\$endif\$", $nPosIf1);
389
390             $aszCondition = explode(" ", trim(substr($szContent, $nPosIf1+4, $nPosIf2-$nPosIf1-4)));
391
392             $szOper = "";
393             $aszValue = array();
394
395             // Find operator and value
396             $nEle = count($aszCondition);
397             for($i=0;$i<$nEle;$i++)
398             {
399                 if (strcasecmp(substr($aszCondition[$i], 0, 5), "OPER=") == 0)
400                 {
401                     $aOper = explode("=", $aszCondition[$i]);
402                     $szOper = strtoupper($aOper[1]);
403                 }
404                 else
405                 if (strcasecmp(substr($aszCondition[$i], 0, 6), "&quot;") == 0)
406                 {   
407                     // Get string bu strip the 6 first chars
408                     $szValue = substr($aszCondition[$i], 6);
409
410                     // If the string value don't have any space
411                     if (substr($szValue, -6) == "&quot;")
412                         $szValue = substr($szValue, 0, -6);
413                     else
414                     {
415                         // Loop thought all element until find the final &quot;
416                         for ($j=$i+1;$j<$nEle;$j++)
417                         {
418                             if (substr($aszCondition[$j], -6) == "&quot;")
419                             {
420                                 $szValue .= " ".substr($aszCondition[$j], 0, -6);
421                                 $i = $j;
422
423                                 break;
424                             }
425
426                             $szValue .= " ".$aszCondition[$j];
427                         }
428                     }
429                     array_push($aszValue, $szValue);
430                 }
431                 else
432                     array_push($aszValue, $aszCondition[$i]);
433             }
434
435             if (substr($szOper, 0, 1) == "I")
436             {
437                 $aszValue[0] = strtoupper($aszValue[0]);
438                 $aszValue[1] = strtoupper($aszValue[1]);
439             }
440
441             switch ($szOper)
442             {
443                 case "IEQ":
444                 case "EQ" : if (count($aszValue) == 2) $ret = (($aszValue[0] == $aszValue[1]) ? 1 : 0); else $ret = -1; break;
445                 case "INE":
446                 case "NE" : if (count($aszValue) == 2) $ret = (($aszValue[0] != $aszValue[1]) ? 1 : 0); else $ret = -1; break;
447                 case "IGR":
448                 case "GR" : if (count($aszValue) == 2) $ret = (($aszValue[0] >  $aszValue[1]) ? 1 : 0); else $ret = -1; break;
449                 case "ILE":
450                 case "LE" : if (count($aszValue) == 2) $ret = (($aszValue[0] <  $aszValue[1]) ? 1 : 0); else $ret = -1; break;
451                 case "IGRE":
452                 case "GRE": if (count($aszValue) == 2) $ret = (($aszValue[0] >= $aszValue[1]) ? 1 : 0); else $ret = -1; break;
453                 case "ILEE":
454                 case "LEE": if (count($aszValue) == 2) $ret = (($aszValue[0] <= $aszValue[1]) ? 1 : 0); else $ret = -1; break;
455                 default : $ret = -1;
456             }
457
458             if ($ret == 1) // condition returned true
459             {
460                 $szContent = str_replace(substr($szContent, $nPosIf1, $nPosIf3+7-$nPosIf1),
461                                          substr($szContent, $nPosIf2+1, $nPosIf3-$nPosIf2-1), $szContent);
462             }
463             else
464             if ($ret == 0) // condition returned false
465             {
466                 $szContent = str_replace(substr($szContent, $nPosIf1, $nPosIf3+7-$nPosIf1), "", $szContent);
467             }
468             else // Something goes wrong.
469             {
470                 $szContent = str_replace(substr($szContent, $nPosIf1, $nPosIf3+7-$nPosIf1),
471                                          "<font color=red>[".str_replace("\$" ," ",
472                                          htmlentities(substr($szContent, $nPosIf1, $nPosIf3+7-$nPosIf1)))."]</font><br>", $szContent);
473             }
474
475             $nPosIf1 = $this->strlastpos($szContent, "\$if");
476         }
477
478         $szContent = str_replace("&quot;", "\"", $szContent);
479
480         return $szContent;
481     }
482
483     /**
484      * return javascript functions required for this widget
485      */
486     function GetJavascriptFunctions()
487     {
488         $aReturn = array();
489
490         $szJsFunctionName = "SortBy_".$this->mszName;
491         $szFunction = <<<EOT
492 /**
493  * set the active collumn to sortby
494  */
495 function {$szJsFunctionName}(sCol)
496 {
497     {$this->mszHTMLForm}.SORTBY_{$this->mszName}.value = sCol;
498     {$this->mszHTMLForm}.submit();
499 }
500 EOT;
501         $aReturn[$szJsFunctionName] = $szFunction;
502
503         $szJsFunctionName = "NextPage_".$this->mszName;
504         $szFunction = <<<EOT
505 /**
506  * Show Next page
507  */
508 function {$szJsFunctionName}(sCol)
509 {
510     var nTot = parseInt({$this->mnStart}) + parseInt({$this->mnCount});
511     {$this->mszHTMLForm}.START_{$this->mszName}.value = nTot;
512
513     {$this->mszHTMLForm}.submit();
514 }
515 EOT;
516         $aReturn[$szJsFunctionName] = $szFunction;
517
518
519         $szJsFunctionName = "PrevPage_".$this->mszName;
520         $szFunction = <<<EOT
521 /**
522  * Show previous page
523  */
524 function {$szJsFunctionName}(sCol)
525 {
526     var nTot = parseInt({$this->mnStart}) - parseInt({$this->mnCount});
527
528     {$this->mszHTMLForm}.START_{$this->mszName}.value = nTot;
529
530     if ({$this->mszHTMLForm}.START_{$this->mszName}.value < 0)
531         {$this->mszHTMLForm}.START_{$this->mszName}.value = 0;
532
533     {$this->mszHTMLForm}.submit();
534 }
535 EOT;
536         $aReturn[$szJsFunctionName] = $szFunction;
537
538
539
540         return $aReturn;
541     }
542
543     /**
544      * handle the user using this tool as part of a toolset
545      */
546     function  ParseURL()
547     {
548         if ($this->isVarSet( "SORTBY_".strtoupper($this->mszName) ))
549             $this->mszSortBy = $this->getVar( "SORTBY_".strtoupper($this->mszName) );
550
551         if ($this->isVarSet( "START_".strtoupper($this->mszName) ))
552             $this->mnStart = $this->getVar( "START_".strtoupper($this->mszName) );
553
554         if ($this->isVarSet( "COUNT_".strtoupper($this->mszName) ))
555             $this->mnCount = $this->getVar( "COUNT_".strtoupper($this->mszName) );
556
557         return parent::ParseURL();
558     }
559
560     /**
561      * return hidden variables required for this tool
562      */
563     function GetHTMLHiddenVariables()
564     {
565           $szVariable = "SORTBY_".$this->mszName;
566         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"".$this->mszSortBy."\">";
567         $aReturn[$szVariable] = $szValue;
568
569         $szVariable = "START_".$this->mszName;
570         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"".$this->mnStart."\">";
571         $aReturn[$szVariable] = $szValue;
572
573         $szVariable = "COUNT_".$this->mszName;
574         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"".$this->mnCount."\">";
575         $aReturn[$szVariable] = $szValue;
576
577         $szVariable = "REVERSE_".$this->mszName;
578         $szValue = " <INPUT TYPE=HIDDEN NAME=$szVariable VALUE=\"".(($this->mbReverseSort) ? "TRUE": "FALSE")."\">";
579         $aReturn[$szVariable] = $szValue;
580
581         return $aReturn;
582     }
583
584
585     function strlastpos($haystack, $needle)
586     {
587         // flip both strings around and search, then adjust position based on string lengths
588         if (strpos(strrev($haystack), strrev($needle)) !== false)
589             return strlen($haystack) - strlen($needle) - strpos(strrev($haystack), strrev($needle));
590         else
591             return false;
592     }
593
594     function getTableSharedResource()
595     {
596         if(count($this->maoSharedResources) > 0)
597         {
598             return $this->getMultipleSharedResource();
599         }
600
601         if (strpos($this->mszSharedResourceName,".") !== false)
602             $aPath = explode(".", $this->mszSharedResourceName);
603         else
604             $aPath = array($this->mszSharedResourceName);
605         if(isset($this->maSharedResourceWidgets[$aPath[0]]))
606         {
607             $aSharedResource = $this->maSharedResourceWidgets[$aPath[0]];
608
609             $bInSRWidget = false;
610             // Check if the Shared resource is a widget
611             if (isset($aSharedResource->mszWidgetName) &&
612                 $aSharedResource->mszWidgetName == "SharedResource")
613             {
614                 $bInSRWidget = true;
615                 $aSharedResource = $aSharedResource->maszContents;
616             }
617
618             for($i=1;$i<count($aPath); $i++)
619             {
620                 if ($bInSRWidget)
621                     $aSharedResource = $aSharedResource[strtoupper($aPath[$i])];
622                 else
623                     $aSharedResource = $aSharedResource[$aPath[$i]];
624             }
625
626             // If sortby is set, then sort the table
627             if ($this->mszSortBy != "")
628                 usort($aSharedResource, array($this, "SortBy"));               
629
630             return $aSharedResource;
631         }
632
633         return "";
634     }
635
636     function getMultipleSharedResource()
637     {
638         // Get all the SR
639         $numSR = 0;
640         $aSRArray = array();
641         $aSRDesc = array();
642         $aSRStart = array();
643         $aSRCount = array();
644         $numSR = count($this->maoSharedResources);
645         for($i=0; $i<$numSR; $i++)
646         {
647             $oSRDesc =& $this->maoSharedResources[$i];
648             if(!isset($oSRDesc["SRNAME"]) || !$oSRDesc["SRNAME"] != "" ||
649                !isset($this->maSharedResourceWidgets[$oSRDesc["SRNAME"]]))
650                 continue;
651
652             // process the condition or use the entire array
653             if(isset($oSRDesc["CONDITION"]) && $oSRDesc["CONDITION"] != "")
654                 $oSR =& $this->processArrayCondition(
655                             $this->maSharedResourceWidgets[$oSRDesc["SRNAME"]],
656                             $oSRDesc["CONDITION"]);
657             else
658                 $oSR = $this->maSharedResourceWidgets[$oSRDesc["SRNAME"]];
659
660             // Make sure that the new SR has at least one row
661             if(count($oSR) <= 0)
662                 continue;
663
664             // If sortby is set, then sort the table
665             if (isset($oSRDesc["SORTBY"]) && $oSRDesc["SORTBY"] != "")
666             {
667                 $szSortBy = $this->mszSortBy;
668                 $this->mszSortBy = $oSRDesc["SORTBY"];
669                 usort($oSR, array($this, "SortBy"));
670                 $this->mszSortBy = $szSortBy;
671             }
672
673             // if there's a limit, get it.
674             $nLimit = -1;
675             $nStart = -1;
676             if(isset($oSRDesc["LIMIT"]) && is_numeric($oSRDesc["LIMIT"]))
677                 $nLimit = $oSRDesc["LIMIT"];
678             if(isset($oSRDesc["START"]) && is_numeric($oSRDesc["START"]))
679                 $nStart = $oSRDesc["START"];
680             if($nLimit > -1 || $nStart > -1)
681             {
682                 if($nLimit == -1)
683                     $nLimit = count($oSR);
684                 if($nStart == -1)
685                     $nStart = 0;
686
687                 $oSR =& array_slice($oSR, $nStart, $nLimit);
688             }
689
690             // If postsortby is set, then resort the table
691             if (isset($oSRDesc["POSTSORTBY"]) && $oSRDesc["POSTSORTBY"] != "")
692             {
693                 $szSortBy = $this->mszSortBy;
694                 $this->mszSortBy = $oSRDesc["POSTSORTBY"];
695                 usort($oSR, array($this, "SortBy"));
696                 $this->mszSortBy = $szSortBy;
697             }
698
699             // Put the new SR in the SR array
700             array_push($aSRArray, $oSR);
701             array_push($aSRDesc, $oSRDesc);
702             array_push($aSRStart, 0);
703             array_push($aSRCount, count($oSR));
704         }
705
706         $aSharedResource = array();
707         $numSREoa = 0;
708         $numSR = count($aSRArray);
709         while($numSREoa < $numSR)
710         {
711             $aSRPage = array();
712             for($i=0; $i<$numSR; $i++)
713             {
714                 if($aSRStart[$i] == -1)
715                 {
716                     continue;
717                 }
718
719                 // Get the count
720                 if ($this->mnCount == -1)
721                     $nCount = count($aSRArray[$i]);
722                 else
723                     $nCount = $this->mnCount - count($aSRPage);
724
725                 if(isset($aSRDesc[$i]["COUNT"]) &&
726                    is_numeric($aSRDesc[$i]["COUNT"]))
727                     $nCount = ($nCount <= $aSRDesc[$i]["COUNT"])? $nCount :
728                                                        $aSRDesc[$i]["COUNT"];
729
730                 for($j=0; $j<$nCount; $j++)
731                 {
732                     if(($aSRStart[$i]+$j) >= $aSRCount[$i])
733                     {
734                         $j = 0;
735                         $aSRStart[$i] = -1;
736                         $numSREoa++;
737                         break;
738                     }
739                     array_push($aSRPage, $aSRArray[$i][($aSRStart[$i]+$j)]);
740
741                     if($this->mnCount != -1 && count($aSRPage) == $this->mnCount)
742                     {
743                         $j++;
744                         break;
745                     }
746                 }
747                 $aSRStart[$i] += $j;
748
749                 if($this->mnCount != -1 && count($aSRPage) == $this->mnCount)
750                     break;
751             }
752
753             // Sort the page
754             if ($this->mszSortBy != "")
755             {
756                 usort($aSRPage, array($this, "SortBy"));
757             }
758             $aSharedResource=array_merge_recursive($aSharedResource, $aSRPage);
759         }
760
761         return $aSharedResource;
762     }
763
764     function processArrayCondition($aArray, $szCondition)
765     {
766         $aOutArray = array();
767         $numRow = count($aArray);
768         $aszKey = array_keys($aArray);
769
770         for($i=0; $i<$numRow; $i++)
771         {
772             $szCondition = str_replace("&quot;", "\"", $szCondition);
773
774             $bResult = false;
775             $nCnt = count($aArray[$aszKey[$i]]);
776             $aszKeys = array_keys($aArray[$aszKey[$i]]);
777             $szExp = $szCondition;
778             if(strpos($szExp, "%") !== false)
779             {
780                 for($j=0;$j<$nCnt;$j++)
781                 {
782                     $szExp = str_replace("\"%".$aszKeys[$j]."%\"",
783                                          "\"".$aArray[$aszKey[$i]]
784                                          [$aszKeys[$j]]."\"",
785                                          $szExp);
786                     $szExp = str_replace("%".$aszKeys[$j]."%",
787                                          "\"".$aArray[$aszKey[$i]]
788                                          [$aszKeys[$j]]."\"",
789                                          $szExp);
790                 }
791             }
792
793             if(evaluateExpression($szExp))
794                 array_push($aOutArray, $aArray[$aszKey[$i]]);
795         }
796
797         return $aOutArray;
798     }
799 }
800 ?>
801
Note: See TracBrowser for help on using the browser.