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