CMS  Version 3.9
attachment_field_renderer.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9  Copyright (c) 2010 Sonjara, Inc
10 
11  Permission is hereby granted, free of charge, to any person
12  obtaining a copy of this software and associated documentation
13  files (the "Software"), to deal in the Software without
14  restriction, including without limitation the rights to use,
15  copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the
17  Software is furnished to do so, subject to the following
18  conditions:
19 
20  The above copyright notice and this permission notice shall be
21  included in all copies or substantial portions of the Software.
22 
23  Except as contained in this notice, the name(s) of the above
24  copyright holders shall not be used in advertising or otherwise
25  to promote the sale, use or other dealings in this Software
26  without prior written authorization.
27 
28  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
30  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
32  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
33  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
34  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
35  OTHER DEALINGS IN THE SOFTWARE.
36 
37 *****************************************************************/
38 
39 Fakoli::usingFile("framework/field_renderers.inc");
40 
41 class AttachmentFieldRenderer extends FieldRenderer
42 {
43  var $colspan = 2;
45  var $field;
46  var $label;
47  var $cssClass = "attachment";
48  var $deleteIcon = "/fakoli/images/delete.gif";
49  var $useCamera = false;
50  var $addLabel = "Add an Attachment";
51  var $emptyMessage = "No files have been attached";
52 
54  {
55  $this->xrefClass = $xrefClass;
56  $this->field = $field;
57 
58  $this->FieldRenderer($form);
59  $this->label = $form->prettifyFieldName($field);
60  $form->add($this);
61 
62  if (isset($form->attachment_css_class)) $this->cssClass = $form->attachment_css_class;
63  if (isset($form->attachment_delete_icon)) $this->deleteIcon = $form->attachment_delete_icon;
64  }
65 
66 
67  function getMatchingPK($xref, $obj)
68  {
69  $pks = $obj->getPrimaryKeyList();
70  foreach($pks as $pk)
71  {
72  if ($xref->hasField($pk)) return $pk;
73  }
74 
75  throw new DataItemException("No matching foreign key in xref table");
76  }
77 
78  static function renderDialog($useCamera)
79  {
80  ob_start();
81 ?>
82 <div class="dialog" id="attachmentDialog" style="width: 260px">
83  <div class="dialog_header" id="attachmentDialogHeader">
84  <div style="padding: 4px;">
85  <div style="float: right">&nbsp;<a id='closeAttachmentDialog'>Close &times;</a></div>
86  <span style="font-weight: bold" id="attachmentDialogTitle">Add an Attachment</span>
87  </div>
88  </div>
89  <div class="dialog_body">
90 <?
92 ?>
93  </form>
94  </div>
95 </div>
96 <?
97  $dialog = ob_get_contents();
98  ob_end_clean();
99  return $dialog;
100  }
101 
102  static function renderDialogBody($useCamera)
103  {
104  ob_start();
105 ?>
106  <p id="attachmentDialogMessage"></p>
107  <form id="attachmentForm" method="POST" enctype="multipart/form-data" action="/action/attachment/upload" >
108  <br/>
109  <label>Attachment File</label><br/>
110  <input type="file" name="attachmentFile"<?if ($useCamera) { echo ' accept="image/*" capture="camera"'; }?>/>
111  <br/>
112  <br/><input type="submit" class="button" name="submit" value="Upload Attachment" style="float: right; margin-bottom: 4px"/>
113 <?
114  $body = ob_get_contents();
115  ob_end_clean();
116  return $body;
117  }
118 
120  {
121  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
122 
123  global $dialogs;
124  if (!$field) $field = $this->field;
125 
126  $dialogs .= AttachmentFieldRenderer::renderDialog($this->useCamera);
127 ?>
128  <script type="text/javascript">
129  window.addEvent('domready', function()
130  {
131  new AttachmentUploader().setup('attachmentForm', '<?echo $this->parent->id?>_<?echo $field?>_list', '<?echo $this->parent->id?>_<?echo $field?>', '<?echo $this->cssClass?>', '<?echo $this->deleteIcon?>');
132  });
133  </script>
134 <?
135  }
136 
137  function renderField($field = "")
138  {
139  if (!$field) $field = $this->field;
140 
141  $xref = new $this->xrefClass;
142 
143  $this->_startField($field);
144 
145  $attachments = $this->getAttachments();
146 
147  $ids = displayFieldAsList($attachments, "attachment_id");
148 ?>
149  <input type="hidden" value="<?echo $ids?>" name="<?echo $field?>" id="<?echo $this->parent->id?>_<?echo $field?>"/>
150  <ul id="<?echo $this->parent->id?>_<?echo $field?>_list" class="<?echo $this->cssClass?>" style='display: table-cell'>
151  <?
152 
153  foreach($attachments as $attachment)
154  {
155  ?> <li id='attachment_<?echo $attachment->attachment_id?>' class='<?echo $this->cssClass?>'><span>
156  <?php
157  $this->drawOneAttachment($attachment);
158  ?>
159  &nbsp;
160  <a href="#" onclick='new AttachmentUploader().deleteAttachment("<?echo $attachment->filename?>", <?echo $attachment->attachment_id?>); return false' title='Delete this Attachment'><img src='<?echo $this->deleteIcon?>' style='display:inline-block; vertical-align: middle' alt='Delete this Attachment'/></a>
161  </span></li>
162 <?
163  }
164  ?>
165  </ul>
166  <a class='add_attachment_label' href="#" onclick="new AttachmentUploader().addAttachment(); return false"> <?echo $this->addLabel?> </a>
167 <?
168  $this->_endField($field);
169  }
170 
171  // JDG 5/24/11 - add readonly handling
172  function renderReadOnly($field = "")
173  {
174  if (!$field) $field = $this->field;
175 
176  $attachments = $this->getAttachments();
177  if(count($attachments) < 2) $this->colspan = 1;
178 
179  $this->_startField($field);
180 
181  if (count($attachments) == 0)
182  {
183  echo "<p>{$this->emptyMessage}</p>";
184  }
185  else if(count($attachments) == 1)
186  {
187  // Omit ul li list if only one
188 
189  $this->drawOneAttachment($attachments[0]);
190  }
191  else if(count($attachments) > 0)
192  {
193 ?>
194  <ul id="<?echo $this->parent->id?>_<?echo $field?>_list" class="<?echo $this->cssClass?>">
195 <?
196  foreach($attachments as $attachment)
197  {
198  ?> <li id='attachment_<?echo $attachment->attachment_id?>' class='<?echo $this->cssClass?>'><span>
199  <?php
200  $this->drawOneAttachment($attachment);
201  ?>
202  </span></li>
203 <?
204  }
205 ?></ul><?php
206  }
207 
208  $this->_endField($field);
209  }
210 
211  function getAttachments()
212  {
213  $xref = new $this->xrefClass;
214 
215  $pk = $this->getMatchingPK($xref, $this->parent->data);
216  $pkv = $this->parent->data->get($pk);
217 
218  if ($pkv)
219  {
220  $attachments = query(Attachment, "WHERE attachment_id in (select attachment_id from {$xref->table} where $pk=$pkv) ORDER BY attachment_id");
221  }
222  else
223  {
224  $attachments = array();
225  }
226 
227  return $attachments;
228  }
229 
231  {
232  $icon = getDocIcon($attachment->filename);
233 ?>
234  <img src="<?echo $icon?>" alt="Icon" style="display:inline-block;vertical-align:middle"/>&nbsp;
235  <a href='/action/attachment/download?attachment_id=<?echo $attachment->attachment_id?>'><?echo $attachment->filename?></a>&nbsp;
236  (<?echo $attachment->file_size?>)
237 <?
238  }
239 
240  static function formatAttachment($attachment)
241  {
242  $icon = getDocIcon($attachment->filename);
243 
244  return $attachment->format("<img src='{$icon}' alt='Icon' style='display:inline-block;vertical-align:middle'/>&nbsp;<a href='/action/attachment/download?attachment_id={attachment_id}'>{filename}</a>&nbsp;({file_size})");
245  }
246 
247  function postProcess($field = "")
248  {
249  if (!$field) $field = $this->field;
250 
251  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
252  trace("AttachmentFieldRenderer postProcess field $field value ". $_POST[$field], 3);
253 
254  $xref = new $this->xrefClass;
255 
256  $obj = $this->parent->data;
257  $pk = $this->getMatchingPK($xref, $obj);
258 
259  // Clear out current relations
260 
261  $xref = new $this->xrefClass;
262  $xref->delete("WHERE {$pk}=".$obj->get($pk));
263 
264  if (isset($_POST[$field]))
265  {
266  trace("AttachmentFieldRenderer postProcess _POST attachment field is ". $_POST[$field], 3);
267  $ids = explode(",", $_POST[$field]);
268 
269  foreach($ids as $id)
270  {
271  $xref = new $this->xrefClass;
272  $xref->set($pk, $obj->get($pk));
273  $xref->set("attachment_id", $id);
274  $xref->save();
275  }
276  }
277  else
278  {
279  trace("AttachmentFieldRenderer postProcess _POST attachment field is empty", 3);
280  }
281  }
282 }
283 ?>
& nbsp
Definition: index.inc:49
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$table cssClass
$helpTree style
Definition: tree.inc:46
$form action
Definition: edit.inc:67
if(! $attachment_id) $attachment
Definition: delete.inc:42
$icon
Definition: upload.inc:92
$bookmark title
AttachmentFieldRenderer(&$form, $field, $xrefClass)
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
$topicList colspan
Definition: group_form.inc:54
$openData field