source: trunk/phpgwapi/inc/class.javascript.inc.php @ 3090

Revision 3090, 8.5 KB checked in by amuller, 14 years ago (diff)

Ticket #1036 - Arrumando geração do js quando arq nao existe

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1<?php
2  /**************************************************************************\
3  * eGroupWare API - JavaScript                                              *
4  * Written by Dave Hall skwashd at phpgroupware.org                         *
5  * Copyright (C) 2003 Free Software Foundation Inc                          *
6  * -------------------------------------------------------------------------*
7  * This library is part of the eGroupWare API                               *
8  * http://www.egroupware.org/api                                            *
9  * ------------------------------------------------------------------------ *
10  *  This program is Free Software; you can redistribute it and/or modify it *
11  *  under the terms of the GNU General Public License as published by the   *
12  *  Free Software Foundation; either version 2 of the License, or (at your  *
13  *  option) any later version.                                              *
14  \**************************************************************************/
15
16       /**
17       * eGroupWare javascript support class
18       *
19       * Only instanstiate this class using:
20       * <code>
21       *  if(!@is_object($GLOBALS['phpgw']->js))
22       *  {
23       *    $GLOBALS['phpgw']->js = CreateObject('phpgwapi.javascript');
24       *  }
25       * </code>
26       *
27       * This way a theme can see if this is a defined object and include the data,
28       * while the is_object() wrapper prevents whiping out existing data held in
29       * this instance variables, primarily the $files variable.
30       *
31       * Note: The package arguement is the subdirectory of js - all js should live in subdirectories
32       *
33       * @package phpgwapi
34       * @subpackage sessions
35       * @abstract
36       * @author Dave Hall
37       * @copyright &copy; 2003 Free Software Foundation
38       * @license GPL
39       * @uses template
40       */
41        class javascript
42        {
43                /**
44                * @var array elements to be used for the on(Un)Load attributes of the body tag
45                */
46                var $body;
47
48                /**
49                * @var array list of validated files to be included in the head section of a page
50                */
51                private $files = array( );
52
53                /**
54                * @var object used for holding an instance of the Template class
55                */
56                var $t;
57               
58                /**
59                * Constructor
60                *
61                * Initialize the instance variables
62                */
63                function javascript()
64                {
65                        //$this->t = CreateObject('phpgwapi.Template', ExecMethod('phpgwapi.phpgw.common.get_tpl_dir','phpgwapi'));
66                        //not currently used, but will be soon - I hope :)
67                }
68
69               
70                /**
71                * Returns the javascript required for displaying a popup message box
72                *
73                * @param string $msg the message to be displayed to user
74                * @returns string the javascript to be used for displaying the message
75                */
76                function get_alert($msg)
77                {
78                  return 'return alert("'.lang($msg).'");';
79                }
80
81                /**
82                * Adds on(Un)Load= attributes to the body tag of a page
83                *
84                * @returns string the attributes to be used
85                */
86                function get_body_attribs()
87                {
88                        $js = '';
89                        foreach(array('onLoad','onUnload','onResize') as $what)
90                        {
91                                if (!empty($this->body[$what]))
92                                {
93                                        $js .= ' '.$what.'="' . str_replace(array('"','\\'),array('\\"','\\\\'),$this->body[$what]) . '"';
94                                }
95                        }
96                        return $js;
97                }
98
99                /**
100                * Returns the javascript required for displaying a confirmation message box
101                *
102                * @param string $msg the message to be displayed to user
103                * @returns string the javascript to be used for displaying the message
104                */
105                function get_confirm($msg)
106                {
107                        return 'return confirm("'.lang($msg).'");';
108                }
109               
110                /**
111                * Used for generating the list of external js files to be included in the head of a page
112                *
113                * NOTE: This method should only be called by the template class.
114                * The validation is done when the file is added so we don't have to worry now
115                *
116                * @returns string the html needed for importing the js into a page
117                */
118                function get_script_links()
119                {
120                        $links = '';
121                        if(!empty($this->files) && is_array($this->files))
122                        {
123                                $links = "\n\t\t<!--JS Imports from phpGW javascript class -->";
124                                foreach($this->files as $app => $packages)
125                                {
126                                        if(!empty($packages) && is_array($packages))
127                                        {
128                                                foreach($packages as $pkg => $files)
129                                                {
130                                                        $script = '';
131                                                        if(!empty($files) && is_array($files))
132                                                        {
133                                                                foreach($files as $file => $ignored)
134                                                                {
135                                                                        $jsFilePath=PHPGW_INCLUDE_ROOT.SEP.$app.SEP.'js'.SEP.$pkg.SEP.$file.'.js';
136                                                                        if ($GLOBALS['phpgw_info']['server']['jspacker'] == "True")
137                                                                        {
138
139                                                                                $packFilePath=PHPGW_INCLUDE_ROOT.SEP.$app.SEP.'js'.SEP.$pkg.SEP.$pkg.'.jspack.js';
140                                                                                if (!file_exists($packFilePath) || filemtime($jsFilePath) > filemtime($packFilePath)) // Is newer
141                                                                                {
142                                                                                        $regeratePack = True;
143                                                                                }
144                                                                                $script .= file_get_contents($jsFilePath);
145                                                                        }
146                                                                        else
147                                                                        {
148                                                                                $_script = '<script type="text/javascript" src="%s/%s/js/%s/%s.js?%d">/* %s  */</script>';
149                                                                                $links .= "\n\t\t" . sprintf( $_script, $GLOBALS[ 'phpgw_info' ][ 'server' ][ 'webserver_url' ], $app, $pkg, $file, filemtime( $jsFilePath ), $file );
150                                                                        }
151
152                                                                        $this -> unset_script_link( $app, $pkg, $file );
153                                                                }
154                                                        }
155
156                                                        if ( $script && $GLOBALS['phpgw_info']['server']['jspacker'] == "True")
157                                                        {
158                                                                if (!file_exists($packFilePath) || $regeratePack)
159                                                                {
160                                                                        $fp = fopen($packFilePath, 'w');
161                                                                        require_once('class.JavaScriptPacker.php');
162                                                                        $packer = new JavaScriptPacker($script, 'High ASCII', true, true);
163                                                                        $packed = $packer->pack();
164                                                                        fwrite($fp, $packed);
165                                                                        fclose($fp);
166                                                                }
167                                                                $links .= '<script type="text/javascript" src="'
168                                                                        .$GLOBALS['phpgw_info']['server']['webserver_url']
169                                                                        ."/$app/js/$pkg/$pkg" . ".jspack.js\"></script>\n";
170                                                        }
171                                                }
172                                        }
173                                }
174                        }
175                        return $links;
176                }
177
178                /**
179                * Sets an onLoad action for a page
180                *
181                * @param string javascript to be used
182                */
183                function set_onload($code)
184                {
185                        $this->body['onLoad'] .= $code;
186                }
187
188                /**
189                * Sets an onUnload action for a page
190                *
191                * @param string javascript to be used
192                */
193                function set_onunload($code)
194                {
195                        $this->body['onUnload'] .= $code;
196                }
197
198                /**
199                * Sets an onResize action for a page
200                *
201                * @param string javascript to be used
202                */
203                function set_onresize($code)
204                {
205                        $this->body['onResize'] .= $code;
206                }
207
208                /**
209                * DO NOT USE - NOT SURE IF I AM GOING TO USE IT - ALSO IT NEEDS SOME CHANGES!!!!
210                * Used for removing a file or package of files to be included in the head section of a page
211                *
212                * @param string $app application to use
213                * @param string $package the name of the package to be removed
214                * @param string $file the name of a file in the package to be removed - if ommitted package is removed
215                */
216                function unset_script_link($app, $package, $file=False)
217                {
218                        if($file !== False)
219                        {
220                                unset($this->files[$app][$package][$file]);
221                        }
222                        else
223                        {
224                                unset($this->files[$app][$package]);
225                        }
226                }
227
228                /**
229                * Checks to make sure a valid package and file name is provided
230                *
231                * @param string $package package to be included
232                * @param string $file file to be included - no ".js" on the end
233                * @param string $app application directory to search - default = phpgwapi
234                * @returns bool was the file found?
235                */
236                function validate_file( $package, $file, $app = NULL, $stack = false )
237                {
238                        if ( $app == NULL )
239                                $app = 'phpgwapi';
240
241                        $_file = PHPGW_INCLUDE_ROOT . SEP . $app . SEP . 'js' . SEP . $package . SEP. $file . '.js';
242                        if ( is_readable( $_file ) )
243                        {
244                                unset( $_file );
245
246                                if ( ! array_key_exists( $app, $this -> files ) )
247                                        if ( $stack )
248                                                $this -> files = array_merge( array( $app => array( ) ), $this -> files) ;
249                                        else
250                                                $this -> files[ $app ] = array( );
251
252                                if ( ! array_key_exists( $package, $this -> files[ $app ] ) )
253                                        if ( $stack )
254                                                $this -> files[ $app ] = array_merge( array( $package => array( ) ), $this -> files[ $app ] );
255                                        else
256                                                $this -> files[ $app ][ $package ] = array( );
257
258                                if ( $stack )
259                                        $this -> files[ $app ][ $package ] = array_merge( array( $file => $file ), $this -> files[ $app ][ $package ] );
260                                else
261                                        $this -> files[ $app ][ $package ][ $file ] = $file;
262
263                                return true;
264                        }
265                        return false;
266                }
267
268                function get_source( $param )
269                {
270                        ob_start( );
271                        $source = '';
272                        foreach( explode( ';', $param[ 'source' ] ) as $script )
273                                if ( strrpos( $script, '.php' ) != strlen( $script ) - 4 )
274                                        $source .= file_get_contents( realpath( dirname( __FILE__ ) . '/../..' ) . $script );
275                                else
276                                {
277                                        include_once realpath( dirname( __FILE__ ) . '/../..' ) . $script;
278                                        $source .= ob_get_contents( );
279                                        ob_clean( );
280                                }
281
282                        echo $source;
283                        exit;
284                }
285        }
286?>
Note: See TracBrowser for help on using the repository browser.