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