ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilAdvancedMDSubstitution.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
33 {
34  private static $instances = null;
35  private static $mappings = null;
36 
37  protected $db;
38 
39  protected $type;
40  protected $substitutions;
41  protected $bold = array();
42  protected $newline = array();
43 
44  protected $enabled_desc = true;
45  protected $enabled_field_names = true;
46  protected $active = false;
47  protected $date_fields = array();
48  protected $datetime_fields = array();
49  protected $active_fields = array();
50 
51 
52  /*
53  * Singleton class
54  * Use _getInstance
55  * @access private
56  * @param
57  */
58  private function __construct($a_type)
59  {
60  global $ilDB;
61 
62  $this->db = $ilDB;
63  $this->type = $a_type;
64 
65  $this->initECSMappings();
66  $this->read();
67  }
68 
77  public static function _getInstanceByObjectType($a_type)
78  {
79  if (isset(self::$instances[$a_type])) {
80  return self::$instances[$a_type];
81  }
82  return self::$instances[$a_type] = new ilAdvancedMDSubstitution($a_type);
83  }
84 
92  public function sortDefinitions($a_definitions)
93  {
94  $sorted = array();
95  foreach ($this->substitutions as $field_id) {
96  if (isset($a_definitions[$field_id])) {
97  $sorted[$field_id] = $a_definitions[$field_id];
98  unset($a_definitions[$field_id]);
99  }
100  }
101  return array_merge($sorted, $a_definitions);
102  }
103 
110  public function isActive()
111  {
112  return $this->active;
113  }
114 
121  public function isDescriptionEnabled()
122  {
123  return (bool) $this->enabled_desc;
124  }
125 
133  public function enableDescription($a_status)
134  {
135  $this->enabled_desc = $a_status;
136  }
137 
144  public function enabledFieldNames()
145  {
146  return (bool) $this->enabled_field_names;
147  }
148 
156  public function enableFieldNames($a_status)
157  {
158  $this->enabled_field_names = $a_status;
159  }
160 
170  public function getParsedSubstitutions($a_ref_id, $a_obj_id)
171  {
172  if (!count($this->getSubstitutions())) {
173  return array();
174  }
175 
176  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
177  $values_records = ilAdvancedMDValues::preloadedRead($this->type, $a_obj_id);
178 
179  $counter = 0;
180  foreach ($this->getSubstitutions() as $field_id) {
181  if (!isset($this->active_fields[$field_id])) {
182  continue;
183  }
184 
185  $value = $this->parseValue($field_id, $values_records);
186 
187  if ($value === null) {
188  if ($this->hasNewline($field_id) and $counter) {
189  $substituted[$counter-1]['newline'] = true;
190  }
191  continue;
192  }
193 
194  $substituted[$counter]['name'] = $this->active_fields[$field_id];
195  $substituted[$counter]['value'] = $value;
196  $substituted[$counter]['bold'] = $this->isBold($field_id);
197  if ($this->hasNewline($field_id)) {
198  $substituted[$counter]['newline'] = true;
199  } else {
200  $substituted[$counter]['newline'] = false;
201  }
202  $substituted[$counter]['show_field'] = $this->enabledFieldNames();
203  $counter++;
204  }
205 
206  return $substituted ? $substituted : array();
207  }
208 
219  private function parseValue($a_field_id, $a_values_records)
220  {
221  global $ilUser;
222 
223  if ($this->type == 'crs' or $this->type == 'rcrs') {
224  // Special handling for ECS fields
225  // @FIXME
226  /*
227  if($a_field_id == self::$mappings->getMappingByECSName('begin') and
228  $end = self::$mappings->getMappingByECSName('end'))
229  {
230  // Parse a duration
231  $start = in_array($a_field_id,$this->date_fields) ?
232  new ilDate($a_values[$a_field_id],IL_CAL_UNIX) :
233  new ilDateTime($a_values[$a_field_id],IL_CAL_UNIX);
234  $end = in_array($end,$this->date_fields) ?
235  new ilDate($a_values[$end],IL_CAL_UNIX) :
236  new ilDateTime($a_values[$end],IL_CAL_UNIX);
237 
238  include_once('./Services/Calendar/classes/class.ilCalendarUtil.php');
239  $weekday = ilCalendarUtil::_numericDayToString($start->get(IL_CAL_FKT_DATE,'w',$ilUser->getTimeZone()),false);
240 
241  ilDatePresentation::setUseRelativeDates(false);
242  $value = ilDatePresentation::formatPeriod($start,$end);
243  ilDatePresentation::setUseRelativeDates(true);
244  return $weekday.', '.$value;
245  }
246  */
247  }
248 
249  foreach ($a_values_records as $a_values) {
250  if ($a_values->getADTGroup()->hasElement($a_field_id)) {
251  $element = $a_values->getADTGroup()->getElement($a_field_id);
252  if (!$element->isNull()) {
253  return ilADTFactory::getInstance()->getPresentationBridgeForInstance($element)->getList();
254  }
255  }
256  }
257  }
258 
259 
267  public function resetSubstitutions()
268  {
269  $this->substitutions = array();
270  $this->bold = array();
271  $this->newline = array();
272  }
273 
281  public function appendSubstitution($a_field_id, $a_bold = false, $a_newline = false)
282  {
283  $this->substitutions[] = $a_field_id;
284  if ($a_bold) {
285  $this->bold[] = $a_field_id;
286  }
287  if ($a_newline) {
288  $this->newline[] = $a_field_id;
289  }
290  }
291 
299  public function getSubstitutions()
300  {
301  return $this->substitutions ? $this->substitutions : array();
302  }
303 
311  public function isSubstituted($a_field_id)
312  {
313  return in_array($a_field_id, $this->getSubstitutions());
314  }
315 
323  public function isBold($a_field_id)
324  {
325  #var_dump("<pre>",$this->bold,$a_field_id,"</pre>");
326 
327  return in_array($a_field_id, $this->bold);
328  }
329 
337  public function hasNewline($a_field_id)
338  {
339  return in_array($a_field_id, $this->newline);
340  }
347  public function update()
348  {
349  global $ilDB;
350 
351  $counter = 0;
352  $substitutions = array();
353 
354  foreach ($this->substitutions as $field_id) {
355  $substitutions[$counter]['field_id'] = $field_id;
356  $substitutions[$counter]['bold'] = $this->isBold($field_id);
357  $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
358  $counter++;
359  }
360 
361  $query = "DELETE FROM adv_md_substitutions WHERE obj_type = " . $ilDB->quote($this->type, 'text');
362  $res = $ilDB->manipulate($query);
363 
364 
365  $values = array(
366  'obj_type' => array('text',$this->type),
367  'substitution' => array('clob',serialize($substitutions)),
368  'hide_description' => array('integer',!$this->isDescriptionEnabled()),
369  'hide_field_names' => array('integer',!$this->enabledFieldNames())
370  );
371  $ilDB->insert('adv_md_substitutions', $values);
372  }
373 
380  private function read()
381  {
382  global $ilDB;
383 
384  // Check active status
385  $query = "SELECT active,field_id,amfd.title FROM adv_md_record amr " .
386  "JOIN adv_md_record_objs amro ON amr.record_id = amro.record_id " .
387  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id " .
388  "WHERE active = 1 " .
389  "AND obj_type = " . $this->db->quote($this->type, 'text') . " ";
390  $res = $this->db->query($query);
391  $this->active = $res->numRows() ? true : false;
392  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
393  $this->active_fields[$row->field_id] = $row->title;
394  }
395 
396  $query = "SELECT * FROM adv_md_substitutions " .
397  "WHERE obj_type = " . $this->db->quote($this->type, 'text') . " ";
398  $res = $this->db->query($query);
399  $this->substitutions = array();
400  $this->bold = array();
401  $this->newline = array();
402  while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
403  $tmp_substitutions = unserialize($row->substitution);
404  if (is_array($tmp_substitutions)) {
405  foreach ($tmp_substitutions as $substitution) {
406  if ($substitution['field_id']) {
407  $this->substitutions[] = $substitution['field_id'];
408  }
409  if ($substitution['bold']) {
410  $this->bold[] = $substitution['field_id'];
411  }
412  if ($substitution['newline']) {
413  $this->newline[] = $substitution['field_id'];
414  }
415  }
416  }
417  $this->enabled_desc = !$row->hide_description;
418  $this->enabled_field_names = !$row->hide_field_names;
419  }
420 
421  if ($this->type == 'crs' or $this->type == 'rcrs') {
422  // Handle ECS substitutions
423  /*
424  if($begin = self::$mappings->getMappingByECSName('begin') and
425  $end = self::$mappings->getMappingByECSName('end'))
426  {
427  // Show something like 'Monday, 30.12.2008 9:00 - 12:00'
428  unset($this->active_fields[$end]);
429  }
430  */
431  }
432  }
433 
440  private function initECSMappings()
441  {
442  return true;
443 
444  include_once('./Services/WebServices/ECS/classes/class.ilECSDataMappingSettings.php');
445 
446  if (isset(self::$mappings) and is_object(self::$mappings)) {
447  return true;
448  }
449  self::$mappings = ilECSDataMappingSettings::_getInstance();
450  return true;
451  }
452 }
enableFieldNames($a_status)
enable field names
isSubstituted($a_field_id)
is substituted
parseValue($a_field_id, $a_values_records)
special handling for date(time) values and ECS dates
enableDescription($a_status)
Enable description presentation.
static getInstance()
Get singleton.
static _getInstanceByObjectType($a_type)
Singleton: use this method to get an instance.
$counter
$a_type
Definition: workflow.php:92
foreach($_POST as $key=> $value) $res
isDescriptionEnabled()
Is description enabled.
getSubstitutions()
get substitution string
$ilUser
Definition: imgupload.php:18
$query
Create styles array
The data for the language used.
global $ilDB
appendSubstitution($a_field_id, $a_bold=false, $a_newline=false)
append field to substitutions
static preloadedRead($a_type, $a_obj_id)
sortDefinitions($a_definitions)
Sort definitions.
static _getInstance()
Get Singleton instance.
getParsedSubstitutions($a_ref_id, $a_obj_id)
Substitute.