Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
image_variety_edit_fns.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset_edit/asset_edit_fns.inc';
19 
32 {
33 
34 
40  {
41  $this->Asset_Edit_Fns();
42  // override static screens - only display details because the others don't apply
43  $this->static_screens = Array(
44  'details' => Array(
45  'name' => translate('details'),
46  'force_unlock' => FALSE,
47  'lock_type' => 'none',
48  ),
49  );
50 
51  }//end constructor
52 
53 
63  public function paintInlineInterface(Image_Variety $asset, Backend_Outputter $o, $prefix)
64  {
65  $wa = $asset->writeAccess('attributes');
66 
67  if (!$asset->id && !$wa) {
68  return FALSE;
69  }
70 
71  $variety_type = $asset->attr('variety_type');
72  $constrain = $asset->attr('constrain');
73 
74  $int_edit_params = new SimpleXMLElement('<int width="8" />');
75  $bool_edit_params = new SimpleXMLElement('<boolean true_text="yes" false_text="no" />');
76  $sel_edit_params = new SimpleXMLElement('<selection style="box" columns="3" extras="" />');
77 
78  if ($asset->name == '') {
79  $o->openField(translate('name'));
80  $text_edit_params = new SimpleXMLElement('<text width="15" />');
81  $attr = $asset->getAttribute('name');
82  $attr->setEditParams($text_edit_params);
83  $attr->paint($prefix.'_'.$attr->name, !$wa);
84  unset($attr);
85  $o->closeField();
86  }
87 
88  $o->openField(translate('variety_type'));
89  $js_prefix = preg_replace('/[^a-z0-9_]/', '_', $prefix);
90  ?>
91  <script type="text/javascript">
92  <!-- //
93 
94  function <?php echo $js_prefix; ?>_update_visible_fields() {
95 
96  var variety_type = get_form_element_value("<?php echo $prefix; ?>_variety_type");
97  var constrain = get_form_element_value("<?php echo $prefix; ?>_constrain");
98 
99  document.getElementById('<?php echo $prefix; ?>_field_constrain').style.display = (variety_type == 'resize') ? '' : 'none';
100  document.getElementById('<?php echo $prefix; ?>_field_width').style.display = (variety_type == 'resize' && (constrain == 'width' || constrain == 'none')) ? '' : 'none';
101  document.getElementById('<?php echo $prefix; ?>_field_height').style.display = (variety_type == 'resize' && (constrain == 'height' || constrain == 'none')) ? '' : 'none';
102  document.getElementById('<?php echo $prefix; ?>_field_dimension').style.display = (variety_type == 'resize' && constrain == 'dimension') ? '' : 'none';
103  document.getElementById('<?php echo $prefix; ?>_field_upload').style.display = (variety_type == 'upload') ? '' : 'none';
104  return true;
105 
106  }// end <?php echo $prefix; ?>_update_visible_fields()
107 
108  // -->
109  </script>
110  <?php
111  $sel_edit_params->attributes()->extras = 'onclick="return '.$js_prefix.'_update_visible_fields();"';
112  $attr = $asset->getAttribute('variety_type');
113  $attr->setEditParams($sel_edit_params);
114  $attr->paint($prefix.'_'.$attr->name, !$wa);
115  unset($attr);
116  $o->closeField();
117 
118  $o->openField(translate('constrain_by'), '', '', ($variety_type != 'resize'), $prefix.'_field_constrain');
119  $sel_edit_params->attributes()->extras = 'onclick="return '.$js_prefix.'_update_visible_fields();"';
120  $attr = $asset->getAttribute('constrain');
121  $attr->setEditParams($sel_edit_params);
122  $attr->paint($prefix.'_'.$attr->name, !$wa);
123  unset($attr);
124  $o->closeField();
125 
126  $o->openField(translate('width'), '', '', ($variety_type != 'resize' || $constrain != 'width'), $prefix.'_field_width');
127  $attr = $asset->getAttribute('width');
128  $attr->setEditParams($int_edit_params);
129  $attr->paint($prefix.'_'.$attr->name, !$wa);
130  unset($attr);
131  $o->closeField();
132 
133  $o->openField(translate('height'), '', '', ($variety_type != 'resize' || $constrain != 'height'), $prefix.'_field_height');
134  $attr = $asset->getAttribute('height');
135  $attr->setEditParams($int_edit_params);
136  $attr->paint($prefix.'_'.$attr->name, !$wa);
137  unset($attr);
138  $o->closeField();
139 
140  $o->openField(translate('dimension'), '', translate('dimension_note'), ($variety_type != 'resize' || $constrain != 'dimension'), $prefix.'_field_dimension');
141  $attr = $asset->getAttribute('dimension');
142  $attr->setEditParams($int_edit_params);
143  $attr->paint($prefix.'_'.$attr->name, !$wa);
144  unset($attr);
145  $o->closeField();
146 
147  $o->openField(translate('upload'), '', '', ($variety_type != 'upload'), $prefix.'_field_upload');
148  if ($wa) file_upload($prefix.'_upload_file');
149  $o->closeField();
150 
151  if ($asset->id) {
152  $o->openField(translate('current_variety'));
153  ?>
154  <a href="<?php echo current_url(TRUE, TRUE).'?a='.$asset->id?>&now=<?php echo time()?>" target="_blank"><?php echo translate('core_click_to_open_in_new_window'); ?></a>
155  <?php
156  $o->closeField();
157  }
158 
159  return TRUE;
160 
161  }//end paintInlineInterface()
162 
163 
173  public function processInlineInterface(Image_Variety $asset, Backend_Outputter $o, $prefix)
174  {
175  if (!$asset->writeAccess('attributes')) return FALSE;
176 
177  $changes_made = FALSE;
178 
179  $vars = Array('variety_type', 'constrain', 'width', 'height', 'dimension');
180  if ($asset->attr('name') == '') {
181  array_unshift($vars, 'name');
182  }
183 
184  foreach ($vars as $attr_name) {
185  $attr = $asset->getAttribute($attr_name);
186  $attr->process($prefix.'_'.$attr->name);
187  if ($attr->processed && $asset->setAttrValue($attr->name, $attr->value)) {
188  $changes_made = TRUE;
189  }
190  }
191 
192  if ($asset->_imageid && $asset->attr('variety_type') == 'upload') {
193  $info = get_file_upload_info($prefix.'_upload_file');
194  if ($this->processUploadedFile($asset, $info)) {
195  $changes_made = TRUE;
196  }
197  }
198 
199  if (!$changes_made) {
200  return FALSE;
201  } else {
202  return $asset->saveAttributes();
203  }
204 
205  }//end processInlineInterface()
206 
207 
224  public function processUploadedFile(Image_Variety $asset, Array $info=Array())
225  {
226  if (empty($asset->_imageid)) return FALSE;
227  if ($asset->attr('name') == '') return FALSE;
228 
229  // return on failed or no upload
230  if ($info === FALSE || empty($info)) {
231  return FALSE;
232  }
233 
234  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
235  $fv = $GLOBALS['SQ_SYSTEM']->getFileVersioning();
236  if (!is_dir($asset->data_path) && !create_directory($asset->data_path)) {
237  return FALSE;
238  }
239 
240  $image = $GLOBALS['SQ_SYSTEM']->am->getAsset($asset->_imageid);
241  if (is_null($image)) return FALSE;
242 
243  $img_ext = get_file_type($image->name);
244  $upload_ext = get_file_type($info['name']);
245 
246  if ($img_ext != $upload_ext) {
247  trigger_localised_error('CORE0003', E_USER_WARNING);
248  return FALSE;
249  }
250 
251  $d_old_filename = $asset->attr('filename');
252  $d_new_filename = $asset->attr('name').'.'.$img_ext;
253  $d_old_file = $asset->data_path.'/'.$d_old_filename;
254  $d_new_file = $asset->data_path.'/'.$d_new_filename;
255 
256  // if the filename has changed then we need to remove the old file
257  if ($d_old_filename != $d_new_filename) {
258  $asset->deleteImage();
259 
260  } else {
261  // make sure we have the latest version checked out if we are modifying the file
262  $up_to_date = $fv->upToDate($d_new_file);
263  if ($up_to_date & FUDGE_FV_OLD_VERSION) {
264  if (!$fv->checkOut($asset->data_path_suffix.'/'.$d_new_filename, $asset->data_path)) {
265  trigger_localised_error('CORE0103', E_USER_WARNING);
266  return FALSE;
267  }
268  }
269 
270  }// endif
271 
272  // copy over the new uploaded file
273  if (is_uploaded_file($info['tmp_name'])) {
274  // a normal uploaded file
275  if (!move_uploaded_file($info['tmp_name'], $d_new_file)) {
276  trigger_localised_error('CORE0038', E_USER_WARNING ,$info['tmp_name'], $d_new_file);
277  return FALSE;
278  }
279  } else if (isset($info['non_uploaded_file']) && $info['non_uploaded_file'] === TRUE) {
280  // we have special permission to move a non-uploaded file
281  if (!copy($info['tmp_name'], $d_new_file)) {
282  trigger_localised_error('CORE0036',E_USER_WARNING, $info['tmp_name'], $d_new_file);
283  return FALSE;
284  }
285  } else {
286  trigger_localised_error('CORE0067', E_USER_ERROR);
287  return FALSE;
288  }
289 
290  $up_to_date = $fv->upToDate($d_new_file);
291 
292  // file not in repository
293  if ($up_to_date & FUDGE_FV_NOT_CHECKED_OUT) {
294  if (!$fv->add($asset->data_path_suffix, $d_new_file, '')) {
295  return FALSE;
296  }
297 
298  if (!$fv->checkOut($asset->data_path_suffix.'/'.$d_new_filename, $asset->data_path)) {
299  trigger_localised_error('CORE0103', E_USER_WARNING);
300  return FALSE;
301  }
302 
303  } else if ($up_to_date & FUDGE_FV_MODIFIED) {
304  if (!$fv->commit($d_new_file, '')) return FALSE;
305 
306  }
307 
308  // if the filename has changed then we need to update the look ups
309  if (!$asset->setAttrValue('filename', $d_new_filename)) {
310  return FALSE;
311  }
312 
313  return TRUE;
314 
315  }//end processUploadedFile()
316 
317 
318 }//end class
319 
320 ?>