Updated years from 2004 to 2005 + all function indexes. NO actual code changes done!
[Packages/TYPO3.CMS.git] / typo3 / wizard_colorpicker.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Colorpicker wizard
29 *
30 * $Id $
31 * Revised for TYPO3 3.7 May/2004 by Kasper Skaarhoj
32 *
33 * @author Mathias Schreiber <schreiber@wmdb.de>
34 * @author Peter Kühn <peter@kuehn.com>
35 * @author Kasper Skaarhoj <typo3@typo3.com>
36 */
37 /**
38 * [CLASS/FUNCTION INDEX of SCRIPT]
39 *
40 *
41 *
42 * 75: class SC_wizard_colorpicker
43 * 103: function init()
44 * 182: function main()
45 * 234: function printContent()
46 * 245: function frameSet()
47 *
48 * SECTION: Rendering of various color selectors
49 * 304: function colorMatrix()
50 * 353: function colorList()
51 * 383: function colorImage()
52 * 416: function getIndex($im,$x,$y)
53 *
54 * TOTAL FUNCTIONS: 8
55 * (This index is automatically created/updated by the extension "extdeveval")
56 *
57 */
58
59
60 $BACK_PATH = '';
61 require('init.php');
62 require('template.php');
63 require_once(PATH_t3lib.'class.t3lib_stdgraphic.php');
64 $LANG->includeLLFile('EXT:lang/locallang_wizards.xml');
65
66 /**
67 * Script Class for colorpicker wizard
68 *
69 * @author Mathias Schreiber <schreiber@wmdb.de>
70 * @author Peter Kühn <peter@kuehn.com>
71 * @author Kasper Skaarhoj <typo3@typo3.com>
72 * @package TYPO3
73 * @subpackage core
74 */
75 class SC_wizard_colorpicker {
76
77 // GET vars:
78 var $P; // Wizard parameters, coming from TCEforms linking to the wizard.
79 var $colorValue; // Value of the current color picked.
80 var $fieldChangeFunc; // Serialized functions for changing the field... Necessary to call when the value is transferred to the TCEform since the form might need to do internal processing. Otherwise the value is simply not be saved.
81 var $fieldName; // Form name (from opener script)
82 var $formName; // Field name (from opener script)
83 var $md5ID; // ID of element in opener script for which to set color.
84 var $showPicker; // Internal: If false, a frameset is rendered, if true the content of the picker script.
85
86 // Static:
87 var $HTMLcolorList = "aqua,black,blue,fuchsia,gray,green,lime,maroon,navy,olive,purple,red,silver,teal,yellow,white";
88
89 // Internal:
90 var $pickerImage = '';
91 var $imageError = ''; // Error message if image not found.
92 var $doc; // Template Object
93 var $content; // Accumulated content.
94
95
96
97
98 /**
99 * Initialises the Class
100 *
101 * @return void
102 */
103 function init() {
104 global $BACK_PATH, $LANG;
105
106 // Setting GET vars (used in frameset script):
107 $this->P = t3lib_div::_GP('P',1);
108
109 // Setting GET vars (used in colorpicker script):
110 $this->colorValue = t3lib_div::_GP('colorValue');
111 $this->fieldChangeFunc = t3lib_div::_GP('fieldChangeFunc');
112 $this->fieldName = t3lib_div::_GP('fieldName');
113 $this->formName = t3lib_div::_GP('formName');
114 $this->md5ID = t3lib_div::_GP('md5ID');
115 $this->exampleImg = t3lib_div::_GP('exampleImg');
116
117
118 // Resolving image (checking existence etc.)
119 $this->imageError = '';
120 if ($this->exampleImg) {
121 $this->pickerImage = t3lib_div::getFileAbsFileName($this->exampleImg,1,1);
122 if (!$this->pickerImage || !@is_file($this->pickerImage)) {
123 $this->imageError = 'ERROR: The image, "'.$this->exampleImg.'", could not be found!';
124 }
125 }
126
127 // Setting field-change functions:
128 $fieldChangeFuncArr = unserialize($this->fieldChangeFunc);
129 $update = '';
130 if (is_array($fieldChangeFuncArr)) {
131 unset($fieldChangeFuncArr['alert']);
132 foreach($fieldChangeFuncArr as $v) {
133 $update.= '
134 parent.opener.'.$v;
135 }
136 }
137
138 // Initialize document object:
139 $this->doc = t3lib_div::makeInstance('smallDoc');
140 $this->doc->backPath = $BACK_PATH;
141 $this->doc->docType = 'xhtml_trans';
142 $this->doc->JScode = $this->doc->wrapScriptTags('
143 function checkReference() { //
144 if (parent.opener && parent.opener.document && parent.opener.document.'.$this->formName.' && parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"]) {
145 return parent.opener.document.'.$this->formName.'["'.$this->fieldName.'"];
146 } else {
147 close();
148 }
149 }
150 function changeBGcolor(color) { // Changes the color in the table sample back in the TCEform.
151 if (parent.opener.document.layers) {
152 parent.opener.document.layers["'.$this->md5ID.'"].bgColor = color;
153 } else if (parent.opener.document.all) {
154 parent.opener.document.all["'.$this->md5ID.'"].style.background = color;
155 } else if (parent.opener.document.getElementById && parent.opener.document.getElementById("'.$this->md5ID.'")) {
156 parent.opener.document.getElementById("'.$this->md5ID.'").bgColor = color;
157 }
158 }
159 function setValue(input) { //
160 var field = checkReference();
161 if (field) {
162 field.value = input;
163 '.$update.'
164 changeBGcolor(input);
165 }
166 }
167 function getValue() { //
168 var field = checkReference();
169 return field.value;
170 }
171 ');
172
173 // Start page:
174 $this->content.=$this->doc->startPage($LANG->getLL('colorpicker_title'));
175 }
176
177 /**
178 * Main Method, rendering either colorpicker or frameset depending on ->showPicker
179 *
180 * @return void
181 */
182 function main() {
183 global $LANG;
184
185 if(!t3lib_div::_GP('showPicker')) { // Show frameset by default:
186 $this->frameSet();
187 } else {
188
189 // Putting together the items into a form:
190 $content = '
191 <form name="colorform" method="post" action="wizard_colorpicker.php">
192 '.$this->colorMatrix().'
193 '.$this->colorList().'
194 '.$this->colorImage().'
195
196 <!-- Value box: -->
197 <p class="c-head">'.$LANG->getLL('colorpicker_colorValue',1).'</p>
198 <table border="0" cellpadding="0" cellspacing="3">
199 <tr>
200 <td><input type="text" '.$this->doc->formWidth(7).' maxlength="10" name="colorValue" value="'.htmlspecialchars($this->colorValue).'" /></td>
201 <td style="background-color:'.htmlspecialchars($this->colorValue).'; border: 1px solid black;">&nbsp;<span style="color: black;">'.$LANG->getLL('colorpicker_black',1).'</span>&nbsp;<span style="color: white;">'.$LANG->getLL('colorpicker_white',1).'</span>&nbsp;</td>
202 <td><input type="submit" name="save_close" value="'.$LANG->getLL('colorpicker_setClose',1).'" /></td>
203 </tr>
204 </table>
205
206 <!-- Hidden fields with values that has to be kept constant -->
207 <input type="hidden" name="showPicker" value="1" />
208 <input type="hidden" name="fieldChangeFunc" value="'.htmlspecialchars($this->fieldChangeFunc).'" />
209 <input type="hidden" name="fieldName" value="'.htmlspecialchars($this->fieldName).'" />
210 <input type="hidden" name="formName" value="'.htmlspecialchars($this->formName).'" />
211 <input type="hidden" name="md5ID" value="'.htmlspecialchars($this->md5ID).'" />
212 <input type="hidden" name="exampleImg" value="'.htmlspecialchars($this->exampleImg).'" />
213 </form>';
214
215 // If the save/close button is clicked, then close:
216 if(t3lib_div::_GP('save_close')) {
217 $content.=$this->doc->wrapScriptTags('
218 setValue(\''.$this->colorValue.'\');
219 parent.close();
220 ');
221 }
222
223 // Output:
224 $this->content.=$this->doc->section($LANG->getLL('colorpicker_title'), $content, 0,1);
225 $this->content.=$this->doc->endPage();
226 }
227 }
228
229 /**
230 * Returnes the sourcecode to the browser
231 *
232 * @return void
233 */
234 function printContent() {
235 echo $this->content;
236 }
237
238 /**
239 * Returnes a frameset so our JavaScript Reference isn't lost
240 * Took some brains to figure this one out ;-)
241 * If Peter wouldn't have been I would've gone insane...
242 *
243 * @return void
244 */
245 function frameSet() {
246 global $LANG;
247
248 // Set doktype:
249 $GLOBALS['TBE_TEMPLATE']->docType = 'xhtml_frames';
250 $GLOBALS['TBE_TEMPLATE']->JScode = $GLOBALS['TBE_TEMPLATE']->wrapScriptTags('
251 if (!window.opener) {
252 alert("ERROR: Sorry, no link to main window... Closing");
253 close();
254 }
255 ');
256
257 $this->content = $GLOBALS['TBE_TEMPLATE']->startPage($LANG->getLL('colorpicker_title'));
258
259 // URL for the inner main frame:
260 $url = 'wizard_colorpicker.php?showPicker=1'.
261 '&colorValue='.rawurlencode($this->P['currentValue']).
262 '&fieldName='.rawurlencode($this->P['itemName']).
263 '&formName='.rawurlencode($this->P['formName']).
264 '&exampleImg='.rawurlencode($this->P['exampleImg']).
265 '&md5ID='.rawurlencode($this->P['md5ID']).
266 '&fieldChangeFunc='.rawurlencode(serialize($this->P['fieldChangeFunc']));
267
268 $this->content.='
269 <frameset rows="*,1" framespacing="0" frameborder="0" border="0">
270 <frame name="content" src="'.htmlspecialchars($url).'" marginwidth="0" marginheight="0" frameborder="0" scrolling="auto" noresize="noresize" />
271 <frame name="menu" src="dummy.php" marginwidth="0" marginheight="0" frameborder="0" scrolling="no" noresize="noresize" />
272 </frameset>
273 ';
274
275 $this->content.='
276 </html>';
277 }
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 /************************************
294 *
295 * Rendering of various color selectors
296 *
297 ************************************/
298
299 /**
300 * Creates a color matrix table
301 *
302 * @return void
303 */
304 function colorMatrix() {
305 global $LANG;
306
307 $steps = 51;
308
309 // Get colors:
310 $color = array();
311
312 for($rr=0;$rr<256;$rr+=$steps) {
313 for($gg=0;$gg<256;$gg+=$steps) {
314 for($bb=0;$bb<256;$bb+=$steps) {
315 $color[] = '#'.
316 substr('0'.dechex($rr),-2).
317 substr('0'.dechex($gg),-2).
318 substr('0'.dechex($bb),-2);
319 }
320 }
321 }
322
323 // Traverse colors:
324 $columns = 24;
325
326 $rows = 0;
327 $tRows = array();
328 while(isset($color[$columns*$rows])) {
329 $tCells = array();
330 for($i=0;$i<$columns;$i++) {
331 $tCells[] = '
332 <td bgcolor="'.$color[$columns*$rows+$i].'" onclick="document.colorform.colorValue.value = \''.$color[$columns*$rows+$i].'\'; document.colorform.submit();" title="'.$color[$columns*$rows+$i].'">&nbsp;&nbsp;</td>';
333 }
334 $tRows[] = '
335 <tr>'.implode('',$tCells).'
336 </tr>';
337 $rows++;
338 }
339
340 $table = '
341 <p class="c-head">'.$LANG->getLL('colorpicker_fromMatrix',1).'</p>
342 <table border="0" cellpadding="1" cellspacing="1" style="width:100%; border: 1px solid black; cursor:crosshair;">'.implode('',$tRows).'
343 </table>';
344
345 return $table;
346 }
347
348 /**
349 * Creates a selector box with all HTML color names.
350 *
351 * @return void
352 */
353 function colorList() {
354 global $LANG;
355
356 // Initialize variables:
357 $colors = explode(',',$this->HTMLcolorList);
358 $currentValue = strtolower($this->colorValue);
359 $opt = array();
360 $opt[] = '<option value=""></option>';
361
362 // Traverse colors, making option tags for selector box.
363 foreach($colors as $colorName) {
364 $opt[] = '<option style="background-color: '.$colorName.';" value="'.htmlspecialchars($colorName).'"'.($currentValue==$colorName ? ' selected="selected"' : '').'>'.htmlspecialchars($colorName).'</option>';
365 }
366
367 // Compile selector box and return result:
368 $output = '
369 <p class="c-head">'.$LANG->getLL('colorpicker_fromList',1).'</p>
370 <select onchange="document.colorform.colorValue.value = this.options[this.selectedIndex].value; document.colorform.submit(); return false;">
371 '.implode('
372 ',$opt).'
373 </select><br/>';
374
375 return $output;
376 }
377
378 /**
379 * Creates a color image selector
380 *
381 * @return void
382 */
383 function colorImage() {
384 global $LANG;
385
386 // Handling color-picker image if any:
387 if (!$this->imageError) {
388 if ($this->pickerImage) {
389 if(t3lib_div::_POST('coords_x')) {
390 $this->colorValue = '#'.$this->getIndex(t3lib_stdgraphic::imageCreateFromFile($this->pickerImage),t3lib_div::_POST('coords_x'),t3lib_div::_POST('coords_y'));
391 }
392 $pickerFormImage = '
393 <p class="c-head">'.$LANG->getLL('colorpicker_fromImage',1).'</p>
394 <input type="image" src="../'.substr($this->pickerImage,strlen(PATH_site)).'" name="coords" style="cursor:crosshair;" /><br />';
395 } else {
396 $pickerFormImage = '';
397 }
398 } else {
399 $pickerFormImage = '
400 <p class="c-head">'.htmlspecialchars($this->imageError).'</p>';
401 }
402
403 return $pickerFormImage;
404 }
405
406 /**
407 * Gets the HTML (Hex) Color Code for the selected pixel of an image
408 * This method handles the correct imageResource no matter what format
409 *
410 * @param pointer Valid ImageResource returned by t3lib_stdgraphic::imageCreateFromFile
411 * @param integer X-Coordinate of the pixel that should be checked
412 * @param integer Y-Coordinate of the pixel that should be checked
413 * @return string HEX RGB value for color
414 * @see colorImage()
415 */
416 function getIndex($im,$x,$y) {
417 $rgb = ImageColorAt($im, $x, $y);
418 $colorrgb = imagecolorsforindex($im,$rgb);
419 $index['r'] = dechex($colorrgb['red']);
420 $index['g'] = dechex($colorrgb['green']);
421 $index['b'] = dechex($colorrgb['blue']);
422 foreach ($index as $value) {
423 if(strlen($value) == 1) {
424 $hexvalue[] = strtoupper('0'.$value);
425 } else {
426 $hexvalue[] = strtoupper($value);
427 }
428 }
429 $hex = implode('',$hexvalue);
430 return $hex;
431 }
432 }
433
434 // Include extension?
435 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php']) {
436 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/wizard_colorpicker.php']);
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450 // Make instance:
451 $SOBE = t3lib_div::makeInstance('SC_wizard_colorpicker');
452 $SOBE->init();
453 $SOBE->main();
454 $SOBE->printContent();
455 ?>