Conditions | 39 |
Paths | > 20000 |
Total Lines | 281 |
Code Lines | 177 |
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 |
||
226 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
227 | { |
||
228 | // phpcs:enable |
||
229 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
230 | global $action; |
||
231 | |||
232 | if (empty($srctemplatepath)) { |
||
233 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
234 | return -1; |
||
235 | } |
||
236 | |||
237 | // Add odtgeneration hook |
||
238 | $hookmanager->initHooks(array('odtgeneration')); |
||
239 | |||
240 | if (!is_object($outputlangs)) { |
||
241 | $outputlangs = $langs; |
||
242 | } |
||
243 | $sav_charset_output = $outputlangs->charset_output; |
||
244 | $outputlangs->charset_output = 'UTF-8'; |
||
245 | |||
246 | $outputlangs->loadLangs(array("main", "dict", "companies", "bills")); |
||
247 | |||
248 | if ($conf->recruitment->dir_output) { |
||
249 | // If $object is id instead of object |
||
250 | if (!is_object($object)) { |
||
251 | $id = $object; |
||
252 | $object = new RecruitmentJobPosition($this->db); |
||
253 | $result = $object->fetch($id); |
||
254 | if ($result < 0) { |
||
255 | dol_print_error($this->db, $object->error); |
||
256 | return -1; |
||
257 | } |
||
258 | } |
||
259 | |||
260 | $dir = $conf->recruitment->multidir_output[isset($object->entity) ? $object->entity : 1] . '/recruitmentjobposition/'; |
||
261 | $objectref = dol_sanitizeFileName($object->ref); |
||
262 | if (!preg_match('/specimen/i', $objectref)) { |
||
263 | $dir .= "/" . $objectref; |
||
264 | } |
||
265 | $file = $dir . "/" . $objectref . ".odt"; |
||
266 | |||
267 | if (!file_exists($dir)) { |
||
268 | if (dol_mkdir($dir) < 0) { |
||
269 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
270 | return -1; |
||
271 | } |
||
272 | } |
||
273 | |||
274 | if (file_exists($dir)) { |
||
275 | //print "srctemplatepath=".$srctemplatepath; // Src filename |
||
276 | $newfile = basename($srctemplatepath); |
||
277 | $newfiletmp = preg_replace('/\.od[ts]/i', '', $newfile); |
||
278 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
279 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
280 | $newfiletmp = $objectref . '_' . $newfiletmp; |
||
281 | //$file=$dir.'/'.$newfiletmp.'.'.dol_print_date(dol_now(),'%Y%m%d%H%M%S').'.odt'; |
||
282 | // Get extension (ods or odt) |
||
283 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1); |
||
284 | if (getDolGlobalString('MAIN_DOC_USE_TIMING')) { |
||
285 | $format = getDolGlobalString('MAIN_DOC_USE_TIMING'); |
||
286 | if ($format == '1') { |
||
287 | $format = '%Y%m%d%H%M%S'; |
||
288 | } |
||
289 | $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat; |
||
290 | } else { |
||
291 | $filename = $newfiletmp . '.' . $newfileformat; |
||
292 | } |
||
293 | $file = $dir . '/' . $filename; |
||
294 | //print "newdir=".$dir; |
||
295 | //print "newfile=".$newfile; |
||
296 | //print "file=".$file; |
||
297 | //print "conf->societe->dir_temp=".$conf->societe->dir_temp; |
||
298 | |||
299 | dol_mkdir($conf->recruitment->dir_temp); |
||
300 | if (!is_writable($conf->recruitment->dir_temp)) { |
||
301 | $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->recruitment->dir_temp); |
||
302 | dol_syslog('Error in write_file: ' . $this->error, LOG_ERR); |
||
303 | return -1; |
||
304 | } |
||
305 | |||
306 | // If CUSTOMER contact defined on order, we use it |
||
307 | $usecontact = false; |
||
308 | $arrayidcontact = $object->getIdContact('external', 'CUSTOMER'); |
||
309 | if (count($arrayidcontact) > 0) { |
||
310 | $usecontact = true; |
||
311 | $result = $object->fetch_contact($arrayidcontact[0]); |
||
312 | } |
||
313 | |||
314 | // Recipient name |
||
315 | $contactobject = null; |
||
316 | if (!empty($usecontact)) { |
||
317 | // We can use the company of contact instead of thirdparty company |
||
318 | if ($object->contact->socid != $object->thirdparty->id && (!isset($conf->global->MAIN_USE_COMPANY_NAME_OF_CONTACT) || getDolGlobalString('MAIN_USE_COMPANY_NAME_OF_CONTACT'))) { |
||
319 | $object->contact->fetch_thirdparty(); |
||
320 | $socobject = $object->contact->thirdparty; |
||
321 | $contactobject = $object->contact; |
||
322 | } else { |
||
323 | $socobject = $object->thirdparty; |
||
324 | // if we have a CUSTOMER contact and we don't use it as thirdparty recipient we store the contact object for later use |
||
325 | $contactobject = $object->contact; |
||
326 | } |
||
327 | } else { |
||
328 | $socobject = $object->thirdparty; |
||
329 | } |
||
330 | |||
331 | // Make substitution |
||
332 | $substitutionarray = array( |
||
333 | '__FROM_NAME__' => $this->emetteur->name, |
||
334 | '__FROM_EMAIL__' => $this->emetteur->email, |
||
335 | '__TOTAL_TTC__' => $object->total_ttc, |
||
336 | '__TOTAL_HT__' => $object->total_ht, |
||
337 | '__TOTAL_VAT__' => $object->total_tva |
||
338 | ); |
||
339 | complete_substitutions_array($substitutionarray, $langs, $object); |
||
340 | // Call the ODTSubstitution hook |
||
341 | $parameters = array('file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$substitutionarray); |
||
342 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
343 | |||
344 | // Line of free text |
||
345 | $newfreetext = ''; |
||
346 | $paramfreetext = 'ORDER_FREE_TEXT'; |
||
347 | if (getDolGlobalString($paramfreetext)) { |
||
348 | $newfreetext = make_substitutions(getDolGlobalString($paramfreetext), $substitutionarray); |
||
349 | } |
||
350 | |||
351 | // Open and load template |
||
352 | require_once ODTPHP_PATH . 'odf.php'; |
||
353 | try { |
||
354 | $odfHandler = new Odf( |
||
355 | $srctemplatepath, |
||
356 | array( |
||
357 | 'PATH_TO_TMP' => $conf->recruitment->dir_temp, |
||
358 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy. |
||
359 | 'DELIMITER_LEFT' => '{', |
||
360 | 'DELIMITER_RIGHT' => '}' |
||
361 | ) |
||
362 | ); |
||
363 | } catch (Exception $e) { |
||
364 | $this->error = $e->getMessage(); |
||
365 | dol_syslog($e->getMessage(), LOG_INFO); |
||
366 | return -1; |
||
367 | } |
||
368 | // After construction $odfHandler->contentXml contains content and |
||
369 | // [!-- BEGIN row.lines --]*[!-- END row.lines --] has been replaced by |
||
370 | // [!-- BEGIN lines --]*[!-- END lines --] |
||
371 | //print html_entity_decode($odfHandler->__toString()); |
||
372 | //print exit; |
||
373 | |||
374 | |||
375 | // Make substitutions into odt of freetext |
||
376 | try { |
||
377 | $odfHandler->setVars('free_text', $newfreetext, true, 'UTF-8'); |
||
378 | } catch (OdfException $e) { |
||
379 | dol_syslog($e->getMessage(), LOG_INFO); |
||
380 | } |
||
381 | |||
382 | // Define substitution array |
||
383 | $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, null, $object); |
||
384 | $array_object_from_properties = $this->get_substitutionarray_each_var_object($object, $outputlangs); |
||
385 | $array_objet = $this->get_substitutionarray_object($object, $outputlangs); |
||
386 | $array_user = $this->get_substitutionarray_user($user, $outputlangs); |
||
387 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
388 | $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs); |
||
389 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
390 | // retrieve contact information for use in object as contact_xxx tags |
||
391 | $array_thirdparty_contact = array(); |
||
392 | if ($usecontact && is_object($contactobject)) { |
||
393 | $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact'); |
||
394 | } |
||
395 | |||
396 | $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact); |
||
397 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
398 | |||
399 | // Call the ODTSubstitution hook |
||
400 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
401 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
402 | |||
403 | foreach ($tmparray as $key => $value) { |
||
404 | try { |
||
405 | if (preg_match('/logo$/', $key)) { |
||
406 | // Image |
||
407 | if (file_exists($value)) { |
||
408 | $odfHandler->setImage($key, $value); |
||
409 | } else { |
||
410 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
411 | } |
||
412 | } else { |
||
413 | // Text |
||
414 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
415 | } |
||
416 | } catch (OdfException $e) { |
||
417 | dol_syslog($e->getMessage(), LOG_INFO); |
||
418 | } |
||
419 | } |
||
420 | // Replace tags of lines |
||
421 | $foundtagforlines = 1; |
||
422 | try { |
||
423 | $listlines = $odfHandler->setSegment('lines'); |
||
424 | } catch (OdfExceptionSegmentNotFound $e) { |
||
425 | // We may arrive here if tags for lines not present into template |
||
426 | $foundtagforlines = 0; |
||
427 | dol_syslog($e->getMessage(), LOG_INFO); |
||
428 | } |
||
429 | if ($foundtagforlines) { |
||
430 | $linenumber = 0; |
||
431 | foreach ($object->lines as $line) { |
||
432 | $linenumber++; |
||
433 | $tmparray = $this->get_substitutionarray_lines($line, $outputlangs, $linenumber); |
||
434 | complete_substitutions_array($tmparray, $outputlangs, $object, $line, "completesubstitutionarray_lines"); |
||
435 | // Call the ODTSubstitutionLine hook |
||
436 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray, 'line' => $line); |
||
437 | $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
438 | foreach ($tmparray as $key => $val) { |
||
439 | try { |
||
440 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
441 | } catch (SegmentException $e) { |
||
442 | dol_syslog($e->getMessage(), LOG_INFO); |
||
443 | } |
||
444 | } |
||
445 | $listlines->merge(); |
||
446 | } |
||
447 | try { |
||
448 | $odfHandler->mergeSegment($listlines); |
||
449 | } catch (OdfException $e) { |
||
450 | $this->error = $e->getMessage(); |
||
451 | dol_syslog($this->error, LOG_WARNING); |
||
452 | return -1; |
||
453 | } |
||
454 | } |
||
455 | |||
456 | // Replace labels translated |
||
457 | $tmparray = $outputlangs->get_translations_for_substitutions(); |
||
458 | foreach ($tmparray as $key => $value) { |
||
459 | try { |
||
460 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
461 | } catch (OdfException $e) { |
||
462 | dol_syslog($e->getMessage(), LOG_INFO); |
||
463 | } |
||
464 | } |
||
465 | |||
466 | // Call the beforeODTSave hook |
||
467 | |||
468 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
469 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
470 | |||
471 | // Write new file |
||
472 | if (getDolGlobalString('MAIN_ODT_AS_PDF')) { |
||
473 | try { |
||
474 | $odfHandler->exportAsAttachedPDF($file); |
||
475 | } catch (Exception $e) { |
||
476 | $this->error = $e->getMessage(); |
||
477 | dol_syslog($e->getMessage(), LOG_INFO); |
||
478 | return -1; |
||
479 | } |
||
480 | } else { |
||
481 | try { |
||
482 | $odfHandler->saveToDisk($file); |
||
483 | } catch (Exception $e) { |
||
484 | $this->error = $e->getMessage(); |
||
485 | dol_syslog($e->getMessage(), LOG_INFO); |
||
486 | return -1; |
||
487 | } |
||
488 | } |
||
489 | |||
490 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
491 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
492 | |||
493 | dolChmod($file); |
||
494 | |||
495 | $odfHandler = null; // Destroy object |
||
496 | |||
497 | $this->result = array('fullpath' => $file); |
||
498 | |||
499 | return 1; // Success |
||
500 | } else { |
||
501 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
502 | return -1; |
||
503 | } |
||
504 | } |
||
505 | |||
506 | return -1; |
||
507 | } |
||
509 |