1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 12 13 14 15 16 17
18 class FOFFormFieldRelation extends FOFFormFieldList
19 {
20 21 22 23 24 25 26 27
28 public function getStatic() {
29 return $this->getRepeatable();
30 }
31
32 33 34 35 36 37 38 39
40 public function getRepeatable()
41 {
42 $class = $this->element['class'] ? (string) $this->element['class'] : $this->id;
43 $relationclass = $this->element['relationclass'] ? (string) $this->element['relationclass'] : '';
44 $value_field = $this->element['value_field'] ? (string) $this->element['value_field'] : 'title';
45 $translate = $this->element['translate'] ? (string) $this->element['translate'] : false;
46 $link_url = $this->element['url'] ? (string) $this->element['url'] : false;
47
48 if (!($link_url && $this->item instanceof FOFTable))
49 {
50 $link_url = false;
51 }
52
53 if ($this->element['empty_replacement'])
54 {
55 $empty_replacement = (string) $this->element['empty_replacement'];
56 }
57
58 $relationName = FOFInflector::pluralize($this->name);
59 $relations = $this->item->getRelations()->getMultiple($relationName);
60
61 foreach ($relations as $relation) {
62
63 $html = '<span class="' . $relationclass . '">';
64
65 if ($link_url)
66 {
67 $keyfield = $relation->getKeyName();
68 $this->_relationId = $relation->$keyfield;
69
70 $url = $this->parseFieldTags($link_url);
71 $html .= '<a href="' . $url . '">';
72 }
73
74 $value = $relation->get($relation->getColumnAlias($value_field));
75
76
77 if (!empty($empty_replacement) && empty($value))
78 {
79 $value = JText::_($empty_replacement);
80 }
81
82 if ($translate == true)
83 {
84 $html .= JText::_($value);
85 }
86 else
87 {
88 $html .= $value;
89 }
90
91 if ($link_url)
92 {
93 $html .= '</a>';
94 }
95
96 $html .= '</span>';
97
98 $rels[] = $html;
99 }
100
101 $html = '<span class="' . $class . '">';
102 $html .= implode(', ', $rels);
103 $html .= '</span>';
104
105 return $html;
106 }
107
108 109 110 111 112
113 protected function getOptions()
114 {
115 $options = array();
116 $this->value = array();
117
118 $value_field = $this->element['value_field'] ? (string) $this->element['value_field'] : 'title';
119
120 $input = new FOFInput;
121 $component = ucfirst(str_replace('com_', '', $input->getString('option')));
122 $view = ucfirst($input->getString('view'));
123 $relation = FOFInflector::pluralize((string) $this->element['name']);
124
125 $model = FOFModel::getTmpInstance(ucfirst($relation), $component . 'Model');
126 $table = $model->getTable();
127
128 $key = $table->getKeyName();
129 $value = $table->getColumnAlias($value_field);
130
131 foreach ($model->getItemList(true) as $option)
132 {
133 $options[] = JHtml::_('select.option', $option->$key, $option->$value);
134 }
135
136 if ($id = FOFModel::getAnInstance($view)->getId())
137 {
138 $table = FOFTable::getInstance($view, $component . 'Table');
139 $table->load($id);
140
141 $relations = $table->getRelations()->getMultiple($relation);
142
143 foreach ($relations as $item)
144 {
145 $this->value[] = $item->getId();
146 }
147 }
148
149 return $options;
150 }
151
152 153 154 155 156 157 158
159 protected function parseFieldTags($text)
160 {
161 $ret = $text;
162
163
164
165 $keyfield = $this->item->getKeyName();
166 $replace = $this->item->$keyfield;
167 $ret = str_replace('[ITEM:ID]', $replace, $ret);
168
169
170 $ret = str_replace('[ITEMID]', JFactory::getApplication()->input->getInt('Itemid', 0), $ret);
171
172
173 $ret = str_replace('[RELATION:ID]', $this->_relationId, $ret);
174
175
176 $fields = $this->item->getTableFields();
177
178 foreach ($fields as $fielddata)
179 {
180 $fieldname = $fielddata->Field;
181
182 if (empty($fieldname))
183 {
184 $fieldname = $fielddata->column_name;
185 }
186
187 $search = '[ITEM:' . strtoupper($fieldname) . ']';
188 $replace = $this->item->$fieldname;
189 $ret = str_replace($search, $replace, $ret);
190 }
191
192 return $ret;
193 }
194 }
195