| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 246 |
| Code Lines | 156 |
| 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 |
||
| 215 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 216 | { |
||
| 217 | // phpcs:enable |
||
| 218 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
| 219 | global $action; |
||
| 220 | |||
| 221 | if (empty($srctemplatepath)) { |
||
| 222 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
| 223 | return -1; |
||
| 224 | } |
||
| 225 | |||
| 226 | // Add odtgeneration hook |
||
| 227 | if (!is_object($hookmanager)) { |
||
| 228 | $hookmanager = new HookManager($this->db); |
||
| 229 | } |
||
| 230 | $hookmanager->initHooks(array('odtgeneration')); |
||
| 231 | |||
| 232 | if (!is_object($outputlangs)) { |
||
| 233 | $outputlangs = $langs; |
||
| 234 | } |
||
| 235 | $sav_charset_output = $outputlangs->charset_output; |
||
| 236 | $outputlangs->charset_output = 'UTF-8'; |
||
| 237 | |||
| 238 | // Load translation files required by the page |
||
| 239 | $outputlangs->loadLangs(array("main", "dict", "companies", "projects")); |
||
| 240 | |||
| 241 | if ($conf->societe->multidir_output[$object->entity]) { |
||
| 242 | $dir = $conf->societe->multidir_output[$object->entity]; |
||
| 243 | $objectref = dol_sanitizeFileName($object->id); |
||
| 244 | if (!preg_match('/specimen/i', $objectref)) { |
||
| 245 | $dir .= "/" . $objectref; |
||
| 246 | } |
||
| 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(s|t)/i', '', $newfile); |
||
| 259 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
| 260 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
| 261 | // Get extension (ods or odt) |
||
| 262 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1); |
||
| 263 | if (getDolGlobalString('MAIN_DOC_USE_OBJECT_THIRDPARTY_NAME')) { |
||
| 264 | $newfiletmp = dol_sanitizeFileName(dol_string_nospecial($object->name)) . '-' . $newfiletmp; |
||
| 265 | $newfiletmp = preg_replace('/__+/', '_', $newfiletmp); // Replace repeated _ into one _ (to avoid string with substitution syntax) |
||
| 266 | } |
||
| 267 | if (getDolGlobalString('MAIN_DOC_USE_TIMING')) { |
||
| 268 | $format = getDolGlobalString('MAIN_DOC_USE_TIMING'); |
||
| 269 | if ($format == '1') { |
||
| 270 | $format = '%Y%m%d%H%M%S'; |
||
| 271 | } |
||
| 272 | $filename = $newfiletmp . '-' . dol_print_date(dol_now(), $format) . '.' . $newfileformat; |
||
| 273 | } else { |
||
| 274 | $filename = $newfiletmp . '.' . $newfileformat; |
||
| 275 | } |
||
| 276 | $file = $dir . '/' . $filename; |
||
| 277 | $object->builddoc_filename = $filename; // For triggers |
||
|
|
|||
| 278 | $object->context['builddoc_filename'] = $filename; // For triggers |
||
| 279 | //print "newfileformat=".$newfileformat; |
||
| 280 | //print "newdir=".$dir; |
||
| 281 | //print "newfile=".$newfile; |
||
| 282 | //print "file=".$file; |
||
| 283 | //print "conf->societe->dir_temp=".$conf->societe->dir_temp; |
||
| 284 | //exit; |
||
| 285 | |||
| 286 | dol_mkdir($conf->societe->multidir_temp[$object->entity]); |
||
| 287 | if (!is_writable($conf->societe->multidir_temp[$object->entity])) { |
||
| 288 | $this->error = $langs->transnoentities("ErrorFailedToWriteInTempDirectory", $conf->societe->multidir_temp[$object->entity]); |
||
| 289 | dol_syslog('Error in write_file: ' . $this->error, LOG_ERR); |
||
| 290 | return -1; |
||
| 291 | } |
||
| 292 | |||
| 293 | // Open and load template |
||
| 294 | require_once ODTPHP_PATH . 'odf.php'; |
||
| 295 | try { |
||
| 296 | $odfHandler = new Odf( |
||
| 297 | $srctemplatepath, |
||
| 298 | array( |
||
| 299 | 'PATH_TO_TMP' => $conf->societe->multidir_temp[$object->entity], |
||
| 300 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy. |
||
| 301 | 'DELIMITER_LEFT' => '{', |
||
| 302 | 'DELIMITER_RIGHT' => '}' |
||
| 303 | ) |
||
| 304 | ); |
||
| 305 | } catch (Exception $e) { |
||
| 306 | $this->error = $e->getMessage(); |
||
| 307 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 308 | return -1; |
||
| 309 | } |
||
| 310 | //print $odfHandler->__toString()."\n"; |
||
| 311 | |||
| 312 | // Replace tags of lines for contacts |
||
| 313 | $contact_arrray = array(); |
||
| 314 | |||
| 315 | $sql = "SELECT p.rowid"; |
||
| 316 | $sql .= " FROM " . MAIN_DB_PREFIX . "socpeople as p"; |
||
| 317 | $sql .= " WHERE p.fk_soc = " . ((int)$object->id); |
||
| 318 | |||
| 319 | $result = $this->db->query($sql); |
||
| 320 | $num = $this->db->num_rows($result); |
||
| 321 | |||
| 322 | if ($num) { |
||
| 323 | $i = 0; |
||
| 324 | $contactstatic = new Contact($this->db); |
||
| 325 | |||
| 326 | while ($i < $num) { |
||
| 327 | $obj = $this->db->fetch_object($result); |
||
| 328 | |||
| 329 | $contact_arrray[$i] = $obj->rowid; |
||
| 330 | $i++; |
||
| 331 | } |
||
| 332 | } |
||
| 333 | if ((is_array($contact_arrray) && count($contact_arrray) > 0)) { |
||
| 334 | $foundtagforlines = 1; |
||
| 335 | try { |
||
| 336 | $listlines = $odfHandler->setSegment('companycontacts'); |
||
| 337 | } catch (OdfExceptionSegmentNotFound $e) { |
||
| 338 | // We may arrive here if tags for lines not present into template |
||
| 339 | $foundtagforlines = 0; |
||
| 340 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 341 | } |
||
| 342 | if ($foundtagforlines) { |
||
| 343 | foreach ($contact_arrray as $array_key => $contact_id) { |
||
| 344 | $res_contact = $contactstatic->fetch($contact_id); |
||
| 345 | if ((int)$res_contact > 0) { |
||
| 346 | $tmparray = $this->get_substitutionarray_contact($contactstatic, $outputlangs, 'contact'); |
||
| 347 | foreach ($tmparray as $key => $val) { |
||
| 348 | try { |
||
| 349 | $listlines->setVars($key, $val, true, 'UTF-8'); |
||
| 350 | } catch (SegmentException $e) { |
||
| 351 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | $listlines->merge(); |
||
| 355 | } else { |
||
| 356 | $this->error = $contactstatic->error; |
||
| 357 | dol_syslog($this->error, LOG_WARNING); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | try { |
||
| 361 | $odfHandler->mergeSegment($listlines); |
||
| 362 | } catch (OdfException $e) { |
||
| 363 | $this->error = $e->getMessage(); |
||
| 364 | dol_syslog($this->error, LOG_WARNING); |
||
| 365 | //return -1; |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | // Make substitutions into odt |
||
| 371 | $array_user = $this->get_substitutionarray_user($user, $outputlangs); |
||
| 372 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
| 373 | $array_thirdparty = $this->get_substitutionarray_thirdparty($object, $outputlangs); |
||
| 374 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
| 375 | |||
| 376 | $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other); |
||
| 377 | |||
| 378 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
| 379 | |||
| 380 | // Call the ODTSubstitution hook |
||
| 381 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
| 382 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 383 | |||
| 384 | // Replace variables into document |
||
| 385 | foreach ($tmparray as $key => $value) { |
||
| 386 | try { |
||
| 387 | if (preg_match('/logo$/', $key)) { // Image |
||
| 388 | if (file_exists($value)) { |
||
| 389 | $odfHandler->setImage($key, $value); |
||
| 390 | } else { |
||
| 391 | $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
| 392 | } |
||
| 393 | } else { // Text |
||
| 394 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 395 | } |
||
| 396 | } catch (OdfException $e) { |
||
| 397 | // setVars failed, probably because key not found |
||
| 398 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | // Replace labels translated |
||
| 403 | $tmparray = $outputlangs->get_translations_for_substitutions(); |
||
| 404 | foreach ($tmparray as $key => $value) { |
||
| 405 | try { |
||
| 406 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 407 | } catch (OdfException $e) { |
||
| 408 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | // Call the beforeODTSave hook |
||
| 413 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
| 414 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 415 | |||
| 416 | // Write new file |
||
| 417 | if (getDolGlobalString('MAIN_ODT_AS_PDF')) { |
||
| 418 | try { |
||
| 419 | $odfHandler->exportAsAttachedPDF($file); |
||
| 420 | } catch (Exception $e) { |
||
| 421 | $this->error = $e->getMessage(); |
||
| 422 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 423 | return -1; |
||
| 424 | } |
||
| 425 | } else { |
||
| 426 | try { |
||
| 427 | $odfHandler->creator = $user->getFullName($outputlangs); |
||
| 428 | $odfHandler->title = $object->builddoc_filename; |
||
| 429 | $odfHandler->subject = $object->builddoc_filename; |
||
| 430 | |||
| 431 | if (getDolGlobalString('ODT_ADD_DOLIBARR_ID')) { |
||
| 432 | $odfHandler->userdefined['dol_id'] = $object->id; |
||
| 433 | $odfHandler->userdefined['dol_element'] = $object->element; |
||
| 434 | } |
||
| 435 | |||
| 436 | $odfHandler->saveToDisk($file); |
||
| 437 | } catch (Exception $e) { |
||
| 438 | $this->error = $e->getMessage(); |
||
| 439 | dol_syslog($e->getMessage(), LOG_INFO); |
||
| 440 | return -1; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | $parameters = array('odfHandler' => &$odfHandler, 'file' => $file, 'object' => $object, 'outputlangs' => $outputlangs, 'substitutionarray' => &$tmparray); |
||
| 444 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 445 | |||
| 446 | dolChmod($file); |
||
| 447 | |||
| 448 | $odfHandler = null; // Destroy object |
||
| 449 | |||
| 450 | $this->result = array('fullpath' => $file); |
||
| 451 | |||
| 452 | return 1; // Success |
||
| 453 | } else { |
||
| 454 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 455 | return -1; |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | $this->error = 'UnknownError'; |
||
| 460 | return -1; |
||
| 461 | } |
||
| 463 |