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

Revision 2549, 7.4 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1009 - Corrigindo problemas com a inclusão de javascript.

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