| Total Complexity | 52 |
| Total Lines | 457 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like doc_generic_contract_odt often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use doc_generic_contract_odt, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class doc_generic_contract_odt extends ModelePDFContract |
||
| 40 | { |
||
| 41 | /** |
||
| 42 | * @var string Dolibarr version of the loaded document |
||
| 43 | */ |
||
| 44 | public $version = 'dolibarr'; |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor |
||
| 49 | * |
||
| 50 | * @param DoliDB $db Database handler |
||
| 51 | */ |
||
| 52 | public function __construct($db) |
||
| 53 | { |
||
| 54 | global $conf, $langs, $mysoc; |
||
| 55 | |||
| 56 | // Load translation files required by the page |
||
| 57 | $langs->loadLangs(array("main", "companies")); |
||
| 58 | |||
| 59 | $this->db = $db; |
||
| 60 | $this->name = "ODT templates"; |
||
| 61 | $this->description = $langs->trans("DocumentModelOdt"); |
||
| 62 | $this->scandir = 'CONTRACT_ADDON_PDF_ODT_PATH'; // Name of constant that is used to save list of directories to scan |
||
| 63 | |||
| 64 | // Page size for A4 format |
||
| 65 | $this->type = 'odt'; |
||
| 66 | $this->page_largeur = 0; |
||
| 67 | $this->page_hauteur = 0; |
||
| 68 | $this->format = array($this->page_largeur, $this->page_hauteur); |
||
| 69 | $this->marge_gauche = 0; |
||
| 70 | $this->marge_droite = 0; |
||
| 71 | $this->marge_haute = 0; |
||
| 72 | $this->marge_basse = 0; |
||
| 73 | |||
| 74 | $this->option_logo = 1; // Display logo |
||
| 75 | $this->option_tva = 0; // Manage the vat CONTRACT_TVAOPTION |
||
| 76 | $this->option_modereg = 0; // Display payment mode |
||
| 77 | $this->option_condreg = 0; // Display payment terms |
||
| 78 | $this->option_multilang = 1; // Available in several languages |
||
| 79 | $this->option_escompte = 0; // Displays if there has been a discount |
||
| 80 | $this->option_credit_note = 0; // Support credit notes |
||
| 81 | $this->option_freetext = 1; // Support add of a personalised text |
||
| 82 | $this->option_draft_watermark = 0; // Support add of a watermark on drafts |
||
| 83 | |||
| 84 | // Get source company |
||
| 85 | $this->emetteur = $mysoc; |
||
| 86 | if (!$this->emetteur->country_code) { |
||
| 87 | $this->emetteur->country_code = substr($langs->defaultlang, -2); // By default if not defined |
||
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Return description of a module |
||
| 94 | * |
||
| 95 | * @param Translate $langs Lang object to use for output |
||
| 96 | * @return string Description |
||
| 97 | */ |
||
| 98 | public function info($langs) |
||
| 99 | { |
||
| 100 | global $conf, $langs; |
||
| 101 | |||
| 102 | // Load translation files required by the page |
||
| 103 | $langs->loadLangs(array('companies', 'errors')); |
||
| 104 | |||
| 105 | $form = new Form($this->db); |
||
| 106 | |||
| 107 | $texte = $this->description . ".<br>\n"; |
||
| 108 | $texte .= '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST" enctype="multipart/form-data">'; |
||
| 109 | $texte .= '<input type="hidden" name="token" value="' . newToken() . '">'; |
||
| 110 | $texte .= '<input type="hidden" name="page_y" value="">'; |
||
| 111 | $texte .= '<input type="hidden" name="action" value="setModuleOptions">'; |
||
| 112 | $texte .= '<input type="hidden" name="param1" value="CONTRACT_ADDON_PDF_ODT_PATH">'; |
||
| 113 | $texte .= '<table class="nobordernopadding centpercent">'; |
||
| 114 | |||
| 115 | // List of directories area |
||
| 116 | $texte .= '<tr><td>'; |
||
| 117 | $texttitle = $langs->trans("ListOfDirectories"); |
||
| 118 | $listofdir = explode(',', preg_replace('/[\r\n]+/', ',', trim($conf->global->CONTRACT_ADDON_PDF_ODT_PATH))); |
||
| 119 | $listoffiles = array(); |
||
| 120 | foreach ($listofdir as $key => $tmpdir) { |
||
| 121 | $tmpdir = trim($tmpdir); |
||
| 122 | $tmpdir = preg_replace('/DOL_DATA_ROOT/', DOL_DATA_ROOT, $tmpdir); |
||
| 123 | if (!$tmpdir) { |
||
| 124 | unset($listofdir[$key]); |
||
| 125 | continue; |
||
| 126 | } |
||
| 127 | if (!is_dir($tmpdir)) { |
||
| 128 | $texttitle .= img_warning($langs->trans("ErrorDirNotFound", $tmpdir), 0); |
||
| 129 | } else { |
||
| 130 | $tmpfiles = dol_dir_list($tmpdir, 'files', 0, '\.(ods|odt)'); |
||
| 131 | if (count($tmpfiles)) { |
||
| 132 | $listoffiles = array_merge($listoffiles, $tmpfiles); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | $texthelp = $langs->trans("ListOfDirectoriesForModelGenODT"); |
||
| 137 | $texthelp .= '<br><br><span class="opacitymedium">' . $langs->trans("ExampleOfDirectoriesForModelGen") . '</span>'; |
||
| 138 | // Add list of substitution keys |
||
| 139 | $texthelp .= '<br>' . $langs->trans("FollowingSubstitutionKeysCanBeUsed") . '<br>'; |
||
| 140 | $texthelp .= $langs->transnoentitiesnoconv("FullListOnOnlineDocumentation"); // This contains an url, we don't modify it |
||
| 141 | |||
| 142 | $texte .= $form->textwithpicto($texttitle, $texthelp, 1, 'help', '', 1, 3, $this->name); |
||
| 143 | $texte .= '<div><div style="display: inline-block; min-width: 100px; vertical-align: middle;">'; |
||
| 144 | $texte .= '<textarea class="flat" cols="60" name="value1">'; |
||
| 145 | $texte .= getDolGlobalString('CONTRACT_ADDON_PDF_ODT_PATH'); |
||
| 146 | $texte .= '</textarea>'; |
||
| 147 | $texte .= '</div><div style="display: inline-block; vertical-align: middle;">'; |
||
| 148 | $texte .= '<input type="submit" class="button reposition smallpaddingimp" value="' . dol_escape_htmltag($langs->trans("Upload")) . '" name="upload">'; |
||
| 149 | $texte .= '<br></div></div>'; |
||
| 150 | |||
| 151 | // Scan directories |
||
| 152 | $nbofiles = count($listoffiles); |
||
| 153 | if (getDolGlobalString('CONTRACT_ADDON_PDF_ODT_PATH')) { |
||
| 154 | $texte .= $langs->trans("NumberOfModelFilesFound") . ': <b>'; |
||
| 155 | //$texte.=$nbofiles?'<a id="a_'.get_class($this).'" href="#">':''; |
||
| 156 | $texte .= count($listoffiles); |
||
| 157 | //$texte.=$nbofiles?'</a>':''; |
||
| 158 | $texte .= '</b>'; |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($nbofiles) { |
||
| 162 | $texte .= '<div id="div_' . get_class($this) . '" class="hiddenx">'; |
||
| 163 | // Show list of found files |
||
| 164 | foreach ($listoffiles as $file) { |
||
| 165 | $texte .= '- ' . $file['name'] . ' <a href="' . DOL_URL_ROOT . '/document.php?modulepart=doctemplates&file=contracts/' . urlencode(basename($file['name'])) . '">' . img_picto('', 'listlight') . '</a>'; |
||
| 166 | $texte .= ' <a class="reposition" href="' . $_SERVER['PHP_SELF'] . '?modulepart=doctemplates&keyforuploaddir=CONTRACT_ADDON_PDF_ODT_PATH&action=deletefile&token=' . newToken() . '&file=' . urlencode(basename($file['name'])) . '">' . img_picto('', 'delete') . '</a>'; |
||
| 167 | $texte .= '<br>'; |
||
| 168 | } |
||
| 169 | $texte .= '</div>'; |
||
| 170 | } |
||
| 171 | // Add input to upload a new template file. |
||
| 172 | $texte .= '<div>' . $langs->trans("UploadNewTemplate"); |
||
| 173 | $maxfilesizearray = getMaxFileSizeArray(); |
||
| 174 | $maxmin = $maxfilesizearray['maxmin']; |
||
| 175 | if ($maxmin > 0) { |
||
| 176 | $texte .= '<input type="hidden" name="MAX_FILE_SIZE" value="' . ($maxmin * 1024) . '">'; // MAX_FILE_SIZE must precede the field type=file |
||
| 177 | } |
||
| 178 | $texte .= ' <input type="file" name="uploadfile">'; |
||
| 179 | $texte .= '<input type="hidden" value="CONTRACT_ADDON_PDF_ODT_PATH" name="keyforuploaddir">'; |
||
| 180 | $texte .= '<input type="submit" class="button reposition smallpaddingimp" value="' . dol_escape_htmltag($langs->trans("Upload")) . '" name="upload">'; |
||
| 181 | $texte .= '</div>'; |
||
| 182 | |||
| 183 | $texte .= '</td>'; |
||
| 184 | |||
| 185 | $texte .= '</tr>'; |
||
| 186 | |||
| 187 | $texte .= '</table>'; |
||
| 188 | $texte .= '</form>'; |
||
| 189 | |||
| 190 | return $texte; |
||
| 191 | } |
||
| 192 | |||
| 193 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
||
| 194 | /** |
||
| 195 | * Function to build a document on disk using the generic odt module. |
||
| 196 | * |
||
| 197 | * @param Contrat $object Object source to build document |
||
| 198 | * @param Translate $outputlangs Lang output object |
||
| 199 | * @param string $srctemplatepath Full path of source filename for generator using a template file |
||
| 200 | * @param int $hidedetails Do not show line details |
||
| 201 | * @param int $hidedesc Do not show desc |
||
| 202 | * @param int $hideref Do not show ref |
||
| 203 | * @return int 1 if OK, <=0 if KO |
||
| 204 | */ |
||
| 205 | public function write_file($object, $outputlangs, $srctemplatepath, $hidedetails = 0, $hidedesc = 0, $hideref = 0) |
||
| 496 | } |
||
| 497 | } |
||
| 498 |