Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
design_area_custom_image_edit_fns.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/designs/design_area/design_area_edit_fns.inc';
19 require_once SQ_FUDGE_PATH.'/general/text.inc';
20 
33 {
34 
35 
40  function __construct()
41  {
42  parent::__construct();
43 
44  }//end constructor()
45 
46 
57  public function paintImageUpload(Design_Area_Custom_Image $asset, Backend_Outputter $o, $prefix)
58  {
59  if (!$asset->writeAccess('attributes')) return FALSE;
60  file_upload($prefix);
61  $o->note(translate('core_only_upload_files_of_type', make_readable_list($asset->allowed_extensions)));
62  $o->note(translate('core_image_size_restricted', $asset->attr('max_width'), $asset->attr('max_height')));
63 
64  return TRUE;
65 
66  }//end paintImageUpload()
67 
68 
79  public function processImageUpload(Design_Area_Custom_Image $asset, Backend_Outputter $o, $prefix)
80  {
81  $info = get_file_upload_info($prefix);
82 
83  // return on failed or no upload
84  if ($info === FALSE || empty($info)) {
85  return FALSE;
86  }
87 
88  // check that the extension is allowed
89  require_once SQ_FUDGE_PATH.'/general/file_system.inc';
90  if (!in_array(get_file_type($info['name']) , $asset->allowed_extensions)) {
91  trigger_localised_error('CORE0190', E_USER_WARNING, make_readable_list($asset->allowed_extensions));
92  return FALSE;
93  }
94 
95  // check that the image dimensions are not greater than our limits
96  $size = getimagesize($info['tmp_name']);
97  if ($size === FALSE) return FALSE;
98  $max_width = (int) $asset->attr('max_width');
99  $max_height = (int) $asset->attr('max_height');
100  if (($max_width && $size[0] > $max_width) || ($max_height && $size[1] > $max_height)) {
101  trigger_localised_error('CORE0191', E_USER_WARNING, $asset->attr('max_width'), $asset->attr('max_height'));
102  return FALSE;
103  }
104 
105  create_directory($asset->data_path_public);
106 
107  $old_image = $asset->attr('image');
108  $new_image = $info['name'];
109 
110  // delete existing uploaded file if it exists
111  if ($old_image && $old_image != $new_image && file_exists($asset->data_path_public.'/'.$old_image)) {
112  if (!unlink($asset->data_path_public.'/'.$old_image)) {
113  trigger_localised_error('CORE0163', E_USER_WARNING);
114  return FALSE;
115  }
116  }
117 
118  // copy over the new uploaded file
119  if (!move_uploaded_file($info['tmp_name'], $asset->data_path_public.'/'.$info['name'])) {
120  trigger_localised_error('CORE0166', E_USER_WARNING);
121  return FALSE;
122  }
123 
124  if (!$asset->setAttrValue('image', $new_image)) {
125  return FALSE;
126  }
127 
128  return TRUE;
129 
130  }//end processImageUpload()
131 
132 
143  public function paintCurrentImage(Design_Area_Custom_Image $asset, Backend_Outputter $o, $prefix)
144  {
145  $image = $asset->attr('image');
146  if (empty($image) || !file_exists($asset->data_path_public.'/'.$image)) {
147  echo translate('core_design_area_custom_image_no_upload');
148  } else {
149  ?>
150  <a href="<?php echo sq_web_path('data').'/assets/'.$asset->type().'/'.get_asset_hash($asset->id).'/'.$asset->id.'/'.$image; ?>" target="_blank"><?php echo $image; ?></a>
151  <?php
152  }
153  return TRUE;
154 
155  }//end paintCurrentImage()
156 
157 
158 }//end class
159 ?>