Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
content_type_structure_tree_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/content_type/content_type_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/var_serialise/var_serialise.inc';
20 
33 {
34 
35 
40  function __construct()
41  {
42  parent::__construct();
43 
44  }//end constructor
45 
46 
56  function paintBackend(&$asset, $prefix)
57  {
58  if ($asset->writeAccess('attributes')) {
59  hidden_field($prefix.'_structure_value', 'bla');
60  // loop through the rows to be printed, escape the 'content' for each
61  $pre_serialised = $asset->attr('structure');
62  foreach ($asset->attr('structure') as $index => $row) {
63  $pre_serialised[$index]['content'] = str_replace("'", "\'", $row['content']);
64  }
65  $serialised = var_serialise($pre_serialised);
66  $serialised = 'var init_data = \''.$serialised;
67  $serialised = str_replace("\n", "\\n'; init_data += '", $serialised);
68  $serialised = substr($serialised, 0, strrpos($serialised, ';') + 1)."\n\n";
69  $format_options = $GLOBALS['SQ_SYSTEM']->getUserPrefs($asset->type(), 'SQ_STRUCTURE_TREE_FORMATTING');
70  ?>
71  <style type="text/css">
72 
73  input.structure {
74  width:150px;
75  border: 1px solid grey;
76  font-size: 8pt;
77  padding-left: 3px;
78  }
79 
80  </style>
81 
82  <script type="text/javascript" src="<?php echo sq_web_path('fudge'); ?>/var_serialise/var_serialise.js"></script>
83  <script type="text/javascript" src="<?php echo sq_web_path('lib'); ?>/js/general.js"></script>
84  <script type="text/javascript">
85 
86  var data_path = "<?php echo sq_web_path('data').'/asset_types/'.$asset->type(); ?>/files/";
87 
88  //Move these out to an external file
89  function row()
90  {
91  this.indent = 0;
92  this.content = '';
93  this.spacer = '0';
94  this.bold = '0';
95  this.italic = '0';
96  this.underline = '0';
97  }
98 
99  function structure()
100  {
101  this.rows = Array();
102  this.elt = '<?php echo $prefix; ?>_cont';
103 
104  this.getValue = function()
105  {
106  return var_serialise(this.rows);
107  }
108 
109  this.init = function()
110  {
111  //Prints out var init_data
112  <?php echo $serialised; ?>
113  var un_serialised = var_unserialise(init_data);
114 
115  for (var i = 0; i < un_serialised.length; i++) {
116  var a = new row();
117  a.indent = un_serialised[i]['indent'];
118  a.content = un_serialised[i]['content'];
119  a.bold = un_serialised[i]['bold'];
120  a.italic = un_serialised[i]['italic'];
121  a.underline = un_serialised[i]['underline'];
122  this.rows.push(a);
123  }
124  this.update();
125  }
126 
127  this.draw = function()
128  {
129  var elt = document.getElementById(this.elt);
130  var src = '';
131  src += '<table style="width: 600px;" cellspacing="1" cellpadding="1">';
132 
133  for (var i in this.rows) {
134  var indent = this.rows[i].indent * 20;
135  src += '<tr>';
136  src += '<td style="border-right: 1px dotted #999999; border-bottom: 1px dotted #999999; width: 50px;">';
137  src += '<TABLE cellSpacing=0 cellPadding=0 border=0>';
138  src += '<TBODY>';
139  src += '<TR>';
140  src += '<TD rowSpan=2><IMG alt=lef src="'+data_path+'arrow_left_blue.png" border=0 onclick="<?php echo $prefix; ?>_structure.moveLeft(' + i + ');" title="Decrease Row Indent" /></TD>';
141  src += '<TD ><IMG alt=up src="'+data_path+'arrow_up_blue.png" border=0 onclick="<?php echo $prefix; ?>_structure.moveUp(' + i + ');" title="Move Row Up Tree" /></TD>';
142  src += '<TD rowSpan=2><IMG alt=right src="'+data_path+'arrow_right_blue.png" border=0 onclick="<?php echo $prefix; ?>_structure.moveRight(' + i + ');" title="Increase Row Indent" /></TD></TR>';
143  src += '<TR>';
144  src += '<TD><IMG style="margin-top: 2px;" alt=down src="'+data_path+'arrow_down_blue.png" border=0 onclick="<?php echo $prefix; ?>_structure.moveDown(' + i + ');" title="Move Row Down Tree" ></TD></TR></TBODY></TABLE>';
145  src += '</td><td style="border-bottom: 1px dotted #999999;">';
146  var inline_style = 'style="';
147  if (this.rows[i].italic == '1') inline_style += 'font-style: italic;';
148  if (this.rows[i].bold == '1') inline_style += 'font-weight: bold;';
149  if (this.rows[i].underline == '1') inline_style += 'text-decoration: underline;';
150  inline_style += '"';
151  src += '<span style="margin-left: ' + indent + 'px; ">';
152  src += '<input class="structure" type="text" value="' + htmlspecialchars(this.rows[i].content) + '" ';
153  src += inline_style;
154  src += ' onBlur="<?php echo $prefix; ?>_structure.updateText(' + i + ', this);" />';
155  src += '</span></td><td style="margin-right: 0px; width: 55px; border-left: 1px dotted #999999;border-bottom: 1px dotted #999999;">';
156  var style_base = ' style="border: 1px solid ';
157 
158  <?php
159  if ($format_options['bold']) {
160  ?>
161  var style_colour = 'white';
162  src += '<img src="'+data_path+'text_bold.png" onclick="<?php echo $prefix; ?>_structure.makeBold(' + i + ');" title="Bold Text" ';
163  if (this.rows[i].bold == '1') style_colour = 'red';
164  src += style_base + style_colour + ';" />';
165  <?php
166  }
167  if ($format_options['underline']) {
168  ?>
169  style_colour = 'white';
170  src += '<img src="'+data_path+'text_underlined.png" onclick="<?php echo $prefix; ?>_structure.underline(' + i + ');" title="Underline Row Text" ';
171  if (this.rows[i].underline == '1') style_colour = 'red';
172  src += style_base + style_colour + ';" />';
173  <?php
174  }
175  if ($format_options['italic']) {
176  ?>
177  style_colour = 'white';
178  src += '<img src="'+data_path+'text_italics.png" onclick="<?php echo $prefix; ?>_structure.italicize(' + i + ');" title="Italicise Row Text" ';
179  if (this.rows[i].italic == '1') style_colour = 'red';
180  src += style_base + style_colour + ';" />';
181  <?php
182  }
183  ?>
184  src += '</td>';
185  <?php
186  if ($format_options['delete']) {
187  ?>
188  src += '<td style="border-left: 1px dotted #999999;border-bottom: 1px dotted #999999; margin-right: 0px; width: 18px;"><img src="<?php echo sq_web_path('data'); ?>/asset_types/bodycopy/images/icons/delete.png" onclick="<?php echo $prefix; ?>_structure.remove(' + i + ');" title="Delete This Row"/></td>';
189  src += '</tr>';
190  <?php
191  }
192  ?>
193  }
194  <?php
195  if ($format_options['insert']) {
196  ?>
197  src += '<tr><td colspan="3" style=""><img src="'+data_path+'add.png" onclick="<?php echo $prefix; ?>_structure.addRow();" title="Add New Row" />&nbsp; Add New Row: <input id="<?php echo $prefix; ?>_new_row_text" style="margin-left: 5px;" class="structure" type="text">';
198  //src += '<img src="'+data_path+'add.png" onclick="<?php echo $prefix; ?>_structure.addSpacer();"/>&nbsp; Add New Spacer: <input id="<?php echo $prefix; ?>_new_spacer_seperator" style="margin-left: 5px;" class="structure" type="text">';
199  src += '</td></tr>';
200  <?php
201  }
202  ?>
203  src += '</table>';
204  elt.innerHTML = src;
205  }
206 
207  this.moveLeft = function(index)
208  {
209  if (this.rows[index].indent > 0) {
210  this.rows[index].indent--;
211  }
212  this.update();
213  }
214 
215  this.moveRight = function(index)
216  {
217  if (index != 0) {
218  if (this.rows[index].indent <= this.rows[index - 1].indent) {
219  this.rows[index].indent++;
220  this.update();
221  }
222  }
223  }
224 
225  this.moveUp = function(index)
226  {
227  if (index != 0) {
228  var dummy = this.rows[index - 1];
229  this.rows[index - 1] = this.rows[index];
230  this.rows[index] = dummy;
231  }
232  this.update();
233  }
234 
235  this.moveDown = function(index)
236  {
237  if (index != this.rows.length - 1) {
238  var dummy = this.rows[index + 1];
239  this.rows[index + 1] = this.rows[index];
240  this.rows[index] = dummy;
241  }
242  this.update();
243  }
244 
245  this.remove = function(index)
246  {
247  this.rows.splice(index, 1);
248  this.update();
249  }
250 
251  this.makeBold = function(index)
252  {
253  if (this.rows[index].bold == '1') {
254  this.rows[index].bold = '0';
255  } else {
256  this.rows[index].bold = '1';
257  }
258  this.update();
259  }
260 
261  this.underline = function(index)
262  {
263  if (this.rows[index].underline == '1') {
264  this.rows[index].underline = '0';
265  } else {
266  this.rows[index].underline = '1';
267  }
268  this.update();
269  }
270 
271  this.italicize = function(index)
272  {
273  if (this.rows[index].italic == '1') {
274  this.rows[index].italic = '0';
275  } else {
276  this.rows[index].italic = '1';
277  }
278  this.update();
279  }
280 
281  this.updateText = function(index, elt)
282  {
283  this.rows[index].content = elt.value;
284  this.update();
285  }
286 
287  this.addRow = function()
288  {
289  var new_row = new row();
290  var new_doc_text = document.getElementById('<?php echo $prefix; ?>_new_row_text');
291  if (new_doc_text.value == '') {
292  new_row.content = 'New Row';
293  } else {
294  new_row.content = new_doc_text.value;
295  }
296  new_doc_text.value = '';
297  var index = this.rows.length - 1;
298  var indent = 0;
299  if (index > 0) {
300  indent = this.rows[index].indent;
301  }
302  new_row.indent = indent;
303  this.rows.push(new_row);
304  this.update();
305  }
306 
307  this.addSpacer = function()
308  {
309  var new_row = new row();
310  //var new_doc_text = document.getElementById('<?php echo $prefix; ?>_new_row_text');
311  new_row.content = '.<br />.<br />.<br />';
312  new_row.spacer = true;
313  var index = this.rows.length - 1;
314  var indent = 0;
315  if (index > 0) {
316  indent = this.rows[index].indent;
317  }
318  new_row.indent = indent;
319  this.rows.push(new_row);
320  this.update();
321  }
322 
323  this.update = function()
324  {
325  //Get the tree into order.
326  for (var i = 0; i < this.rows.length; i++) {
327  //First row needs to be at the base (leftmost) of the tree
328  if (i == 0 && this.rows[i].indent != 0) {
329  this.rows[i].indent = 0;
330  }
331  if (i > 0 && this.rows[i].indent > this.rows[i - 1].indent + 1) {
332  this.rows[i].indent = this.rows[i - 1].indent + 1;
333  }
334  }
335  document.getElementById('<?php echo $prefix; ?>_structure_value').value = this.getValue();
336  this.draw();
337  }
338 
339  }
340 
341  var <?php echo $prefix; ?>_structure = new structure();
342 
343  </script>
344 
345  <div id="<?php echo $prefix; ?>_cont" style="width: 100%;"><br /></div>
346  <script type="text/javascript">
347  <?php echo $prefix; ?>_structure.init();
348  </script>
349  <?php
350  if ($format_options['connections']) {
351  check_box($prefix.'_show_trailers', '1', $asset->attr('trailers'));
352  label('Show Connections between non-adjacent nodes on the frontend', $prefix.'_show_trailers');
353  }
354  }//end if
355  return TRUE;
356 
357  }//end paintBackend()
358 
359 
370  function processBackend($link, &$asset, $prefix)
371  {
372  if ($asset->writeAccess('attributes')) {
373  $serialised = array_get_index($_POST, $prefix.'_structure_value', '');
374  $full_data = var_unserialise($serialised);
375  $asset->setAttrValue('structure', $full_data);
376  $trailers = isset($_POST[$prefix.'_show_trailers']) ? TRUE : FALSE;
377  $asset->setAttrValue('trailers', $trailers);
378  // Generate the HTML
379  $asset->setAttrValue('html', $asset->drawTree($trailers));
380  }
381  return TRUE;
382 
383  }//end processBackend()
384 
385 
386 }//end class
387 
388 ?>