CMS  Version 3.9
image_upload_field_renderer.inc
Go to the documentation of this file.
1 <?php
7 /**************************************************************
8 
9 Copyright (c) 2007-2014 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::usingFeature("field_renderers");
40 
48 class ImageUploadFieldRenderer extends FieldRenderer
49 {
50  var $handler;
51  var $size;
52  var $field;
53  var $gallery;
54  var $imageRecordHandler = null;
55 
57  {
58  $this->FieldRenderer($form);
59 
60  $this->gallery = $gallery;
61  $this->field = $field;
62  $this->imageRecordHandler = $imageRecordHandler;
63 
64  if ($form->data->hasField($field))
65  {
66  $form->override($field, $label, $this);
67  }
68  else
69  {
70  $form->add($this, $field);
71  $form->overrides[$field]['label'] = $label;
72  }
73 
74  $size = 40;
75  }
76 
77  function renderField($field)
78  {
79  $this->_startField($field);
80 
81  $image_id = $this->parent->data->get($field);
82 
83  if ($image_id)
84  {
85  echo "<img src='/action/image/iconize?image_id=$image_id' alt=''/>";
86  }
87 
88  $accept = $this->accept ? " accept='{$this->accept}'" : "";
89  echo "<input id='{$this->parent->id}_{$field}' type='file' name='$field' value='".$this->parent->data->get($field)."' size='{$this->size}'{$accept}>";
90  $this->_endField($field);
91  }
92 
94  {
95  $this->_startField($field);
96 
97  echo $this->parent->data->get($field);
98 
99  $this->_endField($field);
100  }
101 
102  function preProcess($field = "")
103  {
104  global $user;
105 
106  $image = new ImageRecord();
107 
108  if ($this->imageRecordHandler)
109  {
110  call_user_func($this->handler, $image, $field, $this->parent->data);
111  }
112  else
113  {
114  $image->title = $_FILES[$field]["name"];
115  $image->ALT_tag = "";
116  $image->published = false;
117  }
118 
119  $image->gallery_id = $this->gallery->gallery_id;
120  $image->owner_id = $user->get($user->getPrimaryKey());
121 
123  {
124  $image->save();
125  }
126 
127  $this->parent->data->set($field, $image->image_id);
128 
129  return true;
130  }
131 
132  function required($message = "")
133  {
134  $this->parent->validator->add(new RequiredFileValidator($this->field, $this->label, $message));
135  }
136 }
137 ?>
$form
$image_id
Definition: image_form.inc:41
$image
Definition: image_form.inc:46
static usingFeature()
Uses the specified framework feature(s).
Definition: core.inc:388
static imageUploadHandler($field, $image)
Upload the image file specified in the given field.
Field renderer for file upload data fields.
ImageUploadFieldRenderer(&$form, $field, $label, $gallery, $imageRecordHandler=null)
global $user
$message
Definition: mail_to.inc:49
$openData field