Conditions | 35 |
Paths | > 20000 |
Total Lines | 246 |
Code Lines | 156 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
213 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
214 | { |
||
215 | // phpcs:enable |
||
216 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
217 | global $action; |
||
218 | |||
219 | if (empty($srctemplatepath)) { |
||
220 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
221 | return -1; |
||
222 | } |
||
223 | |||
224 | // Add odtgeneration hook |
||
225 | if (!is_object($hookmanager)) { |
||
226 | $hookmanager = new HookManager($this->db); |
||
227 | } |
||
228 | $hookmanager->initHooks(array('odtgeneration')); |
||
229 | |||
230 | if (!is_object($outputlangs)) { |
||
231 | $outputlangs = $langs; |
||
232 | } |
||
233 | $sav_charset_output = $outputlangs->charset_output; |
||
234 | $outputlangs->charset_output = 'UTF-8'; |
||
235 | |||
236 | // Load translation files required by the page |
||
237 | $outputlangs->loadLangs(array("main", "dict", "companies", "projects")); |
||
238 | |||
239 | if ($conf->societe->multidir_output[$object->entity]) { |
||
240 | $dir = $conf->societe->multidir_output[$object->entity]; |
||
241 | $objectref = dol_sanitizeFileName($object->id); |
||
242 | if (!preg_match('/specimen/i', $objectref)) { |
||
243 | $dir .= "/" . $objectref; |
||
244 | } |
||
245 | |||
246 | if (!file_exists($dir)) { |
||
247 | if (dol_mkdir($dir) < 0) { |
||
248 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
249 | return -1; |
||
250 | } |
||
251 | } |
||
252 | |||
253 | if (file_exists($dir)) { |
||
254 | //print "srctemplatepath=".$srctemplatepath; // Src filename |
||
255 | $newfile = basename($srctemplatepath); |
||
256 | $newfiletmp = preg_replace('/\.od(s|t)/i', '', $newfile); |
||
257 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
258 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
259 | // Get extension (ods or odt) |
||
260 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1); |
||
261 | if (getDolGlobalString('MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME')) { |
||
262 | $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)) . '-' . $newfiletmp; |
||
263 | $newfiletmp = preg_replace('/__+/', '_', $newfiletmp); // Replace repeated _ into one _ (to avoid string with substitution syntax) |
||
264 | } |
||
265 | if (getDolGlobalString('MAIN_DOC_USE_TIMING')) { |
||
266 | $format = getDolGlobalString('MAIN_DOC_USE_TIMING'); |
||
267 | if ($format == '1') { |
||
268 | $format = '%Y%m%d%H%M%S'; |
||
269 | } |
||
270 | $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat; |
||
271 | } else { |
||
272 | $filename = $newfiletmp . '.' . $newfileformat; |
||
273 | } |
||
274 | $file = $dir . '/' . $filename; |
||
275 | $object->builddoc_filename = $filename; // For triggers |
||
|
|||
276 | $object->context['builddoc_filename'] = $filename; // For triggers |
||
277 | //print "newfileformat=".$newfileformat; |
||
278 | //print "newdir=".$dir; |
||
279 | //print "newfile=".$newfile; |
||
280 | //print "file=".$file; |
||
281 | //print "conf->societe->dir_temp=".$conf->societe->dir_temp; |
||
282 | //exit; |
||
283 | |||
284 | dol_mkdir($conf->societe->multidir_temp[$object->entity]); |
||
285 | if (!is_writable($conf->societe->multidir_temp[$object->entity])) { |
||
286 | $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->societe->multidir_temp[$object->entity]); |
||
287 | dol_syslog('Error in write_file: ' . $this->error, LOG_ERR); |
||
288 | return -1; |
||
289 | } |
||
290 | |||
291 | // Open and load template |
||
292 | require_once ODTPHP_PATH . 'odf.php'; |
||
293 | try { |
||
294 | $odfHandler = new Odf( |
||
295 | $srctemplatepath, |
||
296 | array( |
||
297 | 'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity], |
||
298 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy. |
||
299 | 'DELIMITER_LEFT' => '{', |
||
300 | 'DELIMITER_RIGHT' => '}' |
||
301 | ) |
||
302 | ); |
||
303 | } catch (Exception $e) { |
||
304 | $this->error = $e->getMessage(); |
||
305 | dol_syslog($e->getMessage(), LOG_INFO); |
||
306 | return -1; |
||
307 | } |
||
308 | //print $odfHandler->__toString()."\n"; |
||
309 | |||
310 | // Replace tags of lines for contacts |
||
311 | $contact_arrray = array(); |
||
312 | |||
313 | $sql = "SELECT p.rowid"; |
||
314 | $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as p"; |
||
315 | $sql .= " WHERE p.fk_soc = " . ((int) $object->id); |
||
316 | |||
317 | $result = $this->db->query($sql); |
||
318 | $num = $this->db->num_rows($result); |
||
319 | |||
320 | if ($num) { |
||
321 | $i = 0; |
||
322 | $contactstatic = new Contact($this->db); |
||
323 | |||
324 | while ($i < $num) { |
||
325 | $obj = $this->db->fetch_object($result); |
||
326 | |||
327 | $contact_arrray[$i] = $obj->rowid; |
||
328 | $i++; |
||
329 | } |
||
330 | } |
||
331 | if ((is_array($contact_arrray) && count($contact_arrray) > 0)) { |
||
332 | $foundtagforlines = 1; |
||
333 | try { |
||
334 | $listlines = $odfHandler->setSegment('companycontacts'); |
||
335 | } catch (OdfExceptionSegmentNotFound $e) { |
||
336 | // We may arrive here if tags for lines not present into template |
||
337 | $foundtagforlines = 0; |
||
338 | dol_syslog($e->getMessage(), LOG_INFO); |
||
339 | } |
||
340 | if ($foundtagforlines) { |
||
341 | foreach ($contact_arrray as $array_key => $contact_id) { |
||
342 | $res_contact = $contactstatic->fetch($contact_id); |
||
343 | if ((int) $res_contact > 0) { |
||
344 | $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact'); |
||
345 | foreach ($tmparray as $key => $val) { |
||
346 | try { |
||
347 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
348 | } catch (SegmentException $e) { |
||
349 | dol_syslog($e->getMessage(), LOG_INFO); |
||
350 | } |
||
351 | } |
||
352 | $listlines->merge(); |
||
353 | } else { |
||
354 | $this->error = $contactstatic->error; |
||
355 | dol_syslog($this->error, LOG_WARNING); |
||
356 | } |
||
357 | } |
||
358 | try { |
||
359 | $odfHandler->mergeSegment($listlines); |
||
360 | } catch (OdfException $e) { |
||
361 | $this->error = $e->getMessage(); |
||
362 | dol_syslog($this->error, LOG_WARNING); |
||
363 | //return -1; |
||
364 | } |
||
365 | } |
||
366 | } |
||
367 | |||
368 | // Make substitutions into odt |
||
369 | $array_user = $this->get_substitutionarray_user($user, $outputlangs); |
||
370 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
371 | $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs); |
||
372 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
373 | |||
374 | $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other); |
||
375 | |||
376 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
377 | |||
378 | // Call the ODTSubstitution hook |
||
379 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
380 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
381 | |||
382 | // Replace variables into document |
||
383 | foreach ($tmparray as $key => $value) { |
||
384 | try { |
||
385 | if (preg_match('/logo$/', $key)) { // Image |
||
386 | if (file_exists($value)) { |
||
387 | $odfHandler->setImage($key, $value); |
||
388 | } else { |
||
389 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
390 | } |
||
391 | } else { // Text |
||
392 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
393 | } |
||
394 | } catch (OdfException $e) { |
||
395 | // setVars failed, probably because key not found |
||
396 | dol_syslog($e->getMessage(), LOG_INFO); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | // Replace labels translated |
||
401 | $tmparray = $outputlangs->get_translations_for_substitutions(); |
||
402 | foreach ($tmparray as $key => $value) { |
||
403 | try { |
||
404 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
405 | } catch (OdfException $e) { |
||
406 | dol_syslog($e->getMessage(), LOG_INFO); |
||
407 | } |
||
408 | } |
||
409 | |||
410 | // Call the beforeODTSave hook |
||
411 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
412 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
413 | |||
414 | // Write new file |
||
415 | if (getDolGlobalString('MAIN_ODT_AS_PDF')) { |
||
416 | try { |
||
417 | $odfHandler->exportAsAttachedPDF($file); |
||
418 | } catch (Exception $e) { |
||
419 | $this->error = $e->getMessage(); |
||
420 | dol_syslog($e->getMessage(), LOG_INFO); |
||
421 | return -1; |
||
422 | } |
||
423 | } else { |
||
424 | try { |
||
425 | $odfHandler->creator = $user->getFullName($outputlangs); |
||
426 | $odfHandler->title = $object->builddoc_filename; |
||
427 | $odfHandler->subject = $object->builddoc_filename; |
||
428 | |||
429 | if (getDolGlobalString('ODT_ADD_DOLIBARR_ID')) { |
||
430 | $odfHandler->userdefined['dol_id'] = $object->id; |
||
431 | $odfHandler->userdefined['dol_element'] = $object->element; |
||
432 | } |
||
433 | |||
434 | $odfHandler->saveToDisk($file); |
||
435 | } catch (Exception $e) { |
||
436 | $this->error = $e->getMessage(); |
||
437 | dol_syslog($e->getMessage(), LOG_INFO); |
||
438 | return -1; |
||
439 | } |
||
440 | } |
||
441 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
442 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
443 | |||
444 | dolChmod($file); |
||
445 | |||
446 | $odfHandler = null; // Destroy object |
||
447 | |||
448 | $this->result = array('fullpath' => $file); |
||
449 | |||
450 | return 1; // Success |
||
451 | } else { |
||
452 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
453 | return -1; |
||
454 | } |
||
455 | } |
||
456 | |||
457 | $this->error = 'UnknownError'; |
||
458 | return -1; |
||
459 | } |
||
461 |