ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilDclContentExporter.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
5 require_once('./Modules/DataCollection/classes/class.ilObjDataCollectionAccess.php');
6 require_once('./Modules/DataCollection/classes/class.ilObjDataCollectionGUI.php');
7 require_once('./Modules/DataCollection/classes/Content/class.ilDclRecordListGUI.php');
8 require_once('./Modules/DataCollection/classes/Table/class.ilDclTable.php');
9 require_once ('./Services/Export/classes/class.ilExport.php');
10 
11 
12 
21 {
22  const SOAP_FUNCTION_NAME = 'exportDataCollectionContent';
23 
24  const EXPORT_EXCEL = 'xlsx';
25  const IN_PROGRESS_POSTFIX = '.prog';
26 
30  protected $ref_id;
31 
35  protected $table_id;
36 
40  protected $filter;
41 
45  protected $dcl;
46 
50  protected $lng;
51 
55  protected $table;
56 
57  public function __construct($ref_id, $table_id = null, $filter = array()) {
58  global $DIC;
59  $lng = $DIC['lng'];
60 
61  $this->ref_id = $ref_id;
62  $this->table_id = $table_id;
63  $this->filter = $filter;
64 
65  $this->dcl = new ilObjDataCollection($ref_id);
66  $this->tables = ($table_id)? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
67 
68  $lng->loadLanguageModule('dcl');
69  $this->lng = $lng;
70  }
71 
80  public function sanitizeFilename($filename) {
81  $dangerous_filename_characters = array(" ", '"', "'", "&", "/", "\\", "?", "#", "`");
82  return str_replace($dangerous_filename_characters, "_", iconv("utf-8","ascii//TRANSLIT", $filename));
83  }
84 
85 
92  public function getExportContentPath($format) {
93  return ilExport::_getExportDirectory($this->dcl->getId(), $format, 'dcl').'/';
94  }
95 
96 
106  $col = 0;
107  foreach ($table->getFields() as $field) {
108  if ($field->getExportable()) {
109  $record->fillRecordFieldExcelExport($worksheet, $row, $col, $field->getId());
110  }
111  }
112  }
113 
114 
123  $col = 0;
124 
125  foreach ($table->getFields() as $field) {
126  if ($field->getExportable()) {
127  $field->fillHeaderExcel($worksheet, $row, $col);
128  }
129  }
130  }
131 
132 
140  protected function fillMetaExcel($table, $worksheet, $row) { }
141 
142 
152  public function export($format = self::EXPORT_EXCEL, $filepath = null, $send = false) {
153 
154  if(count($this->tables) == 0) {
155  return;
156  }
157 
158  if(empty($filepath)) {
159  $filepath = $this->getExportContentPath($format);
160  ilUtil::makeDirParents($filepath);
161 
162  $basename = (isset($this->table_id))? $this->tables[0]->getTitle() : 'complete';
163  $filename = time() . '__' . $basename . "_".date("Y-m-d_H-i");
164 
165  $filepath .= $this->sanitizeFilename($filename);
166  } else {
167  $filename = pathinfo($filepath, PATHINFO_FILENAME);
168  }
169 
170  $in_progress_file = $filepath.self::IN_PROGRESS_POSTFIX;
171  file_put_contents($in_progress_file, "");
172 
173  $data_available = false;
174  $fields_available = false;
175  switch ($format) {
176  case self::EXPORT_EXCEL:
177  require_once "./Services/Excel/classes/class.ilExcel.php";
178 
179  $adapter = new ilExcel();
180  foreach($this->tables as $table) {
182 
183  $list = $table->getPartialRecords(null, null, null, 0, $this->filter);
184  $data_available = $data_available || ($list['total'] > 0);
185  $fields_available = $fields_available || (count($table->getExportableFields()) > 0);
186  if ($list['total'] > 0 && count($table->getExportableFields()) > 0) {
187  // only 31 character-long table-titles are allowed
188  $title = substr($table->getTitle(), 0, 31);
189  $adapter->addSheet($title);
190  $row = 1;
191 
192  $this->fillMetaExcel($table, $adapter, $row);
193 
194  // #14813
195  $pre = $row;
196  $this->fillHeaderExcel($table, $adapter, $row);
197  if ($pre == $row) {
198  $row ++;
199  }
200 
201  foreach ($list['records'] as $set) {
202  $this->fillRowExcel($table, $adapter, $set, $row);
203  $row ++; // #14760
204  }
205 
206  $data_available = true;
207  }
208  }
209  break;
210  }
211 
212  if (file_exists($in_progress_file)) {
213  unlink($in_progress_file);
214  }
215 
216  if (!$data_available) {
217  ilUtil::sendInfo($this->lng->txt('dcl_no_export_content_available'));
218  return false;
219  }
220 
221  if (!$fields_available) {
222  global $ilCtrl;
223  ilUtil::sendInfo(sprintf($this->lng->txt('dcl_no_export_fields_available'),
224  $ilCtrl->getLinkTargetByClass(array('ilDclTableListGUI', 'ilDclTableEditGUI', 'ilDclFieldListGUI'), 'listFields')));
225  return false;
226  }
227 
228  if ($send) {
229  $adapter->sendToClient($filename);
230  exit;
231  } else {
232  $adapter->writeToFile($filepath);
233  }
234  }
235 
236 
246  public function exportAsync($format = self::EXPORT_EXCEL, $filepath = null) {
247  global $DIC;
248  $ilLog = $DIC['ilLog'];
249 
250  $method = self::SOAP_FUNCTION_NAME;
251 
252  $soap_params = array($this->dcl->getRefId());
253  array_push($soap_params, $this->table_id, $format, $filepath);
254 
255  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
256  $client_id = $_COOKIE['ilClientId'];
257 
258  // Start cloning process using soap call
259  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
260 
261  $soap_client = new ilSoapClient();
262  $soap_client->setResponseTimeout(5);
263  $soap_client->enableWSDL(true);
264 
265  $ilLog->write(__METHOD__.': Trying to call Soap client...');
266 
267  array_unshift($soap_params, $new_session_id.'::'.$client_id);
268 
269  if($soap_client->init())
270  {
271  $ilLog->info('Calling soap '.$method.' method with params '.print_r($soap_params, true));
272  $res = $soap_client->call($method, $soap_params);
273  }
274  else
275  {
276  $ilLog->warning('SOAP clone call failed. Calling clone method manually');
277  require_once('./webservice/soap/include/inc.soap_functions.php');
278  if(method_exists('ilSoapFunctions', $method)) {
279  $res = ilSoapFunctions::$method($new_session_id.'::'.$client_id, $this->dcl->getRefId(), $this->table_id, $format, $filepath);
280  } else {
281  throw new ilDclException("SOAP call ".$method." does not exists!");
282  }
283  }
284 
285  return $res;
286  }
287 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$worksheet
fillHeaderExcel(ilDclTable $table, ilExcel $worksheet, $row)
Fill Excel header.
getFields()
Returns all fields of this table including the standard fields.
export($format=self::EXPORT_EXCEL, $filepath=null, $send=false)
Creates an export of a specific datacollection table.
Class ilDclBaseFieldModel.
static resetCache()
Resets all the cache fields.
global $ilCtrl
Definition: ilias.php:18
__construct($ref_id, $table_id=null, $filter=array())
fillRowExcel(ilDclTable $table, ilExcel $worksheet, ilDclBaseRecordModel $record, $row)
Fill a excel row.
static sendInfo($a_info="", $a_keep=false)
Send Info Message to Screen.
fillMetaExcel($table, $worksheet, $row)
Fill Excel meta-data.
sanitizeFilename($filename)
Sanitize the given filename The ilUtil::_sanitizeFilemame() does not clean enough.
Create styles array
The data for the language used.
fillRecordFieldExcelExport(ilExcel $worksheet, &$row, &$col, $field_id)
exportAsync($format=self::EXPORT_EXCEL, $filepath=null)
Start Export async.
static _duplicate($a_session_id)
Duplicate session.
Class ilDclBaseRecordModel.
getExportContentPath($format)
Return export path.
$_COOKIE['ilClientId']
Definition: BPMN2Parser.php:15
Class ilDclException.
static _getExportDirectory($a_obj_id, $a_type="xml", $a_obj_type="", $a_entity="")
Get export directory for an repository object.
Hook-Class for exporting data-collections (used in SOAP-Class) This Class avoids duplicated code by r...
global $DIC
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
$client_id
Class ilObjDataCollection.