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