ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilImagemapFileInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 include_once "./Services/Form/classes/class.ilImageFileInputGUI.php";
12 
14 {
15  protected $areas = array();
16  protected $image_path = "";
17  protected $image_path_web = "";
18  protected $line_color = "";
19 
26  function __construct($a_title = "", $a_postvar = "")
27  {
28  global $lng;
29 
30  parent::__construct($a_title, $a_postvar);
31  }
32 
33  public function setAreas($a_areas)
34  {
35  $this->areas = $a_areas;
36  }
37 
38  public function getLineColor()
39  {
40  return $this->line_color;
41  }
42 
43  public function setLineColor($a_color)
44  {
45  $this->line_color = $a_color;
46  }
47 
48  public function getImagePath()
49  {
50  return $this->image_path;
51  }
52 
53  public function setImagePath($a_path)
54  {
55  $this->image_path = $a_path;
56  }
57 
58  public function getImagePathWeb()
59  {
60  return $this->image_path_web;
61  }
62 
63  public function setImagePathWeb($a_path_web)
64  {
65  $this->image_path_web = $a_path_web;
66  }
67 
68  public function setAreasByArray($a_areas)
69  {
70  if (is_array($a_areas['name']))
71  {
72  $this->areas = array();
73  include_once "./Modules/TestQuestionPool/classes/class.assAnswerImagemap.php";
74  foreach ($a_areas['name'] as $idx => $name)
75  {
76  array_push($this->areas, new ASS_AnswerImagemap($name, $a_areas['points'][$idx], $idx, $a_areas['coords'][$idx], $a_areas['shape'][$idx]));
77  }
78  }
79  }
80 
81  public function getAreas()
82  {
83  return $this->areas;
84  }
85 
91  function setValueByArray($a_values)
92  {
93  $this->setValue($a_values[$this->getPostVar() . '_name']);
94  $this->setAreasByArray($a_values[$this->getPostVar()]['coords']);
95  }
96 
97  public function setValue($a_value)
98  {
99  parent::setValue($a_value);
100  }
101 
107  function checkInput()
108  {
109  global $lng;
110 
111  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
112  // remove trailing '/'
113  while (substr($_FILES[$this->getPostVar()]["name"],-1) == '/')
114  {
115  $_FILES[$this->getPostVar()]["name"] = substr($_FILES[$this->getPostVar()]["name"],0,-1);
116  }
117 
118  $filename = $_FILES[$this->getPostVar()]["name"];
119  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
120  $suffix = $filename_arr["extension"];
121  $mimetype = $_FILES[$this->getPostVar()]["type"];
122  $size_bytes = $_FILES[$this->getPostVar()]["size"];
123  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
124  $error = $_FILES[$this->getPostVar()]["error"];
125 
126  // error handling
127  if ($error > 0)
128  {
129  switch ($error)
130  {
131  case UPLOAD_ERR_INI_SIZE:
132  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
133  return false;
134  break;
135 
136  case UPLOAD_ERR_FORM_SIZE:
137  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
138  return false;
139  break;
140 
141  case UPLOAD_ERR_PARTIAL:
142  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
143  return false;
144  break;
145 
146  case UPLOAD_ERR_NO_FILE:
147  if ($this->getRequired())
148  {
149  if (!strlen($this->getValue()))
150  {
151  $this->setAlert($lng->txt("form_msg_file_no_upload"));
152  return false;
153  }
154  }
155  break;
156 
157  case UPLOAD_ERR_NO_TMP_DIR:
158  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
159  return false;
160  break;
161 
162  case UPLOAD_ERR_CANT_WRITE:
163  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
164  return false;
165  break;
166 
167  case UPLOAD_ERR_EXTENSION:
168  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
169  return false;
170  break;
171  }
172  }
173 
174  // check suffixes
175  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
176  is_array($this->getSuffixes()))
177  {
178  if (!in_array(strtolower($suffix), $this->getSuffixes()))
179  {
180  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
181  return false;
182  }
183  }
184 
185  // virus handling
186  if ($_FILES[$this->getPostVar()]["tmp_name"] != "")
187  {
188  $vir = ilUtil::virusHandling($temp_name, $filename);
189  if ($vir[0] == false)
190  {
191  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
192  return false;
193  }
194  }
195 
196  $max = 0;
197  if (is_array($_POST[$this->getPostVar()]['coords']['name']))
198  {
199  foreach ($_POST[$this->getPostVar()]['coords']['name'] as $idx => $name)
200  {
201  if ((!strlen($_POST[$this->getPostVar()]['coords']['points'][$idx])) && ($this->getRequired))
202  {
203  $this->setAlert($lng->txt('form_msg_area_missing_points'));
204  return false;
205  }
206  if ((!is_numeric($_POST[$this->getPostVar()]['coords']['points'][$idx])))
207  {
208  $this->setAlert($lng->txt('form_msg_numeric_value_required'));
209  return false;
210  }
211  if ($_POST[$this->getPostVar()]['coords']['points'][$idx] > 0) $max = $_POST[$this->getPostVar()]['coords']['points'][$idx];
212  }
213  }
214 
215  if ($max == 0 && (!$filename))
216  {
217  $this->setAlert($lng->txt("enter_enough_positive_points"));
218  return false;
219  }
220  return true;
221  }
222 
226  function insert(&$a_tpl)
227  {
228  global $lng;
229 
230  $template = new ilTemplate("tpl.prop_imagemap_file.html", true, true, "Modules/TestQuestionPool");
231 
232  $this->outputSuffixes($template, "allowed_image_suffixes");
233 
234  if ($this->getImage() != "")
235  {
236  if (strlen($this->getValue()))
237  {
238  $template->setCurrentBlock("has_value");
239  $template->setVariable("TEXT_IMAGE_NAME", $this->getValue());
240  $template->setVariable("POST_VAR_D", $this->getPostVar());
241  $template->parseCurrentBlock();
242  }
243  $template->setCurrentBlock("image");
244  if (count($this->getAreas()))
245  {
246  include_once "./Modules/TestQuestionPool/classes/class.ilImagemapPreview.php";
247  $preview = new ilImagemapPreview($this->getImagePath().$this->getValue());
248  foreach ($this->getAreas() as $index => $area)
249  {
250  $preview->addArea($index, $area->getArea(), $area->getCoords(), $area->getAnswertext(), "", "", true, $this->getLineColor());
251  }
252  $preview->createPreview();
253  $imagepath = $this->getImagePathWeb() . $preview->getPreviewFilename($this->getImagePath(), $this->getValue()) . "?img=" . time();
254  $template->setVariable("SRC_IMAGE", $imagepath);
255  }
256  else
257  {
258  $template->setVariable("SRC_IMAGE", $this->getImage());
259  }
260  $template->setVariable("ALT_IMAGE", $this->getAlt());
261  $template->setVariable("POST_VAR_D", $this->getPostVar());
262  $template->setVariable("TXT_DELETE_EXISTING",
263  $lng->txt("delete_existing_file"));
264  $template->setVariable("TEXT_ADD_RECT", $lng->txt('add_rect'));
265  $template->setVariable("TEXT_ADD_CIRCLE", $lng->txt('add_circle'));
266  $template->setVariable("TEXT_ADD_POLY", $lng->txt('add_poly'));
267  $template->parseCurrentBlock();
268  }
269 
270  if (is_array($this->getAreas()))
271  {
272  $counter = 0;
273  foreach ($this->getAreas() as $area)
274  {
275  if (strlen($area->getPoints()))
276  {
277  $template->setCurrentBlock('area_points_value');
278  $template->setVariable('VALUE_POINTS', $area->getPoints());
279  $template->parseCurrentBlock();
280  }
281  if (strlen($area->getAnswertext()))
282  {
283  $template->setCurrentBlock('area_name_value');
284  $template->setVariable('VALUE_NAME', $area->getAnswertext());
285  $template->parseCurrentBlock();
286  }
287  $template->setCurrentBlock('row');
288  $class = ($counter % 2 == 0) ? "even" : "odd";
289  if ($counter == 0) $class .= " first";
290  if ($counter == count($this->getAreas())-1) $class .= " last";
291  $template->setVariable("ROW_CLASS", $class);
292  $template->setVariable('POST_VAR_R', $this->getPostVar());
293  $template->setVariable('TEXT_SHAPE', strtoupper($area->getArea()));
294  $template->setVariable('VALUE_SHAPE', $area->getArea());
295  $coords = preg_replace("/(\d+,\d+,)/", "\$1 ", $area->getCoords());
296  $template->setVariable('VALUE_COORDINATES', $area->getCoords());
297  $template->setVariable('TEXT_COORDINATES', $coords);
298  $template->setVariable('COUNTER', $counter);
299  $template->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
300  $template->parseCurrentBlock();
301  $counter++;
302  }
303  $template->setCurrentBlock("areas");
304  $template->setVariable("TEXT_NAME", $lng->txt("hint"));
305  $template->setVariable("TEXT_POINTS", $lng->txt("points"));
306  $template->setVariable("TEXT_SHAPE", $lng->txt("shape"));
307  $template->setVariable("TEXT_COORDINATES", $lng->txt("coordinates"));
308  $template->setVariable("TEXT_COMMANDS", $lng->txt("actions"));
309  $template->parseCurrentBlock();
310  }
311 
312  $template->setVariable("POST_VAR", $this->getPostVar());
313  $template->setVariable("ID", $this->getFieldId());
314  $template->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice")." ".
315  $this->getMaxFileSizeString());
316 
317  $a_tpl->setCurrentBlock("prop_generic");
318  $a_tpl->setVariable("PROP_GENERIC", $template->get());
319  $a_tpl->parseCurrentBlock();
320 
321  global $tpl;
322  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
324  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagemap.js");
325  }
326 
327 }