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