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