CMS  Version 3.9
photo_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");
40 
42 {
43  static function create($form, $field, $xrefClass)
44  {
46  {
48  }
49  else
50  {
52  }
53  }
54 
56  {
58  }
59 
60  function renderScript($field)
61  {
62  return "";
63  }
64 
65  function renderField($field = "")
66  {
67  if (!$field) $field = $this->field;
68 
69  $xref = new $this->xrefClass;
70 
71  $this->_startField($field);
72 
73  $attachments = $this->getAttachments();
74 
75  $ids = displayFieldAsList($attachments, "attachment_id");
76 ?>
77  <div class='photo_attachment_handler'>
78  <input type="hidden" value="<?echo $ids?>" name="<?echo $field?>" id="<?echo $this->parent->id?>_<?echo $field?>"/>
79  <ul id="<?echo $this->parent->id?>_<?echo $field?>_list" class="<?echo $this->cssClass?>" style='display: table-cell'>
80  <?
81 
82  foreach($attachments as $attachment)
83  {
84  ?> <li id='attachment_<?echo $attachment->attachment_id?>' class='<?echo $this->cssClass?>'><span>
85  <?php
86  $this->drawOneAttachment($attachment);
87  ?>
88  &nbsp;
89  <a href="#" onclick='new PhotoAttachmentUploader().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>
90  </span></li>
91 <?
92  }
93  ?>
94  </ul>
95  <p id="attachmentDialogMessage"></p>
96  <a href="#" onclick="new PhotoAttachmentUploader().capturePhoto(); return false" class="button"> Take a Photo </a>&nbsp;&nbsp;
97  <a href="#" onclick="new PhotoAttachmentUploader().choosePhoto(); return false" class="button">Upload a Photo</a>
98  </div>
99  <script type="text/javascript">
100  window.addEvent('domready', function()
101  {
102  new PhotoAttachmentUploader().setup('<?echo $this->parent->id?>_<?echo $this->field?>_list', '<?echo $this->parent->id?>_<?echo $this->field?>', '<?echo $this->cssClass?>', '<?echo $this->deleteIcon?>');
103  });
104  </script>
105 <?
106  $this->_endField($field);
107  }
108 
109  // JDG 5/24/11 - add readonly handling
110  function renderReadOnly($field = "")
111  {
112  if (!$field) $field = $this->field;
113 
114  $attachments = $this->getAttachments();
115  if(count($attachments) < 2) $this->colspan = 1;
116 
117  echo "<tr>\n";
118  $this->_printLabel($field);
119  echo "<td";
120  if ($this->parent->valueCSS) echo " class='{$this->parent->valueCSS}'";
121  echo ">";
122 
123  // Omit ul li list if only one
124  if(count($attachments) == 1)
125  {
126  $this->drawOneAttachment($attachments[0]);
127  }
128  elseif(count($attachments) > 0)
129  {
130 ?>
131  <ul id="<?echo $this->parent->id?>_<?echo $field?>_list" class="<?echo $this->cssClass?>">
132 <?
133  foreach($attachments as $attachment)
134  {
135  ?> <li id='attachment_<?echo $attachment->attachment_id?>' class='<?echo $this->cssClass?>'><span>
136  <?php
137  $this->drawOneAttachment($attachment);
138  ?>
139  </span></li>
140 <?
141  }
142 ?></ul><?php
143  }
144 
145  echo "</td></tr>\n";
146  }
147 
148  function getAttachments()
149  {
150  $xref = new $this->xrefClass;
151 
152  $pk = $this->getMatchingPK($xref, $this->parent->data);
153  $pkv = $this->parent->data->get($pk);
154 
155  if ($pkv)
156  {
157  $attachments = query(Attachment, "WHERE attachment_id in (select attachment_id from {$xref->table} where $pk=$pkv) ORDER BY attachment_id");
158  }
159  else
160  {
161  $attachments = array();
162  }
163 
164  return $attachments;
165  }
166 
168  {
169  $icon = getDocIcon($attachment->filename);
170 ?>
171  <img src="<?echo $icon?>" alt="Icon" style="display:inline-block;vertical-align:middle"/>&nbsp;
172  <a href='/action/attachment/download?attachment_id=<?echo $attachment->attachment_id?>'><?echo $attachment->filename?></a>&nbsp;
173  (<?echo $attachment->file_size?>)
174 <?
175  }
176 
177  function postProcess($field = "")
178  {
179  trace("AttachmentFieldRenderer postProcess field $field value ". $_POST[$field], 3);
180  if ($this->parent->readOnlyForm || $this->parent->isReadOnly($field) || array_key_exists($field, $this->parent->hidden)) return "";
181 
182  if (!$field) $field = $this->field;
183 
184  $xref = new $this->xrefClass;
185 
186  $obj = $this->parent->data;
187  $pk = $this->getMatchingPK($xref, $obj);
188 
189  // Clear out current relations
190 
191  $xref = new $this->xrefClass;
192  $xref->delete("WHERE {$pk}=".$obj->get($pk));
193 
194  if (isset($_POST[$field]))
195  {
196  trace("AttachmentFieldRenderer postProcess _POST attachment field is ". $_POST[$field], 3);
197  $ids = explode(",", $_POST[$field]);
198 
199  foreach($ids as $id)
200  {
201  $xref = new $this->xrefClass;
202  $xref->set($pk, $obj->get($pk));
203  $xref->set("attachment_id", $id);
204  $xref->save();
205  }
206  }
207  else
208  {
209  trace("AttachmentFieldRenderer postProcess _POST attachment field is empty", 3);
210  }
211  }
212 }
213 ?>
& nbsp
Definition: index.inc:49
$form
$_POST["owner_id"]
Definition: blog_form.inc:54
$helpTree style
Definition: tree.inc:46
if(! $attachment_id) $attachment
Definition: delete.inc:42
$icon
Definition: upload.inc:92
$bookmark title
AttachmentFieldRenderer(&$form, $field, $xrefClass)
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static create($form, $field, $xrefClass)
PhotoAttachmentFieldRenderer($form, $field, $xrefClass)
$topicList colspan
Definition: group_form.inc:54