Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
bodycopy_table_cell.inc
1 <?php
30 {
31 
36  public $attributes = Array();
37 
38 
43  function __construct()
44  {
45  parent::__construct();
46 
47  }//end constructor
48 
49 
60  public function setAttribute($name, $value)
61  {
62  $name = strtolower($name);
63  if (!isset($this->attributes[$name])) {
64  $this->attributes[$name] = '';
65  }
66  if ($this->attributes[$name] == $value) return FALSE;
67  $this->attributes[$name] = $value;
68  return TRUE;
69 
70  }//end setAttribute()
71 
72 
84  public function setContentType(Bodycopy_Table $table, $type, $rowid, $cellid)
85  {
86  $GLOBALS['SQ_SYSTEM']->am->includeAsset($type);
87  $content_link = Array('asset' => &$table, 'link_type' => SQ_LINK_TYPE_2, 'is_dependant' => 1, 'is_exclusive' => 1);
88  $content_type = new $type();
89  $content_type->setAttrValue('name', 'Row '.($rowid+1).' Cell '.($cellid+1));
90  $this->attributes['linkid'] = $content_type->create($content_link);
91  if (!$this->attributes['linkid']) return FALSE;
92  $this->attributes['content_type'] = $type;
93  return TRUE;
94 
95  }//end setContentType()
96 
97 
112  public function updateContentType(Bodycopy_Table $table, $new_rowid, $new_cellid)
113  {
114  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($this->attributes['linkid'], $table->id);
115  if (!$content_type = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code'])) {
116  return FALSE;
117  }
118 
119  if (!$content_type->setAttrValue('name', 'Row '.($new_rowid+1).' Cell '.($new_cellid+1))) {
120  return FALSE;
121  }
122 
123  if (!$content_type->saveAttributes()) return FALSE;
124 
125  return TRUE;
126 
127  }//end updateContentType()
128 
129 
138  public function delete(Bodycopy_Table $table)
139  {
140  if (!$table->deleteLink($this->attributes['linkid'])) {
141  return FALSE;
142  }
143 
144  // TODO: this is a replacement for re-assigning $this - needs to be
145  // tested
146  $this->attributes = Array();
147  //unset($this->attributes);
148  //$this = new BodyCopy_Table_Cell();
149  return TRUE;
150 
151  }//end delete()
152 
153 
160  public function colspan()
161  {
162  if (empty($this->attributes['colspan'])) return 1;
163  return ((int) $this->attributes['colspan'] > 0) ? (int) $this->attributes['colspan'] : 1;
164 
165  }//end colspan()
166 
167 
174  public function rowspan()
175  {
176  if (empty($this->attributes['rowspan'])) return 1;
177  return ((int) $this->attributes['rowspan'] > 0) ? (int) $this->attributes['rowspan'] : 1;
178 
179  }//end rowspan()
180 
181 
193  public function paint(Bodycopy_Table $table, $prefix='', $editing=FALSE, $generating=FALSE)
194  {
195  if (!isset($this->attributes['linkid'])) return;
196 
197  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($this->attributes['linkid'], $table->id);
198  if (!empty($link)) {
199  $content_type = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
200  } else {
201  $content_type = NULL;
202  }
203  $content_prefix = $prefix.'_content_type_'.$this->attributes['content_type'].'_'.$this->attributes['linkid'];
204 
205  echo "\t\t<td";
206  foreach ($this->attributes as $name => $val) {
207  if ($val == '') continue;
208  if ($name == 'linkid') continue;
209  if ($name == 'content_type') continue;
210  // if we're in edit mode, we don't want to colspan anything at all; the colspan
211  // is handled by outer wrapping divs
212  if ($name == 'colspan' && $editing && $table->writeAccess('content')) {
213  continue;
214  }
215  if (strtolower($name) == 'nowrap') {
216  if ($val) echo ' nowrap';
217  } else {
218  echo ' '.$name.'="'.str_replace('"', '&quot;', $val).'"';
219  }
220  }
221  if ($editing && $table->writeAccess('content')) {
222  echo ' style="margin: 2px; vertical-align:top;" id="'.$content_prefix.'_cell"';
223  }
224  echo ">\n";
225 
226  if (is_null($content_type)) {
227  echo translate('content_type_not_found', $link['minorid']);
228  } else {
229  $content_edit = $content_type->getEditFns();
230  if (!empty($prefix)) {
231  $content_edit->paintBackend($content_type, $content_prefix);
232  } else {
233  ob_start();
234  $content_edit->paint($content_type, $editing, $generating);
235  $cell_content = ob_get_contents();
236  ob_end_clean();
237 
238  // format contents for pretty HTML
239  $cell_content = preg_replace("/^/", "\t\t\t", $cell_content);
240  $cell_content = preg_replace("/\n/", "\n\t\t\t", $cell_content);
241  echo $cell_content;
242  }
243  }
244  echo "\n\t\t</td>\n";
245 
246  }//end paint()
247 
248 
261  public function paintCell(Bodycopy_Table $table, $prefix, $tableid, $rowid, $cellid)
262  {
263  $attribute_list = '';
264  $colspaning = 0;
265  $dir_attribute = '';
266  foreach ($this->attributes as $name => $val) {
267  if ($val == '') continue;
268  if (strtolower($name) == 'nowrap') {
269  if ($val) $attribute_list .= ' nowrap';
270  } else {
271  if (strtolower($name) == 'colspan') {
272  $colspaning = $val;
273  }
274  $attribute_list .= ' '.$name.'="'.str_replace('"', '&quot;', $val).'"';
275  }
276  if ($name == 'dir') $dir_attribute = $val;
277  }
278 
279  // print out the edit buttons
280 
281  Bodycopy_Edit_Fns::printBodycopyIcon("bodycopy_edit_table_cell_properties('$prefix', $tableid, $rowid, $cellid);", 'Edit Cell '.($cellid + 1).' - '.($rowid+1).'\'s Properties', (($attribute_list) ? 'Current Properties :<br>'.$attribute_list : ''), 'cell_properties', '[Edit Cell Properties]');
282 
283  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($this->attributes['linkid'], $table->id);
284  if (!empty($link)) {
285  $content_type = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
286  } else {
287  $content_type = NULL;
288  }
289 
290  // set the dir attribute to one inherited from above if there is no specified for cell
291  if (empty($dir_attribute) && isset($this->_tmp['dir_attr'])) {
292  $dir_attribute = $this->_tmp['dir_attr'];
293  }
294 
295  if (!is_null($content_type)) {
296  $content_edit = $content_type->getEditFns();
297  $content_prefix = $prefix.'_content_type_'.$this->attributes['content_type'].'_'.$this->attributes['linkid'];
298  if (method_exists($content_edit,'setDirAttr') && !empty($dir_attribute)) {
299  $content_edit->setDirAttr($dir_attribute);
300  }
301  $content_edit->paintBackendIcons($content_type, $content_prefix);
302  }
303 
304  return TRUE;
305 
306  }//end paintCell()
307 
308 
321  public function paintCellBackendStatusIcons(Bodycopy_Table $table, $prefix, $tableid, $rowid, $cellid)
322  {
323  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($this->attributes['linkid'], $table->id);
324  if (!empty($link)) {
325  $content_type = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
326  } else {
327  $content_type = NULL;
328  }
329 
330  if (!is_null($content_type)) {
331  $content_edit = $content_type->getEditFns();
332  $content_prefix = $prefix.'_content_type_'.$this->attributes['content_type'].'_'.$this->attributes['linkid'];
333  $content_edit->paintBackendStatusIcons($content_type, $content_prefix);
334  }
335 
336  return TRUE;
337 
338  }//end paintCellBackendStatusIcons()
339 
340 
354  public function processCell(Bodycopy_Table $table, $prefix, $tableid, $rowid, $cellid, Array $bc_saved)
355  {
356  $updated = FALSE;
357 
358  // update the contentType
359  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($this->attributes['linkid'], $table->id);
360  if (empty($link)) return FALSE;
361  $content_type = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
362  $content_edit = $content_type->getEditFns();
363  $content_prefix = $prefix.'_content_type_'.$this->attributes['content_type'].'_'.$this->attributes['linkid'];
364  if ($content_edit->processBackend(Array(), $content_type, $content_prefix)) {
365  $updated = TRUE;
366  }
367  if (!$content_type->saveAttributes()) return FALSE;
368 
369  if (isset($bc_saved[$prefix][$tableid]['rows'][$rowid]['cells'][$cellid]['attributes'])) {
370  foreach ($bc_saved[$prefix][$tableid]['rows'][$rowid]['cells'][$cellid]['attributes'] as $name => $value) {
371  if ($name == 'type') {
372  // changing the content type of this cell
373  if ($this->_updateCellType($table, $value)) {
374  $updated = TRUE;
375  }
376  continue;
377  }
378  if ($this->setAttribute($name, $value)) {
379  $updated = TRUE;
380  }
381  }
382  }
383  return $updated;
384 
385  }//end processCell()
386 
387 
397  protected function _updateCellType(Bodycopy_Table $table, $new_type='')
398  {
399 
400  // dont change anything if the new type id blank
401  $new_type = trim($new_type);
402  if (empty($new_type)) return FALSE;
403 
404  // get the contentType
405  $link = $GLOBALS['SQ_SYSTEM']->am->getLinkById($this->attributes['linkid'], $table->id);
406  if (empty($link)) return FALSE;
407  $content_type = $GLOBALS['SQ_SYSTEM']->am->getAsset($link['minorid'], $link['minor_type_code']);
408 
409  // if the new type is the same as the current, change nothing
410  $current_type = get_class_lower($content_type);
411  if ($current_type == $new_type) return FALSE;
412 
413  // morph the content type to the new type
414  // first up
415  $morphed = $content_type->morph('content_type');
416  if (!$morphed) return FALSE;
417 
418  // then down
419  $content_type = $morphed->morph($new_type);
420  if (!$content_type) return FALSE;
421 
422  $this->setAttribute('content_type', $new_type);
423 
424  return TRUE;
425 
426  }//end _updateCellType()
427 
428 
437  public function printBackendJsData($cellid)
438  {
439  ?>
440 
441  case <?php echo $cellid?> :
442  retVal["attributes"] = '<?php echo var_serialise($this->attributes, TRUE)?>';
443  break;
444  <?php
445 
446  }//end printBackendJsData()
447 
448 
457  public function setDirAttr($dir_attribute)
458  {
459  $this->_tmp['dir_attr'] = $dir_attribute;
460 
461  }//end setDirAttr()
462 
463 
464 }//end class
465 
466 ?>