commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / HTML / QuickForm / ckeditor.php
1 <?php
2
3 require_once('HTML/QuickForm/textarea.php');
4
5 /**
6 * HTML Quickform element for CKeditor
7 *
8 * CKeditor is a WYSIWYG HTML editor which can be obtained from
9 * http://ckeditor.com. I tried to resemble the integration instructions
10 * as much as possible, so the examples from the docs should work with this one.
11 *
12 * @author Kurund Jalmi
13 * @access public
14 */
15 class HTML_QuickForm_CKeditor extends HTML_QuickForm_textarea
16 {
17 /**
18 * The width of the editor in pixels or percent
19 *
20 * @var string
21 * @access public
22 */
23 var $width = '94%';
24
25 /**
26 * The height of the editor in pixels or percent
27 *
28 * @var string
29 * @access public
30 */
31 var $height = '400';
32
33 /**
34 * Class constructor
35 *
36 * @param string FCKeditor instance name
37 * @param string FCKeditor instance label
38 * @param array Config settings for FCKeditor
39 * @param string Attributes for the textarea
40 * @access public
41 * @return void
42 */
43 function HTML_QuickForm_ckeditor($elementName=null, $elementLabel=null, $attributes=null, $options=array())
44 {
45 HTML_QuickForm_element::HTML_QuickForm_element($elementName, $elementLabel, $attributes);
46 $this->_persistantFreeze = true;
47 $this->_type = 'CKeditor';
48 // set editor height smaller if schema defines rows as 4 or less
49 if ( is_array($attributes) && array_key_exists( 'rows', $attributes ) && $attributes['rows'] <= 4 ) {
50 $this->height = 175;
51 }
52 }
53
54 /**
55 * Add js to to convert normal textarea to ckeditor
56 *
57 * @access public
58 * @return string
59 */
60 function toHtml()
61 {
62 if ($this->_flagFrozen) {
63 return $this->getFrozenHtml();
64 } elseif (!$this->getAttribute('click_wysiwyg')) {
65 $elementId = $this->getAttribute('id');
66 $config = CRM_Core_Config::singleton( );
67 $browseUrl = $config->userFrameworkResourceURL . 'packages/kcfinder/browse.php';
68 $uploadUrl = $config->userFrameworkResourceURL . 'packages/kcfinder/upload.php';
69
70 $html = parent::toHtml() . "<script type='text/javascript'>
71 CRM.$(function($) {
72 cj('#{$elementId}').removeClass();
73 if ( CKEDITOR.instances['{$elementId}'] ) {
74 CKEDITOR.remove(CKEDITOR.instances['{$elementId}']);
75 }
76 if ( cj('#{$elementId}').val( ) == '' ) cj('#{$elementId}').val('&nbsp;');
77 CKEDITOR.replace( '{$elementId}' );
78 var editor = CKEDITOR.instances['{$elementId}'];
79 if ( editor ) {
80 editor.config.width = '".$this->width."';
81 editor.config.height = '".$this->height."';
82 editor.config.filebrowserBrowseUrl = '".$browseUrl."?cms=civicrm&type=files';
83 editor.config.filebrowserImageBrowseUrl = '".$browseUrl."?cms=civicrm&type=images';
84 editor.config.filebrowserFlashBrowseUrl = '".$browseUrl."?cms=civicrm&type=flash';
85 editor.config.filebrowserUploadUrl = '".$uploadUrl."?cms=civicrm&type=files';
86 editor.config.filebrowserImageUploadUrl = '".$uploadUrl."?cms=civicrm&type=images';
87 editor.config.filebrowserFlashUploadUrl = '".$uploadUrl."?cms=civicrm&type=flash';
88 }
89 });
90 </script>";
91 return $html;
92 }
93 else {
94 $elementId = $this->getAttribute('id');
95 $plain = '<div id="' . $elementId .'-plain" class="replace-plain" tabindex="0" title="'. ts('Click to edit') .'">' . $this->getFrozenHtml() . '</div>' . parent::toHtml();
96 $config = CRM_Core_Config::singleton( );
97 $browseUrl = $config->userFrameworkResourceURL . 'packages/kcfinder/browse.php';
98 $uploadUrl = $config->userFrameworkResourceURL . 'packages/kcfinder/upload.php';
99
100 $html = $plain . "<script type='text/javascript'>
101 CRM.$(function($) {
102 $('#{$elementId}').hide();
103 var openWysiwyg = function() {
104 var restorePlain = $('#{$elementId}-plain').detach();
105 $('#{$elementId}').removeClass();
106 if ( CKEDITOR.instances['{$elementId}'] ) {
107 CKEDITOR.remove(CKEDITOR.instances['{$elementId}']);
108 }
109 if ( $('#{$elementId}').val( ) == '' ) $('#{$elementId}').val('&nbsp;');
110 CKEDITOR.replace( '{$elementId}' );
111 var editor = CKEDITOR.instances['{$elementId}'];
112 if ( editor ) {
113 editor.config.width = '".$this->width."';
114 editor.config.height = '".$this->height."';
115 editor.config.filebrowserBrowseUrl = '".$browseUrl."?cms=civicrm&type=files';
116 editor.config.filebrowserImageBrowseUrl = '".$browseUrl."?cms=civicrm&type=images';
117 editor.config.filebrowserFlashBrowseUrl = '".$browseUrl."?cms=civicrm&type=flash';
118 editor.config.filebrowserUploadUrl = '".$uploadUrl."?cms=civicrm&type=files';
119 editor.config.filebrowserImageUploadUrl = '".$uploadUrl."?cms=civicrm&type=images';
120 editor.config.filebrowserFlashUploadUrl = '".$uploadUrl."?cms=civicrm&type=flash';
121 editor.on( 'blur', function( e ) {
122 this.updateElement();
123 this.destroy(true);
124 $('#{$elementId}').before(restorePlain);
125 $('#{$elementId}-plain').html($('#{$elementId}').val());
126 $('#{$elementId}').hide();
127 })
128 }
129 };
130 $('#{$elementId}-plain').click(openWysiwyg);
131 $('#{$elementId}-plain').keypress(openWysiwyg);
132 });
133 </script>";
134 return $html;
135 }
136 }
137
138 /**
139 * Returns the htmlarea content in HTML
140 *
141 * @access public
142 * @return string
143 */
144 function getFrozenHtml()
145 {
146 return $this->getValue();
147 }
148 }
149
150 ?>