ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilImagemapFileInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
5 
13 include_once "./Services/Form/classes/class.ilImageFileInputGUI.php";
14 
16 {
17  protected $areas = array();
18  protected $image_path = "";
19  protected $image_path_web = "";
20  protected $line_color = "";
21 
22  protected $pointsUncheckedFieldEnabled = false;
23 
30  public function __construct($a_title = "", $a_postvar = "")
31  {
32  global $lng;
33 
34  parent::__construct($a_title, $a_postvar);
35  }
36 
38  {
39  $this->pointsUncheckedFieldEnabled = (bool) $pointsUncheckedFieldEnabled;
40  }
41 
43  {
45  }
46 
47  public function setAreas($a_areas)
48  {
49  $this->areas = $a_areas;
50  }
51 
52  public function getLineColor()
53  {
54  return $this->line_color;
55  }
56 
57  public function setLineColor($a_color)
58  {
59  $this->line_color = $a_color;
60  }
61 
62  public function getImagePath()
63  {
64  return $this->image_path;
65  }
66 
67  public function setImagePath($a_path)
68  {
69  $this->image_path = $a_path;
70  }
71 
72  public function getImagePathWeb()
73  {
74  return $this->image_path_web;
75  }
76 
77  public function setImagePathWeb($a_path_web)
78  {
79  $this->image_path_web = $a_path_web;
80  }
81 
82  public function setAreasByArray($a_areas)
83  {
84  if (is_array($a_areas['name'])) {
85  $this->areas = array();
86  include_once "./Modules/TestQuestionPool/classes/class.assAnswerImagemap.php";
87  foreach ($a_areas['name'] as $idx => $name) {
88  if ($this->getPointsUncheckedFieldEnabled() && isset($a_areas['points_unchecked'])) {
89  $pointsUnchecked = $a_areas['points_unchecked'][$idx];
90  } else {
91  $pointsUnchecked = 0.0;
92  }
93 
94  array_push($this->areas, new ASS_AnswerImagemap(
95  $name,
96  $a_areas['points'][$idx],
97  $idx,
98  $a_areas['coords'][$idx],
99  $a_areas['shape'][$idx],
100  -1,
101  $pointsUnchecked
102  ));
103  }
104  }
105  }
106 
107  public function getAreas()
108  {
109  return $this->areas;
110  }
111 
117  public function setValueByArray($a_values)
118  {
119  $this->setValue($a_values[$this->getPostVar() . '_name']);
120  $this->setAreasByArray($a_values[$this->getPostVar()]['coords']);
121  }
122 
123  public function setValue($a_value)
124  {
125  parent::setValue($a_value);
126  }
127 
133  public function checkInput()
134  {
135  global $lng;
136 
137  if (is_array($_POST[$this->getPostVar()])) {
139  }
140  // remove trailing '/'
141  $_FILES[$this->getPostVar()]["name"] = rtrim($_FILES[$this->getPostVar()]["name"], '/');
142 
143  $filename = $_FILES[$this->getPostVar()]["name"];
144  $filename_arr = pathinfo($_FILES[$this->getPostVar()]["name"]);
145  $suffix = $filename_arr["extension"];
146  $mimetype = $_FILES[$this->getPostVar()]["type"];
147  $size_bytes = $_FILES[$this->getPostVar()]["size"];
148  $temp_name = $_FILES[$this->getPostVar()]["tmp_name"];
149  $error = $_FILES[$this->getPostVar()]["error"];
150 
151  // error handling
152  if ($error > 0) {
153  switch ($error) {
154  case UPLOAD_ERR_INI_SIZE:
155  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
156  return false;
157  break;
158 
159  case UPLOAD_ERR_FORM_SIZE:
160  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
161  return false;
162  break;
163 
164  case UPLOAD_ERR_PARTIAL:
165  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
166  return false;
167  break;
168 
169  case UPLOAD_ERR_NO_FILE:
170  if ($this->getRequired()) {
171  if (!strlen($this->getValue())) {
172  $this->setAlert($lng->txt("form_msg_file_no_upload"));
173  return false;
174  }
175  }
176  break;
177 
178  case UPLOAD_ERR_NO_TMP_DIR:
179  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
180  return false;
181  break;
182 
183  case UPLOAD_ERR_CANT_WRITE:
184  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
185  return false;
186  break;
187 
188  case UPLOAD_ERR_EXTENSION:
189  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
190  return false;
191  break;
192  }
193  }
194 
195  // check suffixes
196  if ($_FILES[$this->getPostVar()]["tmp_name"] != "" &&
197  is_array($this->getSuffixes())) {
198  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
199  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
200  return false;
201  }
202  }
203 
204  // virus handling
205  if ($_FILES[$this->getPostVar()]["tmp_name"] != "") {
206  $vir = ilUtil::virusHandling($temp_name, $filename);
207  if ($vir[0] == false) {
208  $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
209  return false;
210  }
211  }
212 
213  $max = 0;
214  if (is_array($_POST[$this->getPostVar()]['coords']['name'])) {
215  foreach ($_POST[$this->getPostVar()]['coords']['name'] as $idx => $name) {
216  if ((!strlen($_POST[$this->getPostVar()]['coords']['points'][$idx])) && ($this->getRequired)) {
217  $this->setAlert($lng->txt('form_msg_area_missing_points'));
218  return false;
219  }
220  if ((!is_numeric($_POST[$this->getPostVar()]['coords']['points'][$idx]))) {
221  $this->setAlert($lng->txt('form_msg_numeric_value_required'));
222  return false;
223  }
224  if ($_POST[$this->getPostVar()]['coords']['points'][$idx] > 0) {
225  $max = $_POST[$this->getPostVar()]['coords']['points'][$idx];
226  }
227  }
228  }
229 
230  if ($max == 0 && (!$filename) && !$_FILES['imagemapfile']['tmp_name']) {
231  $this->setAlert($lng->txt("enter_enough_positive_points"));
232  return false;
233  }
234  return true;
235  }
236 
240  public function insert($a_tpl)
241  {
242  global $lng;
243 
244  $template = new ilTemplate("tpl.prop_imagemap_file.html", true, true, "Modules/TestQuestionPool");
245 
246  $this->outputSuffixes($template, "allowed_image_suffixes");
247 
248  if ($this->getImage() != "") {
249  if (strlen($this->getValue())) {
250  $template->setCurrentBlock("has_value");
251  $template->setVariable("TEXT_IMAGE_NAME", $this->getValue());
252  $template->setVariable("POST_VAR_D", $this->getPostVar());
253  $template->parseCurrentBlock();
254  }
255  $template->setCurrentBlock("image");
256  if (count($this->getAreas())) {
257  include_once "./Modules/TestQuestionPool/classes/class.ilImagemapPreview.php";
258  $preview = new ilImagemapPreview($this->getImagePath() . $this->getValue());
259  foreach ($this->getAreas() as $index => $area) {
260  $preview->addArea($index, $area->getArea(), $area->getCoords(), $area->getAnswertext(), "", "", true, $this->getLineColor());
261  }
262  $preview->createPreview();
263  $imagepath = $this->getImagePathWeb() . $preview->getPreviewFilename($this->getImagePath(), $this->getValue()) . "?img=" . time();
264  $template->setVariable("SRC_IMAGE", $imagepath);
265  } else {
266  $template->setVariable("SRC_IMAGE", $this->getImage());
267  }
268  $template->setVariable("ALT_IMAGE", $this->getAlt());
269  $template->setVariable("POST_VAR_D", $this->getPostVar());
270  $template->setVariable(
271  "TXT_DELETE_EXISTING",
272  $lng->txt("delete_existing_file")
273  );
274  $template->setVariable("TEXT_ADD_RECT", $lng->txt('add_rect'));
275  $template->setVariable("TEXT_ADD_CIRCLE", $lng->txt('add_circle'));
276  $template->setVariable("TEXT_ADD_POLY", $lng->txt('add_poly'));
277  $template->parseCurrentBlock();
278  }
279 
280  if (is_array($this->getAreas()) && $this->getAreas()) {
281  $counter = 0;
282  foreach ($this->getAreas() as $area) {
283  if (strlen($area->getPoints())) {
284  $template->setCurrentBlock('area_points_value');
285  $template->setVariable('VALUE_POINTS', $area->getPoints());
286  $template->parseCurrentBlock();
287  }
288  if ($this->getPointsUncheckedFieldEnabled()) {
289  if (strlen($area->getPointsUnchecked())) {
290  $template->setCurrentBlock('area_points_unchecked_value');
291  $template->setVariable('VALUE_POINTS_UNCHECKED', $area->getPointsUnchecked());
292  $template->parseCurrentBlock();
293  }
294 
295  $template->setCurrentBlock('area_points_unchecked_field');
296  $template->parseCurrentBlock();
297  }
298  if (strlen($area->getAnswertext())) {
299  $template->setCurrentBlock('area_name_value');
300  $template->setVariable('VALUE_NAME', $area->getAnswertext());
301  $template->parseCurrentBlock();
302  }
303  $template->setCurrentBlock('row');
304  $template->setVariable('POST_VAR_R', $this->getPostVar());
305  $template->setVariable('TEXT_SHAPE', strtoupper($area->getArea()));
306  $template->setVariable('VALUE_SHAPE', $area->getArea());
307  $coords = preg_replace("/(\d+,\d+,)/", "\$1 ", $area->getCoords());
308  $template->setVariable('VALUE_COORDINATES', $area->getCoords());
309  $template->setVariable('TEXT_COORDINATES', $coords);
310  $template->setVariable('COUNTER', $counter);
311  $template->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
312  $template->parseCurrentBlock();
313  $counter++;
314  }
315  $template->setCurrentBlock("areas");
316  $template->setVariable("TEXT_NAME", $lng->txt("ass_imap_hint"));
317  if ($this->getPointsUncheckedFieldEnabled()) {
318  $template->setVariable("TEXT_POINTS", $lng->txt("points_checked"));
319 
320  $template->setCurrentBlock('area_points_unchecked_head');
321  $template->setVariable("TEXT_POINTS_UNCHECKED", $lng->txt("points_unchecked"));
322  $template->parseCurrentBlock();
323  } else {
324  $template->setVariable("TEXT_POINTS", $lng->txt("points"));
325  }
326  $template->setVariable("TEXT_SHAPE", $lng->txt("shape"));
327  $template->setVariable("TEXT_COORDINATES", $lng->txt("coordinates"));
328  $template->setVariable("TEXT_COMMANDS", $lng->txt("actions"));
329  $template->parseCurrentBlock();
330  }
331 
332  $template->setVariable("POST_VAR", $this->getPostVar());
333  $template->setVariable("ID", $this->getFieldId());
334  $template->setVariable("TXT_BROWSE", $lng->txt("select_file"));
335  $template->setVariable("TXT_MAX_SIZE", $lng->txt("file_notice") . " " .
336  $this->getMaxFileSizeString());
337 
338  $a_tpl->setCurrentBlock("prop_generic");
339  $a_tpl->setVariable("PROP_GENERIC", $template->get());
340  $a_tpl->parseCurrentBlock();
341 
342  global $tpl;
343  $tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
344  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagemap.js");
345  }
346 }
getSuffixes()
Get Accepted Suffixes.
getAlt()
Get Alternative Text.
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
$template
$tpl
Definition: ilias.php:10
Class for true/false or yes/no answers.
getPostVar()
Get Post Variable.
$preview
static get($a_glyph, $a_text="")
Get glyph html.
$index
Definition: metadata.php:60
setValueByArray($a_values)
Set value by array.
$coords
Definition: example_030.php:88
Image map image preview creator.
setAlert($a_alert)
Set Alert Text.
$counter
This class represents an image map file property in a property form.
$error
Definition: Error.php:17
if($format !==null) $name
Definition: metadata.php:146
checkInput()
Check input, strip slashes etc.
getFieldId()
Get Post Variable.
special template class to simplify handling of ITX/PEAR
static stripSlashesRecursive($a_data, $a_strip_html=true, $a_allow="")
Strip slashes from array and sub-arrays.
outputSuffixes($a_tpl, $a_block="allowed_suffixes")
Create styles array
The data for the language used.
insert($a_tpl)
Insert property html.
This class represents an image file property in a property form.
__construct($a_title="", $a_postvar="")
Constructor.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
setPointsUncheckedFieldEnabled($pointsUncheckedFieldEnabled)
$_POST["username"]