CMS  Version 3.9
email_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::using("attachment", "document");
40 Fakoli::usingFile("framework/field_renderers.inc");
41 
42 
52 class EmailAttachmentFieldRenderer extends FieldRenderer
53 {
54  var $colspan = 2;
55  var $field;
56  var $label;
57  var $cssClass = "attachment";
58  var $deleteIcon = "/fakoli/images/delete.gif";
59 
60  function __construct(&$form, $field, $label)
61  {
62  $this->field = $field;
63 
64  $this->FieldRenderer($form);
65  $this->label = ($label) ? $label : $form->prettifyFieldName($field);
66 
67  if($form->hasAdditional($field))
68  {
69  die("field $field already exists in form");
70  }
71  $form->add($this);
72 
73  if (isset($form->attachment_css_class)) $this->cssClass = $form->attachment_css_class;
74  if (isset($form->attachment_delete_icon)) $this->deleteIcon = $form->attachment_delete_icon;
75  }
76 
77  function renderField($field = "")
78  {
79  if (!$field) $field = $this->field;
80 
81  $this->_startField($field);
82 
83  echo "<input id='{$this->parent->id}_{$field}' type='hidden' name='{$field}' value='' />\n";
84  echo "<ul id='{$this->parent->id}_{$field}_list' class='{$this->cssClass}' style='display: table-cell'>\n</ul>\n";
85  echo "<a href='#' onclick='emailAttachmentMgr.showAddAttachmentDialog(); return false'>Add an Attachment</a>\n";
86 
87  $this->_endField($field);
88  }
89 
90  function renderScript($field)
91  {
92  if (!$field) $field = $this->field;
93 
94  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
95 
96  global $dialogs;
97 
98  ob_start();
99  ?>
100  <div class="dialog" id="attachmentDialog" style="width: 260px">
101  <div class="dialog_header" id="attachmentDialogHeader">
102  <div style="padding: 4px;">
103  <div style="float: right">&nbsp;<a id='closeAttachmentDialog'>Close &times;</a></div>
104  <span"style="font-weight: bold" id="attachmentDialogTitle">Add an Attachment</span>
105  </div>
106  </div>
107  <div class="dialog_body">
108  <p id="attachmentDialogMessage"></p>
109  <form id="attachmentForm" method="POST" enctype="multipart/form-data" action="/action/attachment/upload" >
110  <br/>
111  <label>Attachment File</label><br/>
112  <input type="file" name="attachmentFile"/>
113  <br/>
114  <br/><input type="submit" class="button" name="submit" value="Upload Attachment" style="float: right; margin-bottom: 4px"/>
115  </form>
116  </div>
117  </div>
118  <?
119  $dialogs .= ob_get_contents();
120  ob_end_clean();
121 
122  echo "<script type='text/javascript'>\n";
123  echo "var emailAttachmentMgr;\n";
124  echo "window.addEvent('domready', function()\n";
125  echo "{\n";
126  echo "\temailAttachmentMgr = new EmailAttachmentManager('{$this->parent->id}_{$field}_list', '{$this->parent->id}_{$field}', '{$this->cssClass}', '{$this->deleteIcon}');\n";
127  echo "});\n";
128  echo "</script>\n";
129 
130  }
131 
139  function preProcess($field = "")
140  {
141  if (!$field) $field = $this->field;
142  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
143 
144  if (isset($_POST[$field]) && $_POST[$field] != "")
145  {
146  $ids = explode(",", $_POST[$field]);
147 
148  foreach($ids as $id)
149  {
150  $attachment = new Attachment($id);
151  $file = sanitizePath($this->formatFilePath($attachment));
152  $attachments[$attachment->filename] = $file;
153  }
154  $this->parent->data->set($field, $attachments);
155  }
156  }
157 
158 
166  function postProcess($field = "")
167  {
168  if (!$field) $field = $this->field;
169 
170  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
171 
172  if (isset($_POST[$field]) && $_POST[$field] != "")
173  {
174  $ids = explode(",", $_POST[$field]);
175 
176  foreach($ids as $id)
177  {
178  $attachment = new Attachment($id);
179  $file = $this->formatFilePath($attachment);
180  if (file_exists($file))
181  {
182  unlink($file);
183  }
184  $attachment->delete();
185  }
186  }
187  }
188 
189  function formatFilePath($attachment)
190  {
191  global $config;
192 
193  $dir = $config["attachmentdir"];
194  $base = $config['uploadbase'];
195 
196  $file = "$base/$dir{$attachment->local_filename}";
197 
198  return $file;
199  }
200 } // end EmailAttachmentFieldRenderer
201 
212 class EmailLibraryAttachmentFieldRenderer extends FieldRenderer
213 {
214  var $colspan = 2;
215  var $field;
216  var $label;
217  var $cssClass = "attachment";
218  var $deleteIcon = "/fakoli/images/delete.gif";
219 
220  function __construct(&$form, $field, $label)
221  {
222  $this->field = $field;
223 
224  $this->FieldRenderer($form);
225  $this->label = ($label) ? $label : $form->prettifyFieldName($field);
226 
227  if($form->hasAdditional($field))
228  {
229  die("field $field already exists in form");
230  }
231  $form->add($this);
232 
233  if (isset($form->attachment_css_class)) $this->cssClass = $form->attachment_css_class;
234  if (isset($form->attachment_delete_icon)) $this->deleteIcon = $form->attachment_delete_icon;
235  }
236 
237 
238  function getAttachmentType()
239  {
240  return "document";
241  }
242 
243  function renderField($field = "")
244  {
245  if (!$field) $field = $this->field;
246 
247  $this->_startField($field);
248 
249  echo "<input id='{$this->parent->id}_{$field}' type='hidden' name='{$field}' value='' />\n";
250  echo "<ul id='{$this->parent->id}_{$field}_list' class='{$this->cssClass}' style='display: table-cell'>\n</ul>\n";
251  echo "<a href='#' onclick='emailDocAttachmentMgr.showDocumentSelectDialog(); return false'>Add an Attachment</a>\n";
252 
253  $this->_endField($field);
254  }
255 
256  function renderReadOnly($field = "")
257  {
258  // nothing to do
259  }
260 
268  function preProcess($field = "")
269  {
270  if (!$field) $field = $this->field;
271 
272  trace("EmailAttachmentFieldRenderer preProcess field $field value ". $_POST[$field], 3);
273  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
274 
275  if (isset($_POST[$field]) && $_POST[$field] != "")
276  {
277  $ids = explode(",", $_POST[$field]);
278 
279  foreach($ids as $id)
280  {
281  $document = new Document($id);
282  $library = $document->DocumentLibrary();
283  $file = sanitizePath($library->getLibraryDirectory() . DIRECTORY_SEPARATOR . $document->file);
284  $attachments[basename($document->file)] = $file;
285  }
286  $this->parent->data->set($field, $attachments);
287  }
288  }
289 
290 
291  function renderScript($field)
292  {
293  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
294 
295  if (!$field) $field = $this->field;
296 
297  echo "<script type='text/javascript' src='/components/email/js/email_attachment_manager.js'></script>\n";
298  echo "<script type='text/javascript'>\n";
299  echo "var emailDocAttachmentMgr;\n";
300  echo "window.addEvent('load', function()\n";
301  echo "{\n";
302  echo "\temailDocAttachmentMgr = new EmailDocumentAttachmentManager('{$this->parent->id}_{$field}_list', '{$this->parent->id}_{$field}', '{$this->cssClass}', '{$this->deleteIcon}');\n";
303  echo "});\n";
304  echo "</script>\n";
305  }
306 
307 
311  static function getLibraryOptions()
312  {
313  $libraries = Query::create(DocumentLibrary, "ORDER BY name")->execute();
314  $libraries = filterByRole($libraries, "allow_access");
315 
316  $options = array();
317  if (count($libraries) > 0)
318  {
319  foreach($libraries as $library)
320  {
321  $options[$library->document_library_id] = $library->name;
322  }
323  }
324  return $options;
325  }
326 
330  static function getDocumentOptions()
331  {
332  $documents = Query::create(Document, "ORDER BY title")->execute();
333  $documents = filterByRole($documents, "allow_access");
334 
335  if (count($documents) > 0)
336  {
337  $documents = regroupList($documents, "document_library_id");
338  }
339  return $documents;
340  }
341 }
342 ?>
& nbsp
Definition: index.inc:49
$form
$table cssClass
$helpTree style
Definition: tree.inc:46
$form action
Definition: edit.inc:67
$dir
Definition: delete.inc:44
if(! $attachment_id) $attachment
Definition: delete.inc:42
$base
Definition: delete.inc:45
$bookmark title
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static usingFile()
Uses the specified framework file(s) from the framework directory.
Definition: core.inc:369
$openData field