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

Revision 2623, 8.0 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1009 - Organizando o HTML do template default.

  • 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                                                        if(!empty($files) && is_array($files))
131                                                        {
132                                                                foreach($files as $file => $ignored)
133                                                                {
134                                                                        $jsFilePath=PHPGW_INCLUDE_ROOT.SEP.$app.SEP.'js'.SEP.$pkg.SEP.$file.'.js';
135                                                                        if ($GLOBALS['phpgw_info']['server']['jspacker'] == "True")
136                                                                        {
137
138                                                                                $packFilePath=PHPGW_INCLUDE_ROOT.SEP.$app.SEP.'js'.SEP.$pkg.SEP.$pkg.'.jspack.js';
139                                                                                if (filemtime($jsFilePath) > filemtime($packFilePath)) // Is newer
140                                                                                {
141                                                                                        $regeratePack = True;
142                                                                                }
143                                                                                $script .= file_get_contents($jsFilePath);
144                                                                        }
145                                                                        else
146                                                                        {
147                                                                                $_script = '<script type="text/javascript" src="%s/%s/js/%s/%s.js?%d"></script>';
148                                                                                $links .= "\n\t\t" . sprintf( $_script, $GLOBALS[ 'phpgw_info' ][ 'server' ][ 'webserver_url' ], $app, $pkg, $file, filemtime( $jsFilePath ) );
149                                                                        }
150
151                                                                        $this -> unset_script_link( $app, $pkg, $file );
152                                                                }
153                                                        }
154
155                                                        if ($GLOBALS['phpgw_info']['server']['jspacker'] == "True")
156                                                        {
157                                                                if (!file_exists($packFilePath) || $regeratePack)
158                                                                {
159                                                                        $fp = fopen($packFilePath, 'w');
160                                                                        require_once('class.JavaScriptPacker.php');
161                                                                        $packer = new JavaScriptPacker($script, 'High ASCII', true, true);
162                                                                        $packed = $packer->pack();
163                                                                        fwrite($fp, $packed);
164                                                                        fclose($fp);
165                                                                }
166                                                                $links .= '<script type="text/javascript" src="'
167                                                                        .$GLOBALS['phpgw_info']['server']['webserver_url']
168                                                                        ."/$app/js/$pkg/$pkg" . ".jspack.js\"></script>\n";
169                                                        }
170                                                }
171                                        }
172                                }
173                        }
174                        return $links;
175                }
176
177                /**
178                * Sets an onLoad action for a page
179                *
180                * @param string javascript to be used
181                */
182                function set_onload($code)
183                {
184                        $this->body['onLoad'] .= $code;
185                }
186
187                /**
188                * Sets an onUnload action for a page
189                *
190                * @param string javascript to be used
191                */
192                function set_onunload($code)
193                {
194                        $this->body['onUnload'] .= $code;
195                }
196
197                /**
198                * Sets an onResize action for a page
199                *
200                * @param string javascript to be used
201                */
202                function set_onresize($code)
203                {
204                        $this->body['onResize'] .= $code;
205                }
206
207                /**
208                * DO NOT USE - NOT SURE IF I AM GOING TO USE IT - ALSO IT NEEDS SOME CHANGES!!!!
209                * Used for removing a file or package of files to be included in the head section of a page
210                *
211                * @param string $app application to use
212                * @param string $package the name of the package to be removed
213                * @param string $file the name of a file in the package to be removed - if ommitted package is removed
214                */
215                function unset_script_link($app, $package, $file=False)
216                {
217                        if($file !== False)
218                        {
219                                unset($this->files[$app][$package][$file]);
220                        }
221                        else
222                        {
223                                unset($this->files[$app][$package]);
224                        }
225                }
226
227                /**
228                * Checks to make sure a valid package and file name is provided
229                *
230                * @param string $package package to be included
231                * @param string $file file to be included - no ".js" on the end
232                * @param string $app application directory to search - default = phpgwapi
233                * @returns bool was the file found?
234                */
235                function validate_file( $package, $file, $app = NULL, $stack = false )
236                {
237                        if ( $app == NULL )
238                                $app = 'phpgwapi';
239
240                        $_file = PHPGW_INCLUDE_ROOT . SEP . $app . SEP . 'js' . SEP . $package . SEP. $file . '.js';
241                        if ( is_readable( $_file ) )
242                        {
243                                unset( $_file );
244
245                                if ( ! array_key_exists( $app, $this -> files ) )
246                                        if ( $stack )
247                                                $this -> files = array_merge( array( $app => array( ) ), $this -> files) ;
248                                        else
249                                                $this -> files[ $app ] = array( );
250
251                                if ( ! array_key_exists( $package, $this -> files[ $app ] ) )
252                                        if ( $stack )
253                                                $this -> files[ $app ] = array_merge( array( $package => array( ) ), $this -> files[ $app ] );
254                                        else
255                                                $this -> files[ $app ][ $package ] = array( );
256
257                                if ( $stack )
258                                        $this -> files[ $app ][ $package ] = array_merge( array( $file => $file ), $this -> files[ $app ][ $package ] );
259                                else
260                                        $this -> files[ $app ][ $package ][ $file ] = $file;
261
262                                return true;
263                        }
264                        error_log( $_file . "\n\n", 3, '/tmp/log' );
265                        return false;
266                }
267        }
268?>
Note: See TracBrowser for help on using the repository browser.