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