CMS  Version 3.9
connectable_field_renderers.inc
Go to the documentation of this file.
1 <?php
7 Fakoli::usingFeature("field_renderers");
8 
9 class ConnectableTableFieldRenderer extends FieldRenderer
10 {
11  var $table;
12 
13  var $field;
14  var $label;
15 
17  var $target;
18 
19  var $colspan = 2;
20  var $related;
21 
23  {
24  $this->FieldRenderer($form);
25 
26  $this->field = $field;
27  $this->label = $label;
28  $this->targetClass = $targetClass;
29 
30  $this->target = new $targetClass;
31 
32  $this->parent->add($this);
33 
34  $items = ConnectableManager::getConnectedItems($this->parent->data, $this->targetClass);
35  $this->related = reindexList($items, $this->target->getPrimaryKey());
36  $this->buildTable();
37  }
38 
39  function buildTable()
40  {
41  $this->table = new DataListView(null, "connectables");
42  $this->table->rowId = true;
43 
44  $this->table->pageSize = 5;
45  $this->table->filter = true;
46  $this->table->emptyMessage = "There are no ".$this->target->prettifyClassName(true)." to select from";
47 
48  $this->table->selectable = true;
49  $this->table->selector(array($this, 'formatCheckbox'));
50 
51  $this->target->populateSelectionTable($this->table);
52 
53  }
54 
55  function formatCheckbox($item)
56  {
57  $pk = $item->getPrimaryKey();
58  $pkval = $item->get($pk);
59 
60  $checked = array_key_exists($pkval, $this->related) ? " checked='checked'" : "";
61 
62  return $item->format("<input type='checkbox' class='checkbox' name='{$pk}[]' id='{$pk}_{{$pk}}' value='{{$pk}}'{$checked}/>");
63  }
64 
65  function renderScript()
66  {
67  echo $this->table->writeScript();
68  }
69 
70  function renderField($field)
71  {
72  echo $this->table->drawView();
73  }
74 
75  function postProcess($field = "")
76  {
77  if (!$field) $field = $this->field;
78 
79  ConnectableManager::removeConnections($this->parent->data, $this->targetClass);
80 
81  $ids = $_POST[$this->target->getPrimaryKey()];
82 
83  foreach($ids as $id)
84  {
85  $target = new $this->target;
86  $target->load($id);
87  ConnectableManager::addConnection($this->parent->data, $target);
88  }
89 
90  return true;
91  }
92 }?>
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$siteTree target
Definition: site_map.inc:56
static addConnection($source, $target)
Add a connection between the given source and target objects.
static getConnectedItems($sourceItem, $targetClass, $constraint="")
Returns the connected items for the specified source that are of the specified class.
static removeConnections($source, $targetClass)
Remove any connection that exists between the source and objects of the specified target class (inclu...
__construct($form, $field, $label, $targetClass)
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
$openData field