| Conditions | 43 |
| Paths | > 20000 |
| Total Lines | 283 |
| Code Lines | 180 |
| 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 |
||
| 222 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 223 | { |
||
| 224 | // phpcs:enable |
||
| 225 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
| 226 | |||
| 227 | if (empty($srctemplatepath)) { |
||
| 228 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
| 229 | return -1; |
||
| 230 | } |
||
| 231 | |||
| 232 | // Add odtgeneration hook |
||
| 233 | if (!is_object($hookmanager)) { |
||
| 234 | include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; |
||
| 235 | $hookmanager = new HookManager($this->db); |
||
| 236 | } |
||
| 237 | $hookmanager->initHooks(array('odtgeneration')); |
||
| 238 | global $action; |
||
| 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 | // Load translation files required by the page |
||
| 247 | $outputlangs->loadLangs(array("main", "companies", "bills", "dict")); |
||
| 248 | |||
| 249 | if ($conf->user->dir_output) { |
||
| 250 | // If $object is id instead of object |
||
| 251 | if (!is_object($object)) { |
||
| 252 | $id = $object; |
||
| 253 | $object = new UserGroup($this->db); |
||
| 254 | $result = $object->fetch($id); |
||
| 255 | if ($result < 0) { |
||
| 256 | dol_print_error($this->db, $object->error); |
||
| 257 | return -1; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | $dir = $conf->usergroup->dir_output; |
||
| 262 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 263 | if (!preg_match('/specimen/i', $objectref)) { |
||
| 264 | $dir .= "/".$objectref; |
||
| 265 | } |
||
| 266 | $file = $dir."/".$objectref.".odt"; |
||
| 267 | |||
| 268 | if (!file_exists($dir)) { |
||
| 269 | if (dol_mkdir($dir) < 0) { |
||
| 270 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 271 | return -1; |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | if (file_exists($dir)) { |
||
| 276 | //print "srctemplatepath=".$srctemplatepath; // Src filename |
||
| 277 | $newfile = basename($srctemplatepath); |
||
| 278 | $newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile); |
||
| 279 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
| 280 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
| 281 | |||
| 282 | $newfiletmp = $objectref.'_'.$newfiletmp; |
||
| 283 | |||
| 284 | // Get extension (ods or odt) |
||
| 285 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1); |
||
| 286 | if (!empty($conf->global->MAIN_DOC_USE_TIMING)) { |
||
| 287 | $format = $conf->global->MAIN_DOC_USE_TIMING; |
||
| 288 | if ($format == '1') { |
||
| 289 | $format = '%Y%m%d%H%M%S'; |
||
| 290 | } |
||
| 291 | $filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat; |
||
| 292 | } else { |
||
| 293 | $filename = $newfiletmp.'.'.$newfileformat; |
||
| 294 | } |
||
| 295 | $file = $dir.'/'.$filename; |
||
| 296 | //print "newdir=".$dir; |
||
| 297 | //print "newfile=".$newfile; |
||
| 298 | //print "file=".$file; |
||
| 299 | //print "conf->user->dir_temp=".$conf->user->dir_temp; |
||
| 300 | |||
| 301 | dol_mkdir($conf->user->dir_temp); |
||
| 302 | if (!is_writable($conf->user->dir_temp)) { |
||
| 303 | $this->error = "Failed to write in temp directory ".$conf->user->dir_temp; |
||
| 304 | dol_syslog('Error in write_file: '.$this->error, LOG_ERR); |
||
| 305 | return -1; |
||
| 306 | } |
||
| 307 | |||
| 308 | // If CUSTOMER contact defined on user, we use it |
||
| 309 | $usecontact = false; |
||
| 310 | $arrayidcontact = $object->getIdContact('external', 'CUSTOMER'); |
||
| 311 | if (count($arrayidcontact) > 0) { |
||
| 312 | $usecontact = true; |
||
| 313 | $result = $object->fetch_contact($arrayidcontact[0]); |
||
| 314 | } |
||
| 315 | |||
| 316 | // Recipient name |
||
| 317 | if (!empty($usecontact)) { |
||
| 318 | 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)))) { |
||
| 319 | $socobject = $object->contact; |
||
| 320 | } else { |
||
| 321 | $socobject = $object->thirdparty; |
||
| 322 | // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use |
||
| 323 | $contactobject = $object->contact; |
||
| 324 | } |
||
| 325 | } else { |
||
| 326 | $socobject = $object->thirdparty; |
||
| 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 = 'user_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->user->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_WARNING); |
||
| 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_WARNING); |
||
| 377 | } |
||
| 378 | |||
| 379 | // Make substitutions into odt |
||
| 380 | $array_user = $this->get_substitutionarray_user($user, $outputlangs); |
||
| 381 | $array_global = $this->get_substitutionarray_each_var_object($object, $outputlangs); |
||
| 382 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
| 383 | $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs); |
||
| 384 | $array_objet = $this->get_substitutionarray_each_var_object($object, $outputlangs); |
||
| 385 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
| 386 | // retrieve contact information for use in object as contact_xxx tags |
||
| 387 | $array_thirdparty_contact = array(); |
||
| 388 | if ($usecontact && is_object($contactobject)) { |
||
| 389 | $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact'); |
||
| 390 | } |
||
| 391 | |||
| 392 | $tmparray = array_merge($array_global, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_thirdparty_contact); |
||
| 393 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
| 394 | $object->fetch_optionals(); |
||
| 395 | // Call the ODTSubstitution hook |
||
| 396 | $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray); |
||
| 397 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 398 | foreach ($tmparray as $key => $value) { |
||
| 399 | try { |
||
| 400 | if (preg_match('/logo$/', $key)) { // Image |
||
| 401 | if (file_exists($value)) { |
||
| 402 | $odfHandler->setImage($key, $value); |
||
| 403 | } else { |
||
| 404 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
| 405 | } |
||
| 406 | } else // Text |
||
| 407 | { |
||
| 408 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 409 | } |
||
| 410 | } catch (OdfException $e) { |
||
| 411 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 412 | } |
||
| 413 | } |
||
| 414 | // Replace tags of lines |
||
| 415 | try { |
||
| 416 | $foundtagforlines = 1; |
||
| 417 | try { |
||
| 418 | $listlines = $odfHandler->setSegment('lines'); |
||
| 419 | } catch (OdfException $e) { |
||
| 420 | // We may arrive here if tags for lines not present into template |
||
| 421 | $foundtagforlines = 0; |
||
| 422 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 423 | } |
||
| 424 | if ($foundtagforlines) { |
||
| 425 | foreach ($object->members as $u) { |
||
| 426 | $tmparray = $this->get_substitutionarray_each_var_object($u, $outputlangs); |
||
| 427 | unset($tmparray['object_pass']); |
||
| 428 | unset($tmparray['object_pass_indatabase']); |
||
| 429 | complete_substitutions_array($tmparray, $outputlangs, $object, $user, "completesubstitutionarray_users"); |
||
| 430 | // Call the ODTSubstitutionLine hook |
||
| 431 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray, 'line'=>$u); |
||
| 432 | $reshook = $hookmanager->executeHooks('ODTSubstitutionLine', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 433 | foreach ($tmparray as $key => $val) { |
||
| 434 | try { |
||
| 435 | if (!is_array($val)) { |
||
| 436 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
| 437 | } |
||
| 438 | } catch (OdfException $e) { |
||
| 439 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 440 | } catch (SegmentException $e) { |
||
| 441 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | $listlines->merge(); |
||
| 445 | } |
||
| 446 | $odfHandler->mergeSegment($listlines); |
||
| 447 | } |
||
| 448 | } catch (OdfException $e) { |
||
| 449 | $this->error = $e->getMessage(); |
||
| 450 | dol_syslog($this->error, LOG_WARNING); |
||
| 451 | return -1; |
||
| 452 | } |
||
| 453 | |||
| 454 | // Replace labels translated |
||
| 455 | $tmparray = $outputlangs->get_translations_for_substitutions(); |
||
| 456 | foreach ($tmparray as $key => $value) { |
||
| 457 | try { |
||
| 458 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 459 | } catch (OdfException $e) { |
||
| 460 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | // Call the beforeODTSave hook |
||
| 465 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs); |
||
| 466 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 467 | |||
| 468 | // Write new file |
||
| 469 | if (!empty($conf->global->MAIN_ODT_AS_PDF)) { |
||
| 470 | try { |
||
| 471 | $odfHandler->exportAsAttachedPDF($file); |
||
| 472 | } catch (Exception $e) { |
||
| 473 | $this->error = $e->getMessage(); |
||
| 474 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 475 | return -1; |
||
| 476 | } |
||
| 477 | } else { |
||
| 478 | try { |
||
| 479 | $odfHandler->saveToDisk($file); |
||
| 480 | } catch (Exception $e) { |
||
| 481 | $this->error = $e->getMessage(); |
||
| 482 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 483 | return -1; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | |||
| 487 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 488 | |||
| 489 | if (!empty($conf->global->MAIN_UMASK)) { |
||
| 490 | @chmod($file, octdec($conf->global->MAIN_UMASK)); |
||
| 491 | } |
||
| 492 | |||
| 493 | $odfHandler = null; // Destroy object |
||
| 494 | |||
| 495 | $this->result = array('fullpath'=>$file); |
||
| 496 | |||
| 497 | return 1; // Success |
||
| 498 | } else { |
||
| 499 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 500 | return -1; |
||
| 501 | } |
||
| 502 | } |
||
| 503 | |||
| 504 | return -1; |
||
| 505 | } |
||
| 507 |