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

root/Chameleon/trunk/Chameleon/CWC2ButtonCache.php

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

Latest Chameleon code checkout from previous repository

Line 
1 <?php
2 /**
3  * handle a cache of CWC2 buttons and provide convenience functions for accessing them.
4  */
5 include_once("session.inc.php");
6 include_once( COMMON."buttonizer/buttonizer.php" );
7
8 /**
9  * create a button based on a graphic, a label, and some parameters.  The
10  * button is cached so it is only actually created once.
11  */
12 function makeButton( $szJSCallback, $szJSParam, $szClassName, $szGraphic, $szLabel, $szAltText, $aExtraParams = array() )
13 {
14     $aExtraParams['graphic'] = findFile( $szGraphic );
15     $aExtraParams['label'] = $szLabel;
16     $szResourceName = strtoupper( $szClassName.'PopupStyleResource' );
17     
18     $bButtonize = false;
19     if(isset($_SESSION[$szResourceName]) &&
20        isset($_SESSION[$_SESSION[$szResourceName]]))
21     {
22         $aResource = $_SESSION[$_SESSION[$szResourceName]];
23         $bButtonize = true;
24     }
25     if ($szJSParam == "")
26         $szJSParam = "null";
27     elseif ($szJSParam == "true" || $szJSParam === true)
28         $szJSParam = "true";
29     elseif ($szJSParam == "false" || $szJSParam === false)
30         $szJSParam = "false";
31     elseif (!is_numeric( $szJSParam ))
32         $szJSParam = '"'.$szJSParam.'"';
33  
34     if ($bButtonize)
35     {
36         $szImageID = "b".md5($szJSCallback.$szJSParam.$szClassName.$szGraphic.$szLabel);
37
38         $szNormalImage = 'null';
39         $nNormalWidth = '';
40         $nNormalHeight = '';
41         if (isset($aResource['normal']))
42         {
43              $aResource['normal'] = array_merge( $aResource['normal'], $aExtraParams );
44              $szNormalImage = '"'.GetImage( $szImageID, $aResource['normal'] ).'"';
45              $nNormalWidth = $aResource['normal']['width'];
46              $nNormalHeight = $aResource['normal']['height'];
47         }
48         $szSelectedImage = 'null';
49         if (isset($aResource['selected']))
50         {
51              $aResource['selected'] = array_merge( $aResource['selected'], $aExtraParams );
52              $szSelectedImage = '"'.GetImage( $szImageID, $aResource['selected']).'"';
53         }
54         $szHoverImage = 'null';
55         if (isset($aResource['hover']))
56         {
57              $aResource['hover'] = array_merge( $aResource['hover'], $aExtraParams );
58              $szHoverImage = '"'.GetImage( $szImageID, $aResource['hover']).'"';
59         }
60         $szDisabledImage = 'null';
61         if (isset($aResource['disabled']))
62         {
63              $aResource['disabled'] = array_merge( $aResource['disabled'], $aExtraParams );
64             $szDisabledImage = '"'.GetImage( $szImageID, $aResource['disabled']).'"';
65         }
66
67
68         $szReturn = <<<EOT
69 <img name="{$szImageID}" id="{$szImageID}" src={$szNormalImage} width="{$nNormalWidth}" height="{$nNormalHeight}" ALT="{$szAltText}" TITLE="{$szAltText}" BORDER="0">
70 <script language="Javascript" type="text/javascript">
71 goCWCButtonManager.AddButton( new CWCButton("{$szImageID}", 0, "null", $szJSCallback, $szJSParam, $szNormalImage, $szHoverImage, $szSelectedImage, $szDisabledImage) );
72 </script>
73 EOT;
74     }
75     else
76     {
77         //none-buttonized-version :)
78         $szReturn = <<<EOT
79 <input type="button" onclick='$szJSCallback(null, $szJSParam);' value="$szLabel">
80 EOT;
81     }
82     
83     return $szReturn;
84 }
85
86 /**
87  * GetImage returns the name of an image to use for a particular resource and state
88  */
89 function GetImage( $szImageID, $aParams )
90 {
91         //TODO: figure out an effective caching mechanism for this?
92         //using $szImageID
93         
94         //load platform specific behaviours
95         $aParams["gd_module"] = "gd2";
96         $aParams["freetype"] = "FreeType";
97         
98         //generate the cached image.
99         $szImageFile = md5( implode( "", $aParams ) ).".png";
100         
101         $szImagePath = $_SESSION['gszButtonCachePath'].$szImageFile;
102
103         $szImageWebPath = $_SESSION['gszButtonCacheWebPath'].$szImageFile;
104         
105         //fix relative paths if necessary
106         $aPaths = array( 'graphic',
107                          'border_tl_image',
108                          'border_t_image',
109                          'border_tr_image',
110                          'border_r_image',
111                          'border_br_image',
112                          'border_b_image',
113                          'border_bl_image',
114                          'border_l_image',
115                          'backgroundimage',
116                          'labelfont'
117                         );
118         
119         foreach ($aPaths as $aPath)
120             if (isset($aParams[$aPath]))
121             {
122                 $aParams[$aPath] = findFile( $aParams[$aPath] );
123             }
124         //cache the URL and generate the image, if required.
125         if (!isset($aParams['usecache']) || (isset($aParams['usecache']) && $aParams['usecache']))
126         {
127             //TODO: can we effectively cache this?
128             //$_SESSION[$szCacheName] = $szImageWebPath;
129             if (!is_file( $szImagePath ))
130             {
131                 buttonize( $szImagePath, $aParams );
132             }
133         }
134         else
135         {
136             buttonize( $szImagePath, $aParams );
137         }
138         
139         return $szImageWebPath;
140 }
141
142 /**
143  * this function attempts to locate a file in one of
144  * (possibly) several locations.  If the original file
145  * path is absolute, it will be returned as is.  If it
146  * is relative, the it will be looked for in four possible
147  * cases.
148  *
149  * 1. relative to app path in skin search paths
150  * 2. relative to app path
151  * 3. relative to chameleon in skin search paths
152  * 4. relative to chameleon
153  *
154  * @param szFilePath an absolute or relative path and file
155  *        name to locate
156  * @return an absolute path to the file or false if the file
157  *         could not be found.
158  */
159 function findFile( $szFilePath )
160 {
161     /* case 1 - skin search path relative to app */
162     foreach( array_reverse($_SESSION['gaszSkinSearchPath']) as $szPath )
163     {
164         $szTempPath = resolvePath2( $szFilePath, $_SESSION['gszAppPath'].'/'.$szPath."/" );
165         if ($szTempPath != "" && file_exists($szTempPath))
166         {
167             return $szTempPath;
168         }
169     }
170
171     /* case 2 - relative to app */
172     $szTempPath = resolvePath2( $szFilePath, $_SESSION['gszAppPath'] );
173     if ($szTempPath != "" && file_exists($szTempPath))
174     {
175         return $szTempPath;
176     }
177
178     /* case 3 - skin search path relative to chameleon */
179     foreach( array_reverse($_SESSION['gaszSkinSearchPath']) as $szPath )
180     {
181         $szTempPath = resolvePath2( $szFilePath, $_SESSION['gszCorePath'].'/'.$szPath.'/' );
182         if ($szTempPath != "" && file_exists($szTempPath))
183         {
184             return $szTempPath;
185         }
186     }
187
188     /* case 4 - relative to chameleon */
189     $szTempPath = resolvePath2( $szFilePath, $_SESSION['gszCorePath'] );
190     if ($szTempPath != "" && file_exists($szTempPath))
191     {
192         return $szTempPath;
193     }
194
195     return false;
196 }
197
198 function fileSystemToURL( $szFileSystemPath )
199 {
200     if (strncmp( $szFileSystemPath, $_SESSION['gszAppPath'], strlen($_SESSION['gszAppPath']) ) == 0)
201         return $_SESSION['gszAppWebPath'].'/'.substr( $szFileSystemPath, strlen($_SESSION['gszAppPath']));
202
203     if (strncmp( $szFileSystemPath, $_SESSION['gszCorePath'], strlen($_SESSION['gszCorePath']) ) == 0)
204         return $_SESSION['gszCoreWebPath'].'/'.substr($szFileSystemPath,strlen($_SESSION['gszCorePath']));
205
206     return $szFileSystemPath;
207 }
208
209 /**
210  * this function determines if a path represents an absolute path and returns
211  * true if it is, and false if it is not
212  * @param szPath the path to test
213  * @result boolean, true if the path is absolute, false otherwise
214  */
215 function isAbsolutePath( $szPath )
216 {
217     if ($szPath == "") return false;
218     if ($szDestPath[0] == "/" || preg_match('/^(\w:)/', $szDestPath))
219     {
220         return true;
221     }
222     else
223     {
224         return false;
225     }
226 }
227
228 /**
229  * This function translate $szDestPath (relative or absolute)
230  * to a absolute path based on $szOrigPath.
231  *
232  * @param $szDestPath Destination path to translate
233  * @param $szOrigPath Refenrece path
234  */
235 function resolvePath2 ($szDestPath, $szOrigPath)
236 {
237     // If one or other path is missing or absolute, return it.
238     if ($szDestPath == "") return $szOrigPath;
239     if ($szOrigPath == "") return $szDestPath;
240     if ($szDestPath[0] == "/" || preg_match('/^(\w:)/', $szDestPath))
241     {
242         return $szDestPath;
243     }
244
245     // If windows prefix (eg: c:) get it
246     $szPrefix = "";
247     if ($szOrigPath[0] != "/")
248     {
249         if (preg_match('/^(\w:)(.*)/i', $szOrigPath, $aMatch))
250         {
251             $szPrefix = $aMatch[1];
252             $szOrigPath = $aMatch[2];
253         }
254         else
255         {
256             $szOrigPath = "/".$szOrigPath;
257         }
258     }
259
260     // Make sure path finishing with "/"
261     if ($szOrigPath[strlen($szOrigPath)-1] != "/" &&
262         !is_dir($szOrigPath))
263         $szOrigPath = dirname($szOrigPath)."/";
264     if ($szOrigPath[strlen($szOrigPath)-1] != "/")
265         $szOrigPath = $szOrigPath."/";
266
267     $szPath = $szOrigPath.$szDestPath;
268
269     // Remove repetitive "/"
270     $szPath = ereg_replace ("/+", "/", $szPath);
271
272     $szPath = iterate_ereg_replace ("/\./", "/", $szPath);
273     $szPath = iterate_ereg_replace ("/[^(/|\.)]+/\.\./", "/", $szPath);
274
275     //Rest of the function
276     return $szPrefix.$szPath;
277 }
278
279 /**
280  * Recursive ereg_replace
281  */
282 function iterate_ereg_replace ( $szPattern, $szReplacement, $szString)
283 {
284     $szResult = $szString;
285     do
286     {
287         $szString = $szResult;
288         $szResult = ereg_replace ($szPattern, $szReplacement, $szString);
289     }
290     while ($szResult != $szString);
291
292     return $szResult;
293 }
294
295 ?>
296
Note: See TracBrowser for help on using the browser.