ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  {
59  global $DIC;
60  $lng = $DIC['lng'];
61 
62  $this->ref_id = $ref_id;
63  $this->table_id = $table_id;
64  $this->filter = $filter;
65 
66  $this->dcl = new ilObjDataCollection($ref_id);
67  $this->tables = ($table_id)? array($this->dcl->getTableById($table_id)) : $this->dcl->getTables();
68 
69  $lng->loadLanguageModule('dcl');
70  $this->lng = $lng;
71  }
72 
81  public function sanitizeFilename($filename)
82  {
83  $dangerous_filename_characters = array(" ", '"', "'", "&", "/", "\\", "?", "#", "`");
84  return str_replace($dangerous_filename_characters, "_", iconv("utf-8", "ascii//TRANSLIT", $filename));
85  }
86 
87 
94  public function getExportContentPath($format)
95  {
96  return ilExport::_getExportDirectory($this->dcl->getId(), $format, 'dcl') . '/';
97  }
98 
99 
109  {
110  $col = 0;
111  foreach ($table->getFields() as $field) {
112  if ($field->getExportable()) {
113  $record->fillRecordFieldExcelExport($worksheet, $row, $col, $field->getId());
114  }
115  }
116  }
117 
118 
127  {
128  $col = 0;
129 
130  foreach ($table->getFields() as $field) {
131  if ($field->getExportable()) {
132  $field->fillHeaderExcel($worksheet, $row, $col);
133  }
134  }
135  }
136 
137 
145  protected function fillMetaExcel($table, $worksheet, $row)
146  {
147  }
148 
149 
159  public function export($format = self::EXPORT_EXCEL, $filepath = null, $send = false)
160  {
161  if (count($this->tables) == 0) {
162  return;
163  }
164 
165  if (empty($filepath)) {
166  $filepath = $this->getExportContentPath($format);
167  ilUtil::makeDirParents($filepath);
168 
169  $basename = (isset($this->table_id))? $this->tables[0]->getTitle() : 'complete';
170  $filename = time() . '__' . $basename . "_" . date("Y-m-d_H-i");
171 
172  $filepath .= $this->sanitizeFilename($filename);
173  } else {
174  $filename = pathinfo($filepath, PATHINFO_FILENAME);
175  }
176 
177  $in_progress_file = $filepath . self::IN_PROGRESS_POSTFIX;
178  file_put_contents($in_progress_file, "");
179 
180  $data_available = false;
181  $fields_available = false;
182  switch ($format) {
183  case self::EXPORT_EXCEL:
184  require_once "./Services/Excel/classes/class.ilExcel.php";
185 
186  $adapter = new ilExcel();
187  foreach ($this->tables as $table) {
189 
190  $list = $table->getPartialRecords(null, null, null, 0, $this->filter);
191  $data_available = $data_available || ($list['total'] > 0);
192  $fields_available = $fields_available || (count($table->getExportableFields()) > 0);
193  if ($list['total'] > 0 && count($table->getExportableFields()) > 0) {
194  // only 31 character-long table-titles are allowed
195  $title = substr($table->getTitle(), 0, 31);
196  $adapter->addSheet($title);
197  $row = 1;
198 
199  $this->fillMetaExcel($table, $adapter, $row);
200 
201  // #14813
202  $pre = $row;
203  $this->fillHeaderExcel($table, $adapter, $row);
204  if ($pre == $row) {
205  $row++;
206  }
207 
208  foreach ($list['records'] as $set) {
209  $this->fillRowExcel($table, $adapter, $set, $row);
210  $row++; // #14760
211  }
212 
213  $data_available = true;
214  }
215  }
216  break;
217  }
218 
219  if (file_exists($in_progress_file)) {
220  unlink($in_progress_file);
221  }
222 
223  if (!$data_available) {
224  ilUtil::sendInfo($this->lng->txt('dcl_no_export_content_available'));
225  return false;
226  }
227 
228  if (!$fields_available) {
229  global $ilCtrl;
230  ilUtil::sendInfo(sprintf(
231  $this->lng->txt('dcl_no_export_fields_available'),
232  $ilCtrl->getLinkTargetByClass(array('ilDclTableListGUI', 'ilDclTableEditGUI', 'ilDclFieldListGUI'), 'listFields')
233  ));
234  return false;
235  }
236 
237  if ($send) {
238  $adapter->sendToClient($filename);
239  exit;
240  } else {
241  $adapter->writeToFile($filepath);
242  }
243  }
244 
245 
255  public function exportAsync($format = self::EXPORT_EXCEL, $filepath = null)
256  {
257  global $DIC;
258  $ilLog = $DIC['ilLog'];
259 
260  $method = self::SOAP_FUNCTION_NAME;
261 
262  $soap_params = array($this->dcl->getRefId());
263  array_push($soap_params, $this->table_id, $format, $filepath);
264 
265  $new_session_id = ilSession::_duplicate($_COOKIE['PHPSESSID']);
266  $client_id = $_COOKIE['ilClientId'];
267 
268  // Start cloning process using soap call
269  include_once 'Services/WebServices/SOAP/classes/class.ilSoapClient.php';
270 
271  $soap_client = new ilSoapClient();
272  $soap_client->setResponseTimeout(5);
273  $soap_client->enableWSDL(true);
274 
275  $ilLog->write(__METHOD__ . ': Trying to call Soap client...');
276 
277  array_unshift($soap_params, $new_session_id . '::' . $client_id);
278 
279  if ($soap_client->init()) {
280  $ilLog->info('Calling soap ' . $method . ' method with params ' . print_r($soap_params, true));
281  $res = $soap_client->call($method, $soap_params);
282  } else {
283  $ilLog->warning('SOAP clone call failed. Calling clone method manually');
284  require_once('./webservice/soap/include/inc.soap_functions.php');
285  if (method_exists('ilSoapFunctions', $method)) {
286  $res = ilSoapFunctions::$method($new_session_id . '::' . $client_id, $this->dcl->getRefId(), $this->table_id, $format, $filepath);
287  } else {
288  throw new ilDclException("SOAP call " . $method . " does not exists!");
289  }
290  }
291 
292  return $res;
293  }
294 }
static makeDirParents($a_dir)
Create a new directory and all parent directories.
$_COOKIE['client_id']
Definition: server.php:9
if(isset($_REQUEST['delete'])) $list
Definition: registry.php:41
$worksheet
$format
Definition: metadata.php:141
global $DIC
Definition: saml.php:7
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.
foreach($_POST as $key=> $value) $res
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
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.
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...
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
$client_id
Class ilObjDataCollection.