-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathenews_generator.module
executable file
·262 lines (228 loc) · 7.19 KB
/
enews_generator.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
/**
* Implementation of hook_menu
*/
function enews_generator_menu(){
$items['newsletter/generator'] = array(
'title' => 'newsletter generator',
'page callback' => 'drupal_get_form',
'page arguments' => array('enews_generator_pickup_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_theme
*/
function enews_generator_theme() {
return array(
'enews_generator_pickup_form' => array(
'arguments' => array('form' => NULL),
),
);
}
/**
* hook_nodeapi
*/
function enews_generator_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
if($node->type == 'enews' && $op == 'view'){
$o = drupal_get_form('enews_generator_pickup_form', $node->nid);
$node->content['enews_picker'] = array(
'#value' => $o,
);
}
}
/**
* Form callback of drupal_get_form from menu
*/
function enews_generator_pickup_form(&$form_state, $possible_template = NULL){
// Get templates we provide
$templates = _enews_generator_templates();
if(!$possible_template){
$possible_template = $form_state['post']['select'] ? $form_state['post']['select'] : arg(2);
}
// step 2 - pick color
if(array_key_exists($possible_template, $templates) || is_numeric($possible_template) ){
$form['#template'] = $possible_template;
// Load variable for color setting. Each element is a ´textfield´ Form
$names = array(
'background' => t('Background color'),
'base' => t('Base color'),
'article-odd' => t('Article background 1'),
'article-even' => t('Article background 2'),
// special handle for text or link
'link' => t('Link color'),
'text' => t('Text color'),
// extra1-x just search data-type
'extra1' => t('Extra color !d', array('!d' => '1')),
);
// Add Farbtastic color picker
drupal_add_css('misc/farbtastic/farbtastic.css', 'module', 'all', FALSE);
drupal_add_js('misc/farbtastic/farbtastic.js');
// Add custom CSS/JS
drupal_add_css(drupal_get_path('module', 'enews_generator').'/enews_generator.css');
drupal_add_js(drupal_get_path('module', 'enews_generator').'/enews_generator.js');
// Add scheme
$schemes = _enews_generator_schemes($names);
$schemes[''] = t('Custom');
$form['scheme'] = array(
'#type' => 'select',
'#title' => t('Color set'),
'#options' => $schemes,
'#default_value' => '',
);
// Add palette fields
$form['palette']['#tree'] = true;
foreach ($names as $name => $translation) {
$form['palette'][$name] = array(
'#type' => 'textfield',
'#title' => $translation,
'#default_value' => $value,
'#attributes' => array('class' => 'enews-palette'),
'#size' => 8,
);
}
}
// step 1 - select template
else{
$form['select'] = array(
'#type' => 'select',
//'#value' => t('template'),
'#title' => t('Template'),
'#options' => $templates,
'#default_value' => '',
);
$form['choose'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#default_value' => '',
);
$form['element'] = array(
'#type' => 'value',
'#value' => isset($form_state['values']['select']) ? $form_state['values']['select'] : FALSE,
);
}
return $form;
}
/**
* callback after submit a form
*/
function enews_generator_pickup_form_submit($form, &$form_state){
// Set rebuild to TRUE or form will not appear ~jinmou
$form_state['rebuild'] = TRUE;
}
/**
* theme callback of form
*/
function theme_enews_generator_pickup_form($form) {
$template = $form['#template'] ? $form['#template'] : arg(2);
if ($template) {
$output = '';
// Wrapper
$output .= '<div id="enews-wrapper" class="clear-block">';
// button for newsletter
$output .= '<div align="center"><input type="button" id="enews-result" value="» 產生電子報" /></div>';
// Color schemes
$output .= _enews_generator_colorscheme_load($template, $form['scheme']);
//$output .= drupal_render($form['scheme']);
// Palette
$output .= '<div id="enews-palette">';
foreach (element_children($form['palette']) as $name) {
$output .= drupal_render($form['palette'][$name]);
}
$output .= '</div>';
// Picker color box
$output .= '<div id="enews-picker"></div>';
// Close wrapper
$output .= '</div>';
// Preview
$output .= '<h2>'. t('Template Preview') .'</h2>';
$output .= '<div id="enews-preview">';
$output .= _enews_generator_template_load($template);
$output .= '</div>';
}
return $output;
}
/**
* Template helper function
*
* scan directory to find out tpl.php
*/
function _enews_generator_templates(){
$path = drupal_get_path('module', 'enews_generator').'/templates';
$files = file_scan_directory($path, '.tpl.php', array('.', '..', 'CVS'), NULL, TRUE, 'name');
foreach($files as $tpl => $file){
$idx = rtrim($tpl, '.tpl');
$info = drupal_parse_info_file($file->filename);
$schemes[$idx] = $info['name'];
}
return $schemes;
}
function _enews_generator_schemes($names){
$schemes = array(
'#53A3AA,#F07404,#FFFFFF,#FFFFFF,#A81D06,#153204,#53A3AA' => 'Type1',
'#C6344B,#B5A5A8,#EEE7E0,#EEE7E0,#777777,#C6344B,#A46E7C' => 'Type2',
'#F0FDC1,#86A02D,#E5F4AD,#E5F4AD,#FF9100,#777777,#A9C457' => 'Type3',
);
return $schemes;
}
/**
* Template load helper function
*
* embed css into html to support modern mailbox
*/
function _enews_generator_template_load($nid){
if(is_numeric($nid)){
if(arg(2) == 'revisions' && is_numeric(arg(3))){
$vid = arg(3);
}
else{
$vid = NULL;
}
$node = node_load($nid, $vid);
$css = $node->field_enews_css[0]['value'];
$html = $node->field_enews_html[0]['value'];
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
}
else{
$name = $nid;
$path = drupal_get_path('module', 'enews_generator');
$html = file_get_contents($path.'/templates/'.$name.'.inc');
$html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8');
$css = file_get_contents($path.'/templates/'.$name.'.css');
}
module_load_include('inc', 'enews_generator', 'css_to_inline_styles');
$inline = new CSSToInlineStyles($html, $css); //class?
$inline->setEncoding('UTF-8');
$html = $inline->convert();
$html = mb_convert_encoding($html, 'UTF-8', 'HTML-ENTITIES');
$html = preg_replace('/<\!DOC[^>]+>/', '', $html);
$html = preg_replace('/<\/?html>/', '', $html);
$html = preg_replace('/<\/?body>/', '', $html);
return $html;
}
/**
* Color scheme loading function
*
* load color scheme either from user input or default values
*/
function _enews_generator_colorscheme_load($nid, $color){
if(is_numeric($nid)){
$node = node_load($nid);
$colorscheme = $node->field_enews_colorscheme[0]['value'];
$colorarray = explode("\n",$colorscheme);
$num_color = count($colorarray);
for ($i=0 ; $i<$num_color ; $i++ ){
$tmp = explode("=>", $colorarray[$i]);
$fcolor[$tmp[0]] = $tmp[1];
}
$fcolor[''] = t('Custom');
$color['#options'] = $fcolor;
$html = drupal_render($color);
}
else{ // default color scheme
$html = drupal_render($color);
}
return $html;
}