| Total Complexity | 41 |
| Total Lines | 267 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CdavLib 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 CdavLib, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class CdavLib |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var DoliDB |
||
| 39 | */ |
||
| 40 | private $db; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var User |
||
| 44 | */ |
||
| 45 | private $user; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var Translate |
||
| 49 | */ |
||
| 50 | private $langs; // @phpstan-ignore-line |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Constructor |
||
| 54 | * |
||
| 55 | * @param User $user user |
||
| 56 | * @param DoliDB $db Database handler |
||
| 57 | * @param Translate $langs translation |
||
| 58 | */ |
||
| 59 | public function __construct($user, $db, $langs) |
||
| 60 | { |
||
| 61 | $this->user = $user; |
||
| 62 | $this->db = $db; |
||
| 63 | $this->langs = $langs; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Base sql request for calendar events |
||
| 68 | * |
||
| 69 | * @param int $calid Calendard id |
||
| 70 | * @param int|boolean $oid Oid |
||
| 71 | * @param int|boolean $ouri Ouri |
||
| 72 | * @return string |
||
| 73 | */ |
||
| 74 | public function getSqlCalEvents($calid, $oid = false, $ouri = false) |
||
| 75 | { |
||
| 76 | // TODO : replace GROUP_CONCAT by |
||
| 77 | $sql = 'SELECT |
||
| 78 | a.tms AS lastupd, |
||
| 79 | a.*, |
||
| 80 | sp.firstname, |
||
| 81 | sp.lastname, |
||
| 82 | sp.address, |
||
| 83 | sp.zip, |
||
| 84 | sp.town, |
||
| 85 | co.label country_label, |
||
| 86 | sp.phone, |
||
| 87 | sp.phone_perso, |
||
| 88 | sp.phone_mobile, |
||
| 89 | s.nom AS soc_nom, |
||
| 90 | s.address soc_address, |
||
| 91 | s.zip soc_zip, |
||
| 92 | s.town soc_town, |
||
| 93 | cos.label soc_country_label, |
||
| 94 | s.phone soc_phone, |
||
| 95 | ac.sourceuid, |
||
| 96 | (SELECT GROUP_CONCAT(u.login) FROM ' . MAIN_DB_PREFIX . 'actioncomm_resources ar |
||
| 97 | LEFT OUTER JOIN ' . MAIN_DB_PREFIX . 'user AS u ON (u.rowid=fk_element) |
||
| 98 | WHERE ar.element_type=\'user\' AND fk_actioncomm=a.id) AS other_users |
||
| 99 | FROM ' . MAIN_DB_PREFIX . 'actioncomm AS a'; |
||
| 100 | $sql .= " LEFT JOIN '.MAIN_DB_PREFIX.'c_country as co ON co.rowid = sp.fk_pays |
||
| 101 | LEFT JOIN '.MAIN_DB_PREFIX.'c_country as cos ON cos.rowid = s.fk_pays |
||
| 102 | WHERE a.id IN (SELECT ar.fk_actioncomm FROM '.MAIN_DB_PREFIX.'actioncomm_resources ar WHERE ar.element_type='user' AND ar.fk_element=" . ((int) $calid) . ") |
||
| 103 | AND a.code IN (SELECT cac.code FROM '.MAIN_DB_PREFIX.'c_actioncomm cac WHERE cac.type <> 'systemauto') |
||
| 104 | AND a.entity IN (" . getEntity('societe', 1) . ")"; |
||
| 105 | // TODO Restrict on external users |
||
| 106 | if ($oid !== false) { |
||
| 107 | if ($ouri === false) { |
||
| 108 | $sql .= ' AND a.id = ' . ((int) $oid); |
||
| 109 | } else { |
||
| 110 | $sql .= ' AND (a.id = ' . ((int) $oid) . " OR ac.uuidext = '" . $this->db->escape($ouri) . "')"; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | return $sql; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Convert calendar row to VCalendar string |
||
| 119 | * |
||
| 120 | * @param int $calid Calendar id |
||
| 121 | * @param Object $obj Object id |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function toVCalendar($calid, $obj) |
||
| 125 | { |
||
| 126 | /*$categ = array(); |
||
| 127 | if($obj->soc_client) |
||
| 128 | { |
||
| 129 | $nick[] = $obj->soc_code_client; |
||
| 130 | $categ[] = $this->langs->transnoentitiesnoconv('Customer'); |
||
| 131 | }*/ |
||
| 132 | |||
| 133 | $location = $obj->location; |
||
| 134 | |||
| 135 | // contact address |
||
| 136 | if (empty($location) && !empty($obj->address)) { |
||
| 137 | $location = trim(str_replace(array("\r", "\t", "\n"), ' ', $obj->address)); |
||
| 138 | $location = trim($location . ', ' . $obj->zip); |
||
| 139 | $location = trim($location . ' ' . $obj->town); |
||
| 140 | $location = trim($location . ', ' . $obj->country_label); |
||
| 141 | } |
||
| 142 | |||
| 143 | // contact address |
||
| 144 | if (empty($location) && !empty($obj->soc_address)) { |
||
| 145 | $location = trim(str_replace(array("\r", "\t", "\n"), ' ', $obj->soc_address)); |
||
| 146 | $location = trim($location . ', ' . $obj->soc_zip); |
||
| 147 | $location = trim($location . ' ' . $obj->soc_town); |
||
| 148 | $location = trim($location . ', ' . $obj->soc_country_label); |
||
| 149 | } |
||
| 150 | |||
| 151 | /* |
||
| 152 | $address = explode("\n", $obj->address, 2); |
||
| 153 | foreach ($address as $kAddr => $vAddr) { |
||
| 154 | $address[$kAddr] = trim(str_replace(array("\r", "\t"), ' ', str_replace("\n", ' | ', trim($vAddr)))); |
||
| 155 | } |
||
| 156 | $address[] = ''; |
||
| 157 | $address[] = ''; |
||
| 158 | */ |
||
| 159 | |||
| 160 | if ($obj->percent == -1 && trim($obj->datep) != '') { |
||
| 161 | $type = 'VEVENT'; |
||
| 162 | } else { |
||
| 163 | $type = 'VTODO'; |
||
| 164 | } |
||
| 165 | |||
| 166 | $timezone = date_default_timezone_get(); |
||
| 167 | |||
| 168 | $caldata = "BEGIN:VCALENDAR\n"; |
||
| 169 | $caldata .= "VERSION:2.0\n"; |
||
| 170 | $caldata .= "METHOD:PUBLISH\n"; |
||
| 171 | $caldata .= "PRODID:-//Dolibarr CDav//FR\n"; |
||
| 172 | $caldata .= "BEGIN:" . $type . "\n"; |
||
| 173 | $caldata .= "CREATED:" . gmdate('Ymd\THis', strtotime($obj->datec)) . "Z\n"; |
||
| 174 | $caldata .= "LAST-MODIFIED:" . gmdate('Ymd\THis', strtotime($obj->lastupd)) . "Z\n"; |
||
| 175 | $caldata .= "DTSTAMP:" . gmdate('Ymd\THis', strtotime($obj->lastupd)) . "Z\n"; |
||
| 176 | if ($obj->sourceuid == '') { |
||
| 177 | $caldata .= "UID:" . $obj->id . '-ev-' . $calid . '-cal-' . constant('CDAV_URI_KEY') . "\n"; |
||
| 178 | } else { |
||
| 179 | $caldata .= "UID:" . $obj->sourceuid . "\n"; |
||
| 180 | } |
||
| 181 | $caldata .= "SUMMARY:" . $obj->label . "\n"; |
||
| 182 | $caldata .= "LOCATION:" . $location . "\n"; |
||
| 183 | $caldata .= "PRIORITY:" . $obj->priority . "\n"; |
||
| 184 | if ($obj->fulldayevent) { |
||
| 185 | $caldata .= "DTSTART;VALUE=DATE:" . date('Ymd', strtotime($obj->datep)) . "\n"; |
||
| 186 | if ($type == 'VEVENT') { |
||
| 187 | if (trim($obj->datep2) != '') { |
||
| 188 | $caldata .= "DTEND;VALUE=DATE:" . date('Ymd', strtotime($obj->datep2) + 1) . "\n"; |
||
| 189 | } else { |
||
| 190 | $caldata .= "DTEND;VALUE=DATE:" . date('Ymd', strtotime($obj->datep) + (25 * 3600)) . "\n"; |
||
| 191 | } |
||
| 192 | } elseif (trim($obj->datep2) != '') { |
||
| 193 | $caldata .= "DUE;VALUE=DATE:" . date('Ymd', strtotime($obj->datep2) + 1) . "\n"; |
||
| 194 | } |
||
| 195 | } else { |
||
| 196 | $caldata .= "DTSTART;TZID=" . $timezone . ":" . strtr($obj->datep, array(" " => "T", ":" => "", "-" => "")) . "\n"; |
||
| 197 | if ($type == 'VEVENT') { |
||
| 198 | if (trim($obj->datep2) != '') { |
||
| 199 | $caldata .= "DTEND;TZID=" . $timezone . ":" . strtr($obj->datep2, array(" " => "T", ":" => "", "-" => "")) . "\n"; |
||
| 200 | } else { |
||
| 201 | $caldata .= "DTEND;TZID=" . $timezone . ":" . strtr($obj->datep, array(" " => "T", ":" => "", "-" => "")) . "\n"; |
||
| 202 | } |
||
| 203 | } elseif (trim($obj->datep2) != '') { |
||
| 204 | $caldata .= "DUE;TZID=" . $timezone . ":" . strtr($obj->datep2, array(" " => "T", ":" => "", "-" => "")) . "\n"; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | $caldata .= "CLASS:PUBLIC\n"; |
||
| 208 | if ($obj->transparency == 1) { |
||
| 209 | $caldata .= "TRANSP:TRANSPARENT\n"; |
||
| 210 | } else { |
||
| 211 | $caldata .= "TRANSP:OPAQUE\n"; |
||
| 212 | } |
||
| 213 | |||
| 214 | if ($type == 'VEVENT') { |
||
| 215 | $caldata .= "STATUS:CONFIRMED\n"; |
||
| 216 | } elseif ($obj->percent == 0) { |
||
| 217 | $caldata .= "STATUS:NEEDS-ACTION\n"; |
||
| 218 | } elseif ($obj->percent == 100) { |
||
| 219 | $caldata .= "STATUS:COMPLETED\n"; |
||
| 220 | } else { |
||
| 221 | $caldata .= "STATUS:IN-PROCESS\n"; |
||
| 222 | $caldata .= "PERCENT-COMPLETE:" . $obj->percent . "\n"; |
||
| 223 | } |
||
| 224 | |||
| 225 | $caldata .= "DESCRIPTION:"; |
||
| 226 | $caldata .= strtr($obj->note, array("\n" => "\\n", "\r" => "")); |
||
| 227 | if (!empty($obj->soc_nom)) { |
||
| 228 | $caldata .= "\\n*DOLIBARR-SOC: " . $obj->soc_nom; |
||
| 229 | } |
||
| 230 | if (!empty($obj->soc_phone)) { |
||
| 231 | $caldata .= "\\n*DOLIBARR-SOC-TEL: " . $obj->soc_phone; |
||
| 232 | } |
||
| 233 | if (!empty($obj->firstname) || !empty($obj->lastname)) { |
||
| 234 | $caldata .= "\\n*DOLIBARR-CTC: " . trim($obj->firstname . ' ' . $obj->lastname); |
||
| 235 | } |
||
| 236 | if (!empty($obj->phone) || !empty($obj->phone_perso) || !empty($obj->phone_mobile)) { |
||
| 237 | $caldata .= "\\n*DOLIBARR-CTC-TEL: " . trim($obj->phone . ' ' . $obj->phone_perso . ' ' . $obj->phone_mobile); |
||
| 238 | } |
||
| 239 | if (strpos($obj->other_users, ',')) { // several |
||
| 240 | $caldata .= "\\n*DOLIBARR-USR: " . $obj->other_users; |
||
| 241 | } |
||
| 242 | $caldata .= "\n"; |
||
| 243 | |||
| 244 | $caldata .= "END:" . $type . "\n"; |
||
| 245 | $caldata .= "END:VCALENDAR\n"; |
||
| 246 | |||
| 247 | return $caldata; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * getFullCalendarObjects |
||
| 252 | * |
||
| 253 | * @param int $calendarId Calendar id |
||
| 254 | * @param int $bCalendarData Add calendar data |
||
| 255 | * @return array|string[][] |
||
| 256 | */ |
||
| 257 | public function getFullCalendarObjects($calendarId, $bCalendarData) |
||
| 302 | } |
||
| 303 | } |
||
| 304 |