| Conditions | 32 |
| Paths | > 20000 |
| Total Lines | 213 |
| Code Lines | 123 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 194 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 195 | { |
||
| 196 | // phpcs:enable |
||
| 197 | global $user, $langs, $conf, $mysoc, $hookmanager; |
||
| 198 | |||
| 199 | if (empty($srctemplatepath)) |
||
| 200 | { |
||
| 201 | dol_syslog("doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING); |
||
| 202 | return -1; |
||
| 203 | } |
||
| 204 | |||
| 205 | // Add odtgeneration hook |
||
| 206 | if (!is_object($hookmanager)) |
||
| 207 | { |
||
| 208 | include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php'; |
||
| 209 | $hookmanager = new HookManager($this->db); |
||
| 210 | } |
||
| 211 | $hookmanager->initHooks(array('odtgeneration')); |
||
| 212 | global $action; |
||
| 213 | |||
| 214 | if (!is_object($outputlangs)) $outputlangs = $langs; |
||
| 215 | $sav_charset_output = $outputlangs->charset_output; |
||
| 216 | $outputlangs->charset_output = 'UTF-8'; |
||
| 217 | |||
| 218 | // Load translation files required by the page |
||
| 219 | $outputlangs->loadLangs(array("main", "companies", "bills", "dict")); |
||
| 220 | |||
| 221 | if ($conf->user->dir_output) |
||
| 222 | { |
||
| 223 | // If $object is id instead of object |
||
| 224 | if (!is_object($object)) |
||
| 225 | { |
||
| 226 | $id = $object; |
||
| 227 | $object = new User($this->db); |
||
| 228 | $result = $object->fetch($id); |
||
| 229 | if ($result < 0) |
||
| 230 | { |
||
| 231 | dol_print_error($this->db, $object->error); |
||
| 232 | return -1; |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | $object->fetch_thirdparty(); |
||
| 237 | |||
| 238 | $dir = $conf->user->dir_output; |
||
| 239 | $objectref = dol_sanitizeFileName($object->ref); |
||
| 240 | if (!preg_match('/specimen/i', $objectref)) $dir .= "/".$objectref; |
||
| 241 | $file = $dir."/".$objectref.".odt"; |
||
| 242 | |||
| 243 | if (!file_exists($dir)) |
||
| 244 | { |
||
| 245 | if (dol_mkdir($dir) < 0) |
||
| 246 | { |
||
| 247 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 248 | return -1; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | if (file_exists($dir)) |
||
| 253 | { |
||
| 254 | //print "srctemplatepath=".$srctemplatepath; // Src filename |
||
| 255 | $newfile = basename($srctemplatepath); |
||
| 256 | $newfiletmp = preg_replace('/\.od(t|s)/i', '', $newfile); |
||
| 257 | $newfiletmp = preg_replace('/template_/i', '', $newfiletmp); |
||
| 258 | $newfiletmp = preg_replace('/modele_/i', '', $newfiletmp); |
||
| 259 | |||
| 260 | $newfiletmp = $objectref.'_'.$newfiletmp; |
||
| 261 | |||
| 262 | // Get extension (ods or odt) |
||
| 263 | $newfileformat = substr($newfile, strrpos($newfile, '.') + 1); |
||
| 264 | if (!empty($conf->global->MAIN_DOC_USE_TIMING)) |
||
| 265 | { |
||
| 266 | $format = $conf->global->MAIN_DOC_USE_TIMING; |
||
| 267 | if ($format == '1') $format = '%Y%m%d%H%M%S'; |
||
| 268 | $filename = $newfiletmp.'-'.dol_print_date(dol_now(), $format).'.'.$newfileformat; |
||
| 269 | } else { |
||
| 270 | $filename = $newfiletmp.'.'.$newfileformat; |
||
| 271 | } |
||
| 272 | $file = $dir.'/'.$filename; |
||
| 273 | //print "newdir=".$dir; |
||
| 274 | //print "newfile=".$newfile; |
||
| 275 | //print "file=".$file; |
||
| 276 | //print "conf->user->dir_temp=".$conf->user->dir_temp; |
||
| 277 | |||
| 278 | dol_mkdir($conf->user->dir_temp); |
||
| 279 | |||
| 280 | |||
| 281 | // If CUSTOMER contact defined on user, we use it |
||
| 282 | $usecontact = false; |
||
| 283 | $arrayidcontact = $object->getIdContact('external', 'CUSTOMER'); |
||
| 284 | if (count($arrayidcontact) > 0) |
||
| 285 | { |
||
| 286 | $usecontact = true; |
||
| 287 | $result = $object->fetch_contact($arrayidcontact[0]); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Recipient name |
||
| 291 | if (!empty($usecontact)) |
||
| 292 | { |
||
| 293 | 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)))) { |
||
| 294 | $socobject = $object->contact; |
||
| 295 | } else { |
||
| 296 | $socobject = $object->thirdparty; |
||
| 297 | // if we have a CUSTOMER contact and we dont use it as recipient we store the contact object for later use |
||
| 298 | $contactobject = $object->contact; |
||
| 299 | } |
||
| 300 | } else { |
||
| 301 | $socobject = $object->thirdparty; |
||
| 302 | } |
||
| 303 | |||
| 304 | // Open and load template |
||
| 305 | require_once ODTPHP_PATH.'odf.php'; |
||
| 306 | try { |
||
| 307 | $odfHandler = new odf( |
||
| 308 | $srctemplatepath, |
||
| 309 | array( |
||
| 310 | 'PATH_TO_TMP' => $conf->user->dir_temp, |
||
| 311 | 'ZIP_PROXY' => 'PclZipProxy', // PhpZipProxy or PclZipProxy. Got "bad compression method" error when using PhpZipProxy. |
||
| 312 | 'DELIMITER_LEFT' => '{', |
||
| 313 | 'DELIMITER_RIGHT' => '}' |
||
| 314 | ) |
||
| 315 | ); |
||
| 316 | } catch (Exception $e) |
||
| 317 | { |
||
| 318 | $this->error = $e->getMessage(); |
||
| 319 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 320 | return -1; |
||
| 321 | } |
||
| 322 | |||
| 323 | // Make substitutions into odt |
||
| 324 | $array_user = $this->get_substitutionarray_user($object, $outputlangs); |
||
| 325 | $array_soc = $this->get_substitutionarray_mysoc($mysoc, $outputlangs); |
||
| 326 | $array_thirdparty = $this->get_substitutionarray_thirdparty($socobject, $outputlangs); |
||
| 327 | $array_other = $this->get_substitutionarray_other($outputlangs); |
||
| 328 | // retrieve contact information for use in object as contact_xxx tags |
||
| 329 | $array_thirdparty_contact = array(); |
||
| 330 | if ($usecontact && is_object($contactobject)) $array_thirdparty_contact = $this->get_substitutionarray_contact($contactobject, $outputlangs, 'contact'); |
||
| 331 | |||
| 332 | $tmparray = array_merge($array_user, $array_soc, $array_thirdparty, $array_other, $array_thirdparty_contact); |
||
| 333 | complete_substitutions_array($tmparray, $outputlangs, $object); |
||
| 334 | $object->fetch_optionals(); |
||
| 335 | // Call the ODTSubstitution hook |
||
| 336 | $parameters = array('file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs, 'substitutionarray'=>&$tmparray); |
||
| 337 | $reshook = $hookmanager->executeHooks('ODTSubstitution', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 338 | foreach ($tmparray as $key=>$value) |
||
| 339 | { |
||
| 340 | try { |
||
| 341 | if (preg_match('/logo$/', $key)) // Image |
||
| 342 | { |
||
| 343 | if (file_exists($value)) $odfHandler->setImage($key, $value); |
||
| 344 | else $odfHandler->setVars($key, 'ErrorFileNotFound', true, 'UTF-8'); |
||
| 345 | } else // Text |
||
| 346 | { |
||
| 347 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 348 | } |
||
| 349 | } catch (OdfException $e) |
||
| 350 | { |
||
| 351 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | |||
| 355 | // Replace labels translated |
||
| 356 | $tmparray = $outputlangs->get_translations_for_substitutions(); |
||
| 357 | foreach ($tmparray as $key=>$value) |
||
| 358 | { |
||
| 359 | try { |
||
| 360 | $odfHandler->setVars($key, $value, true, 'UTF-8'); |
||
| 361 | } catch (OdfException $e) |
||
| 362 | { |
||
| 363 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | // Call the beforeODTSave hook |
||
| 368 | $parameters = array('odfHandler'=>&$odfHandler, 'file'=>$file, 'object'=>$object, 'outputlangs'=>$outputlangs); |
||
| 369 | $reshook = $hookmanager->executeHooks('beforeODTSave', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 370 | |||
| 371 | // Write new file |
||
| 372 | if (!empty($conf->global->MAIN_ODT_AS_PDF)) { |
||
| 373 | try { |
||
| 374 | $odfHandler->exportAsAttachedPDF($file); |
||
| 375 | } catch (Exception $e) { |
||
| 376 | $this->error = $e->getMessage(); |
||
| 377 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 378 | return -1; |
||
| 379 | } |
||
| 380 | } else { |
||
| 381 | try { |
||
| 382 | $odfHandler->saveToDisk($file); |
||
| 383 | } catch (Exception $e) { |
||
| 384 | $this->error = $e->getMessage(); |
||
| 385 | dol_syslog($e->getMessage(), LOG_WARNING); |
||
| 386 | return -1; |
||
| 387 | } |
||
| 388 | } |
||
| 389 | |||
| 390 | $reshook = $hookmanager->executeHooks('afterODTCreation', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
||
| 391 | |||
| 392 | if (!empty($conf->global->MAIN_UMASK)) |
||
| 393 | @chmod($file, octdec($conf->global->MAIN_UMASK)); |
||
| 394 | |||
| 395 | $odfHandler = null; // Destroy object |
||
| 396 | |||
| 397 | $this->result = array('fullpath'=>$file); |
||
| 398 | |||
| 399 | return 1; // Success |
||
| 400 | } else { |
||
| 401 | $this->error = $langs->transnoentities("ErrorCanNotCreateDir", $dir); |
||
| 402 | return -1; |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | return -1; |
||
| 407 | } |
||
| 430 |