partnership.class.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. <?php
  2. /* Copyright (C) 2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2021 NextGestion <contact@nextgestion.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/partnership/class/partnership.class.php
  20. * \ingroup partnership
  21. * \brief This file is a CRUD class file for Partnership (Create/Read/Update/Delete)
  22. */
  23. // Put here all includes required by your class file
  24. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  25. /**
  26. * Class for Partnership
  27. */
  28. class Partnership extends CommonObject
  29. {
  30. /**
  31. * @var string ID of module.
  32. */
  33. public $module = 'partnership';
  34. /**
  35. * @var string ID to identify managed object.
  36. */
  37. public $element = 'partnership';
  38. /**
  39. * @var string Name of table without prefix where object is stored. This is also the key used for extrafields management.
  40. */
  41. public $table_element = 'partnership';
  42. /**
  43. * @var int Does this object support multicompany module ?
  44. * 0=No test on entity, 1=Test with field entity, 'field@table'=Test with link by field@table
  45. */
  46. public $ismultientitymanaged = 0;
  47. /**
  48. * @var int Does object support extrafields ? 0=No, 1=Yes
  49. */
  50. public $isextrafieldmanaged = 1;
  51. /**
  52. * @var string String with name of icon for partnership. Must be the part after the 'object_' into object_partnership.png
  53. */
  54. public $picto = 'partnership';
  55. public $type_code;
  56. public $type_label;
  57. const STATUS_DRAFT = 0;
  58. const STATUS_VALIDATED = 1; // Validate (no more draft)
  59. const STATUS_APPROVED = 2; // Approved
  60. const STATUS_REFUSED = 3; // Refused
  61. const STATUS_CANCELED = 9;
  62. /**
  63. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  64. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  65. * 'label' the translation key.
  66. * 'picto' is code of a picto to show before value in forms
  67. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  68. * 'position' is the sort order of field.
  69. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  70. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  71. * 'noteditable' says if field is not editable (1 or 0)
  72. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  73. * 'index' if we want an index in database.
  74. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  75. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  76. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  77. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  78. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  79. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  80. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  81. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  82. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  83. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  84. *
  85. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  86. */
  87. // BEGIN MODULEBUILDER PROPERTIES
  88. /**
  89. * @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
  90. */
  91. public $fields=array(
  92. 'rowid' => array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>'1', 'position'=>1, 'notnull'=>1, 'visible'=>0, 'noteditable'=>'1', 'index'=>1, 'css'=>'left', 'comment'=>"Id"),
  93. 'ref' => array('type'=>'varchar(128)', 'label'=>'Ref', 'enabled'=>'1', 'position'=>10, 'notnull'=>1, 'visible'=>4, 'noteditable'=>'1', 'default'=>'(PROV)', 'index'=>1, 'searchall'=>1, 'showoncombobox'=>'1', 'comment'=>"Reference of object", 'csslist'=>'tdoverflowmax150'),
  94. 'entity' => array('type'=>'integer', 'label'=>'Entity', 'enabled'=>'isModEnabled("multicompany")', 'position'=>15, 'notnull'=>1, 'visible'=>-2, 'default'=>'1', 'index'=>1,),
  95. 'fk_type' => array('type'=>'integer:PartnershipType:partnership/class/partnership_type.class.php:0:(active:=:1)', 'label'=>'Type', 'enabled'=>'1', 'position'=>20, 'notnull'=>1, 'visible'=>1, 'csslist'=>'tdoverflowmax125'),
  96. 'fk_soc' => array('type'=>'integer:Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))', 'label'=>'ThirdParty', 'picto'=>'company', 'enabled'=>'1', 'position'=>50, 'notnull'=>-1, 'visible'=>1, 'index'=>1, 'css'=>'maxwidth500', 'csslist'=>'tdoverflowmax125',),
  97. 'note_public' => array('type'=>'html', 'label'=>'NotePublic', 'enabled'=>'1', 'position'=>61, 'notnull'=>0, 'visible'=>0,),
  98. 'note_private' => array('type'=>'html', 'label'=>'NotePrivate', 'enabled'=>'1', 'position'=>62, 'notnull'=>0, 'visible'=>0,),
  99. 'date_creation' => array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>'1', 'position'=>500, 'notnull'=>1, 'visible'=>-2, 'csslist'=>'nowraponall'),
  100. 'tms' => array('type'=>'timestamp', 'label'=>'DateModification', 'enabled'=>'1', 'position'=>501, 'notnull'=>0, 'visible'=>-2, 'csslist'=>'nowraponall'),
  101. 'fk_user_creat' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserAuthor', 'enabled'=>'1', 'position'=>510, 'notnull'=>1, 'visible'=>-2, 'foreignkey'=>'user.rowid', 'csslist'=>'tdoverflowmax125'),
  102. 'fk_user_modif' => array('type'=>'integer:User:user/class/user.class.php', 'label'=>'UserModif', 'enabled'=>'1', 'position'=>511, 'notnull'=>-1, 'visible'=>-2, 'csslist'=>'tdoverflowmax125'),
  103. 'last_main_doc' => array('type'=>'varchar(255)', 'label'=>'LastMainDoc', 'enabled'=>'1', 'position'=>600, 'notnull'=>0, 'visible'=>0,),
  104. 'import_key' => array('type'=>'varchar(14)', 'label'=>'ImportId', 'enabled'=>'1', 'position'=>1000, 'notnull'=>-1, 'visible'=>-2,),
  105. 'model_pdf' => array('type'=>'varchar(255)', 'label'=>'Model pdf', 'enabled'=>'1', 'position'=>1010, 'notnull'=>-1, 'visible'=>0,),
  106. 'date_partnership_start' => array('type'=>'date', 'label'=>'DatePartnershipStart', 'enabled'=>'1', 'position'=>52, 'notnull'=>1, 'visible'=>1,),
  107. 'date_partnership_end' => array('type'=>'date', 'label'=>'DatePartnershipEnd', 'enabled'=>'1', 'position'=>53, 'notnull'=>0, 'visible'=>1,),
  108. 'url_to_check' => array('type'=>'url', 'label'=>'UrlToCheck', 'enabled'=>'getDolGlobalString("PARTNERSHIP_BACKLINKS_TO_CHECK")', 'position'=>70, 'notnull'=>0, 'visible'=>-1, 'csslist'=>'tdoverflowmax150'),
  109. 'count_last_url_check_error' => array('type'=>'integer', 'label'=>'CountLastUrlCheckError', 'enabled'=>'getDolGlobalString("PARTNERSHIP_BACKLINKS_TO_CHECK")', 'position'=>71, 'notnull'=>0, 'visible'=>-4, 'default'=>'0',),
  110. 'last_check_backlink' => array('type'=>'datetime', 'label'=>'LastCheckBacklink', 'enabled'=>'getDolGlobalString("PARTNERSHIP_BACKLINKS_TO_CHECK")', 'position'=>72, 'notnull'=>0, 'visible'=>-4, 'csslist'=>'nowraponall'),
  111. 'reason_decline_or_cancel' => array('type'=>'text', 'label'=>'ReasonDeclineOrCancel', 'enabled'=>'1', 'position'=>73, 'notnull'=>0, 'visible'=>-2,),
  112. 'ip' => array('type'=>'ip', 'label'=>'IPOfApplicant', 'enabled'=>'1', 'position'=>74, 'notnull'=>0, 'visible'=>-2,),
  113. 'status' => array('type'=>'smallint', 'label'=>'Status', 'enabled'=>'1', 'position'=>2000, 'notnull'=>1, 'visible'=>2, 'default'=>'0', 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Validated', '2'=>'Approved', '3'=>'Refused', '9'=>'Terminated'),),
  114. );
  115. public $rowid;
  116. public $ref;
  117. public $entity;
  118. public $fk_type;
  119. public $note_public;
  120. public $note_private;
  121. public $date_creation;
  122. public $tms;
  123. public $fk_user_creat;
  124. public $fk_user_modif;
  125. public $last_main_doc;
  126. public $import_key;
  127. public $model_pdf;
  128. public $date_partnership_start;
  129. public $date_partnership_end;
  130. public $url_to_check;
  131. public $count_last_url_check_error;
  132. public $last_check_backlink;
  133. public $reason_decline_or_cancel;
  134. public $fk_soc;
  135. public $fk_member;
  136. public $ip;
  137. public $status;
  138. // END MODULEBUILDER PROPERTIES
  139. /**
  140. * Constructor
  141. *
  142. * @param DoliDb $db Database handler
  143. */
  144. public function __construct(DoliDB $db)
  145. {
  146. global $conf, $langs;
  147. $this->db = $db;
  148. if (getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
  149. $this->fields['fk_member'] = array('type'=>'integer:Adherent:adherents/class/adherent.class.php:1', 'label'=>'Member', 'enabled'=>'1', 'position'=>50, 'notnull'=>-1, 'visible'=>1, 'index'=>1, 'picto'=>'member', 'csslist'=>'tdoverflowmax150');
  150. } else {
  151. $this->fields['fk_soc'] = array('type'=>'integer:Societe:societe/class/societe.class.php:1:((status:=:1) AND (entity:IN:__SHARED_ENTITIES__))', 'label'=>'ThirdParty', 'enabled'=>'1', 'position'=>50, 'notnull'=>-1, 'visible'=>1, 'index'=>1, 'picto'=>'company', 'css'=>'maxwidth500', 'csslist'=>'tdoverflowmax150');
  152. }
  153. if (!getDolGlobalString('MAIN_SHOW_TECHNICAL_ID') && isset($this->fields['rowid']) && !empty($this->fields['ref'])) {
  154. $this->fields['rowid']['visible'] = 0;
  155. }
  156. // Unset fields that are disabled
  157. foreach ($this->fields as $key => $val) {
  158. if (isset($val['enabled']) && empty($val['enabled'])) {
  159. unset($this->fields[$key]);
  160. }
  161. }
  162. // Translate some data of arrayofkeyval
  163. if (is_object($langs)) {
  164. foreach ($this->fields as $key => $val) {
  165. if (!empty($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) {
  166. foreach ($val['arrayofkeyval'] as $key2 => $val2) {
  167. $this->fields[$key]['arrayofkeyval'][$key2] = $langs->trans($val2);
  168. }
  169. }
  170. }
  171. }
  172. }
  173. /**
  174. * Create object into database
  175. *
  176. * @param User $user User that creates
  177. * @param bool $notrigger false=launch triggers after, true=disable triggers
  178. * @return int Return integer <0 if KO, Id of created object if OK
  179. */
  180. public function create(User $user, $notrigger = false)
  181. {
  182. if ($this->fk_soc <= 0 && $this->fk_member <= 0) {
  183. $this->error[] = "ErrorThirpdartyOrMemberidIsMandatory";
  184. return -1;
  185. }
  186. $this->status = 0;
  187. return $this->createCommon($user, $notrigger);
  188. }
  189. /**
  190. * Clone an object into another one
  191. *
  192. * @param User $user User that creates
  193. * @param int $fromid Id of object to clone
  194. * @return mixed New object created, <0 if KO
  195. */
  196. public function createFromClone(User $user, $fromid)
  197. {
  198. global $langs, $extrafields;
  199. $error = 0;
  200. dol_syslog(__METHOD__, LOG_DEBUG);
  201. $object = new self($this->db);
  202. $this->db->begin();
  203. // Load source object
  204. $result = $object->fetchCommon($fromid);
  205. if ($result > 0 && !empty($object->table_element_line)) {
  206. $object->fetchLines();
  207. }
  208. // get lines so they will be clone
  209. //foreach($this->lines as $line)
  210. // $line->fetch_optionals();
  211. // Reset some properties
  212. unset($object->id);
  213. unset($object->fk_user_creat);
  214. unset($object->import_key);
  215. // Clear fields
  216. if (property_exists($object, 'ref')) {
  217. $object->ref = empty($this->fields['ref']['default']) ? "Copy_Of_".$object->ref : $this->fields['ref']['default'];
  218. }
  219. if (property_exists($object, 'label')) {
  220. $object->label = empty($this->fields['label']['default']) ? $langs->trans("CopyOf")." ".$object->label : $this->fields['label']['default'];
  221. }
  222. if (property_exists($object, 'status')) {
  223. $object->status = self::STATUS_DRAFT;
  224. }
  225. if (property_exists($object, 'date_creation')) {
  226. $object->date_creation = dol_now();
  227. }
  228. if (property_exists($object, 'date_modification')) {
  229. $object->date_modification = null;
  230. }
  231. // ...
  232. // Clear extrafields that are unique
  233. if (is_array($object->array_options) && count($object->array_options) > 0) {
  234. $extrafields->fetch_name_optionals_label($this->table_element);
  235. foreach ($object->array_options as $key => $option) {
  236. $shortkey = preg_replace('/options_/', '', $key);
  237. if (!empty($extrafields->attributes[$this->table_element]['unique'][$shortkey])) {
  238. //var_dump($key);
  239. //var_dump($clonedObj->array_options[$key]); exit;
  240. unset($object->array_options[$key]);
  241. }
  242. }
  243. }
  244. // Create clone
  245. $object->context['createfromclone'] = 'createfromclone';
  246. $result = $object->createCommon($user);
  247. if ($result < 0) {
  248. $error++;
  249. $this->error = $object->error;
  250. $this->errors = $object->errors;
  251. }
  252. if (!$error) {
  253. // copy internal contacts
  254. if ($this->copy_linked_contact($object, 'internal') < 0) {
  255. $error++;
  256. }
  257. }
  258. if (!$error) {
  259. // copy external contacts if same company
  260. if (property_exists($this, 'fk_soc') && $this->fk_soc == $object->socid) {
  261. if ($this->copy_linked_contact($object, 'external') < 0) {
  262. $error++;
  263. }
  264. }
  265. }
  266. unset($object->context['createfromclone']);
  267. // End
  268. if (!$error) {
  269. $this->db->commit();
  270. return $object;
  271. } else {
  272. $this->db->rollback();
  273. return -1;
  274. }
  275. }
  276. /**
  277. * Load object in memory from the database
  278. * Get object from database. Get also lines.
  279. *
  280. * @param int $id Id of object to load
  281. * @param string $ref Ref of object
  282. * @param int $fk_member fk_member
  283. * @param int $fk_soc fk_soc
  284. * @return int >0 if OK, <0 if KO, 0 if not found
  285. */
  286. public function fetch($id, $ref = null, $fk_member = null, $fk_soc = null)
  287. {
  288. // Check parameters
  289. if (empty($id) && empty($ref) && empty($fk_member) && empty($fk_soc)) {
  290. return -1;
  291. }
  292. $sql = 'SELECT p.rowid, p.ref, p.fk_type, p.fk_soc, p.fk_member, p.status';
  293. $sql .= ', p.entity, p.date_partnership_start, p.date_partnership_end, p.date_creation';
  294. $sql .= ', p.fk_user_creat, p.tms, p.fk_user_modif, p.fk_user_modif';
  295. $sql .= ', p.note_private, p.note_public, p.url_to_check';
  296. $sql .= ', p.last_main_doc, p.count_last_url_check_error, p.last_check_backlink, p.reason_decline_or_cancel';
  297. $sql .= ', p.import_key, p.model_pdf';
  298. $sql .= ', pt.code as type_code, pt.label as type_label';
  299. $sql .= ' FROM '.MAIN_DB_PREFIX.'partnership as p';
  300. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_partnership_type as pt ON p.fk_type = pt.rowid';
  301. if ($id) {
  302. $sql .= " WHERE p.rowid = ".((int) $id);
  303. } else {
  304. $sql .= " WHERE p.entity IN (0,".getEntity('partnership').")"; // Dont't use entity if you use rowid
  305. }
  306. if ($ref) {
  307. $sql .= " AND p.ref='".$this->db->escape($ref)."'";
  308. }
  309. if ($fk_member > 0) {
  310. $sql .= ' AND p.fk_member = '.((int) $fk_member);
  311. }
  312. if ($fk_soc > 0) {
  313. $sql .= ' AND p.fk_soc = '.((int) $fk_soc);
  314. }
  315. $sql .= ' ORDER BY p.date_partnership_end DESC';
  316. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  317. $result = $this->db->query($sql);
  318. if ($result) {
  319. $obj = $this->db->fetch_object($result);
  320. if ($obj) {
  321. $this->id = $obj->rowid;
  322. $this->entity = $obj->entity;
  323. $this->ref = $obj->ref;
  324. $this->fk_type = $obj->fk_type;
  325. $this->type_code = $obj->type_code;
  326. $this->type_label = $obj->type_label;
  327. $this->fk_soc = $obj->fk_soc;
  328. $this->fk_member = $obj->fk_member;
  329. $this->status = $obj->status;
  330. $this->date_partnership_start = $this->db->jdate($obj->date_partnership_start);
  331. $this->date_partnership_end = $this->db->jdate($obj->date_partnership_end);
  332. $this->date_creation = $this->db->jdate($obj->date_creation);
  333. $this->fk_user_creat = $obj->fk_user_creat;
  334. $this->tms = $obj->tms;
  335. $this->fk_user_modif = $obj->fk_user_modif;
  336. $this->note_private = $obj->note_private;
  337. $this->note_public = $obj->note_public;
  338. $this->last_main_doc = $obj->last_main_doc;
  339. $this->count_last_url_check_error = $obj->count_last_url_check_error;
  340. $this->last_check_backlink = $this->db->jdate($obj->last_check_backlink);
  341. $this->reason_decline_or_cancel = $obj->reason_decline_or_cancel;
  342. $this->import_key = $obj->import_key;
  343. $this->model_pdf = $obj->model_pdf;
  344. $this->url_to_check = $obj->url_to_check;
  345. // Retrieve all extrafield
  346. // fetch optionals attributes and labels
  347. $this->fetch_optionals();
  348. $this->db->free($result);
  349. return 1;
  350. } else {
  351. // $this->error = 'Partnership with id '.$id.' not found sql='.$sql;
  352. return 0;
  353. }
  354. } else {
  355. $this->error = $this->db->error();
  356. return -1;
  357. }
  358. }
  359. /**
  360. * Load object lines in memory from the database
  361. *
  362. * @return int Return integer <0 if KO, 0 if not found, >0 if OK
  363. */
  364. public function fetchLines()
  365. {
  366. $this->lines = array();
  367. $result = $this->fetchLinesCommon();
  368. return $result;
  369. }
  370. /**
  371. * Load list of objects in memory from the database.
  372. *
  373. * @param string $sortorder Sort Order
  374. * @param string $sortfield Sort field
  375. * @param int $limit limit
  376. * @param int $offset Offset
  377. * @param array $filter Filter array. Example array('field'=>'valueforlike', 'customurl'=>...)
  378. * @param string $filtermode Filter mode (AND or OR)
  379. * @return array|int int <0 if KO, array of pages if OK
  380. */
  381. public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset = 0, array $filter = array(), $filtermode = 'AND')
  382. {
  383. dol_syslog(__METHOD__, LOG_DEBUG);
  384. $records = array();
  385. $sql = 'SELECT ';
  386. $sql .= $this->getFieldList('t');
  387. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  388. if (isset($this->ismultientitymanaged) && $this->ismultientitymanaged == 1) {
  389. $sql .= ' WHERE t.entity IN ('.getEntity($this->element).')';
  390. } else {
  391. $sql .= ' WHERE 1 = 1';
  392. }
  393. // Manage filter
  394. $sqlwhere = array();
  395. if (count($filter) > 0) {
  396. foreach ($filter as $key => $value) {
  397. if ($key == 't.rowid') {
  398. $sqlwhere[] = $key." = ".((int) $value);
  399. } elseif (array_key_exists($key, $this->fields) && in_array($this->fields[$key]['type'], array('date', 'datetime', 'timestamp'))) {
  400. $sqlwhere[] = $key." = '".$this->db->idate($value)."'";
  401. } elseif ($key == 'customsql') {
  402. $sqlwhere[] = $value;
  403. } elseif (strpos($value, '%') === false) {
  404. $sqlwhere[] = $key." IN (".$this->db->sanitize($this->db->escape($value)).")";
  405. } else {
  406. $sqlwhere[] = $key." LIKE '%".$this->db->escape($value)."%'";
  407. }
  408. }
  409. }
  410. if (count($sqlwhere) > 0) {
  411. $sql .= ' AND ('.implode(' '.$this->db->escape($filtermode).' ', $sqlwhere).')';
  412. }
  413. if (!empty($sortfield)) {
  414. $sql .= $this->db->order($sortfield, $sortorder);
  415. }
  416. if (!empty($limit)) {
  417. $sql .= $this->db->plimit($limit, $offset);
  418. }
  419. $resql = $this->db->query($sql);
  420. if ($resql) {
  421. $num = $this->db->num_rows($resql);
  422. $i = 0;
  423. while ($i < ($limit ? min($limit, $num) : $num)) {
  424. $obj = $this->db->fetch_object($resql);
  425. $record = new self($this->db);
  426. $record->setVarsFromFetchObj($obj);
  427. $records[$record->id] = $record;
  428. $i++;
  429. }
  430. $this->db->free($resql);
  431. return $records;
  432. } else {
  433. $this->errors[] = 'Error '.$this->db->lasterror();
  434. dol_syslog(__METHOD__.' '.join(',', $this->errors), LOG_ERR);
  435. return -1;
  436. }
  437. }
  438. /**
  439. * Update object into database
  440. *
  441. * @param User $user User that modifies
  442. * @param bool $notrigger false=launch triggers after, true=disable triggers
  443. * @return int Return integer <0 if KO, >0 if OK
  444. */
  445. public function update(User $user, $notrigger = false)
  446. {
  447. if ($this->fk_soc <= 0 && $this->fk_member <= 0) {
  448. $this->error[] = "ErrorThirpdartyOrMemberidIsMandatory";
  449. return -1;
  450. }
  451. if (empty($this->fk_user_creat)) { // For the case the object was created with empty user (from public page).
  452. $this->fk_user_creat = $user->id;
  453. }
  454. return $this->updateCommon($user, $notrigger);
  455. }
  456. /**
  457. * Delete object in database
  458. *
  459. * @param User $user User that deletes
  460. * @param bool $notrigger false=launch triggers after, true=disable triggers
  461. * @return int Return integer <0 if KO, >0 if OK
  462. */
  463. public function delete(User $user, $notrigger = false)
  464. {
  465. return $this->deleteCommon($user, $notrigger);
  466. //return $this->deleteCommon($user, $notrigger, 1);
  467. }
  468. /**
  469. * Delete a line of object in database
  470. *
  471. * @param User $user User that delete
  472. * @param int $idline Id of line to delete
  473. * @param bool $notrigger false=launch triggers after, true=disable triggers
  474. * @return int >0 if OK, <0 if KO
  475. */
  476. public function deleteLine(User $user, $idline, $notrigger = false)
  477. {
  478. if ($this->status < 0) {
  479. $this->error = 'ErrorDeleteLineNotAllowedByObjectStatus';
  480. return -2;
  481. }
  482. return $this->deleteLineCommon($user, $idline, $notrigger);
  483. }
  484. /**
  485. * Validate object
  486. *
  487. * @param User $user User making status change
  488. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  489. * @return int Return integer <=0 if OK, 0=Nothing done, >0 if KO
  490. */
  491. public function validate($user, $notrigger = 0)
  492. {
  493. global $conf;
  494. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  495. $error = 0;
  496. // Protection
  497. if ($this->status == self::STATUS_VALIDATED) {
  498. dol_syslog(get_class($this)."::validate action abandonned: already validated", LOG_WARNING);
  499. return 0;
  500. }
  501. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->write))
  502. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->partnership_advance->validate))))
  503. {
  504. $this->error='NotEnoughPermissions';
  505. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  506. return -1;
  507. }*/
  508. $now = dol_now();
  509. $this->db->begin();
  510. // Define new ref
  511. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  512. $num = $this->getNextNumRef();
  513. } else {
  514. $num = $this->ref;
  515. }
  516. $this->newref = $num;
  517. if (!empty($num)) {
  518. // Validate
  519. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  520. $sql .= " SET ref = '".$this->db->escape($num)."',";
  521. $sql .= " status = ".self::STATUS_VALIDATED;
  522. if (!empty($this->fields['date_validation'])) {
  523. $sql .= ", date_validation = '".$this->db->idate($now)."'";
  524. }
  525. if (!empty($this->fields['fk_user_valid'])) {
  526. $sql .= ", fk_user_valid = ".$user->id;
  527. }
  528. $sql .= " WHERE rowid = ".((int) $this->id);
  529. dol_syslog(get_class($this)."::validate()", LOG_DEBUG);
  530. $resql = $this->db->query($sql);
  531. if (!$resql) {
  532. dol_print_error($this->db);
  533. $this->error = $this->db->lasterror();
  534. $error++;
  535. }
  536. if (!$error && !$notrigger) {
  537. // Call trigger
  538. $result = $this->call_trigger('PARTNERSHIP_VALIDATE', $user);
  539. if ($result < 0) {
  540. $error++;
  541. }
  542. // End call triggers
  543. }
  544. }
  545. if (!$error) {
  546. $this->oldref = $this->ref;
  547. // Rename directory if dir was a temporary ref
  548. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  549. // Now we rename also files into index
  550. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'partnership/".$this->db->escape($this->newref)."'";
  551. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'partnership/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  552. $resql = $this->db->query($sql);
  553. if (!$resql) {
  554. $error++;
  555. $this->error = $this->db->lasterror();
  556. }
  557. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'partnership/".$this->db->escape($this->newref)."'";
  558. $sql .= " WHERE filepath = 'partnership/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  559. $resql = $this->db->query($sql);
  560. if (!$resql) {
  561. $error++;
  562. $this->error = $this->db->lasterror();
  563. }
  564. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  565. $oldref = dol_sanitizeFileName($this->ref);
  566. $newref = dol_sanitizeFileName($num);
  567. $dirsource = $conf->partnership->dir_output.'/partnership/'.$oldref;
  568. $dirdest = $conf->partnership->dir_output.'/partnership/'.$newref;
  569. if (!$error && file_exists($dirsource)) {
  570. dol_syslog(get_class($this)."::validate() rename dir ".$dirsource." into ".$dirdest);
  571. if (@rename($dirsource, $dirdest)) {
  572. dol_syslog("Rename ok");
  573. // Rename docs starting with $oldref with $newref
  574. $listoffiles = dol_dir_list($conf->partnership->dir_output.'/partnership/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  575. foreach ($listoffiles as $fileentry) {
  576. $dirsource = $fileentry['name'];
  577. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  578. $dirsource = $fileentry['path'].'/'.$dirsource;
  579. $dirdest = $fileentry['path'].'/'.$dirdest;
  580. @rename($dirsource, $dirdest);
  581. }
  582. }
  583. }
  584. }
  585. }
  586. // Set new ref and current status
  587. if (!$error) {
  588. $this->ref = $num;
  589. $this->status = self::STATUS_VALIDATED;
  590. }
  591. if (!$error) {
  592. $this->db->commit();
  593. return 1;
  594. } else {
  595. $this->db->rollback();
  596. return -1;
  597. }
  598. }
  599. /**
  600. * Approve object
  601. *
  602. * @param User $user User making status change
  603. * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
  604. * @return int Return integer <=0 if OK, 0=Nothing done, >0 if KO
  605. */
  606. public function approve($user, $notrigger = 0)
  607. {
  608. global $conf;
  609. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  610. $error = 0;
  611. // Protection
  612. if ($this->status == self::STATUS_APPROVED) {
  613. dol_syslog(get_class($this)."::accept action abandonned: already acceptd", LOG_WARNING);
  614. return 0;
  615. }
  616. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->write))
  617. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->partnership_advance->accept))))
  618. {
  619. $this->error='NotEnoughPermissions';
  620. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  621. return -1;
  622. }*/
  623. $now = dol_now();
  624. $this->db->begin();
  625. // Define new ref
  626. if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref) || empty($this->ref))) { // empty should not happened, but when it occurs, the test save life
  627. $num = $this->getNextNumRef();
  628. } else {
  629. $num = $this->ref;
  630. }
  631. $this->newref = $num;
  632. if (!empty($num)) {
  633. // Accept
  634. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element;
  635. $sql .= " SET ref = '".$this->db->escape($num)."',";
  636. $sql .= " status = ".self::STATUS_APPROVED;
  637. // if (!empty($this->fields['date_validation'])) {
  638. // $sql .= ", date_validation = '".$this->db->idate($now)."'";
  639. // }
  640. // if (!empty($this->fields['fk_user_valid'])) {
  641. // $sql .= ", fk_user_valid = ".$user->id;
  642. // }
  643. $sql .= " WHERE rowid = ".((int) $this->id);
  644. dol_syslog(get_class($this)."::accept()", LOG_DEBUG);
  645. $resql = $this->db->query($sql);
  646. if (!$resql) {
  647. dol_print_error($this->db);
  648. $this->error = $this->db->lasterror();
  649. $error++;
  650. }
  651. if (!$error && !$notrigger) {
  652. // Call trigger
  653. $result = $this->call_trigger('PARTNERSHIP_ACCEPT', $user);
  654. if ($result < 0) {
  655. $error++;
  656. }
  657. // End call triggers
  658. }
  659. }
  660. if (!$error) {
  661. $this->oldref = $this->ref;
  662. // Rename directory if dir was a temporary ref
  663. if (preg_match('/^[\(]?PROV/i', $this->ref)) {
  664. // Now we rename also files into index
  665. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filename = CONCAT('".$this->db->escape($this->newref)."', SUBSTR(filename, ".(strlen($this->ref) + 1).")), filepath = 'partnership/".$this->db->escape($this->newref)."'";
  666. $sql .= " WHERE filename LIKE '".$this->db->escape($this->ref)."%' AND filepath = 'partnership/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  667. $resql = $this->db->query($sql);
  668. if (!$resql) {
  669. $error++;
  670. $this->error = $this->db->lasterror();
  671. }
  672. $sql = 'UPDATE '.MAIN_DB_PREFIX."ecm_files set filepath = 'partnership/".$this->db->escape($this->newref)."'";
  673. $sql .= " WHERE filepath = 'partnership/".$this->db->escape($this->ref)."' and entity = ".$conf->entity;
  674. $resql = $this->db->query($sql);
  675. if (!$resql) {
  676. $error++;
  677. $this->error = $this->db->lasterror();
  678. }
  679. // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
  680. $oldref = dol_sanitizeFileName($this->ref);
  681. $newref = dol_sanitizeFileName($num);
  682. $dirsource = $conf->partnership->dir_output.'/partnership/'.$oldref;
  683. $dirdest = $conf->partnership->dir_output.'/partnership/'.$newref;
  684. if (!$error && file_exists($dirsource)) {
  685. dol_syslog(get_class($this)."::accept() rename dir ".$dirsource." into ".$dirdest);
  686. if (@rename($dirsource, $dirdest)) {
  687. dol_syslog("Rename ok");
  688. // Rename docs starting with $oldref with $newref
  689. $listoffiles = dol_dir_list($conf->partnership->dir_output.'/partnership/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
  690. foreach ($listoffiles as $fileentry) {
  691. $dirsource = $fileentry['name'];
  692. $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
  693. $dirsource = $fileentry['path'].'/'.$dirsource;
  694. $dirdest = $fileentry['path'].'/'.$dirdest;
  695. @rename($dirsource, $dirdest);
  696. }
  697. }
  698. }
  699. }
  700. }
  701. // Set new ref and current status
  702. if (!$error) {
  703. $this->ref = $num;
  704. $this->status = self::STATUS_APPROVED;
  705. }
  706. if (!$error) {
  707. $this->db->commit();
  708. return 1;
  709. } else {
  710. $this->db->rollback();
  711. return -1;
  712. }
  713. }
  714. /**
  715. * Set draft status
  716. *
  717. * @param User $user Object user that modify
  718. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  719. * @return int Return integer <0 if KO, >0 if OK
  720. */
  721. public function setDraft($user, $notrigger = 0)
  722. {
  723. // Protection
  724. if ($this->status <= self::STATUS_DRAFT) {
  725. return 0;
  726. }
  727. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->write))
  728. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership_advance->validate))))
  729. {
  730. $this->error='Permission denied';
  731. return -1;
  732. }*/
  733. return $this->setStatusCommon($user, self::STATUS_DRAFT, $notrigger, 'PARTNERSHIP_UNVALIDATE');
  734. }
  735. /**
  736. * Set refused status
  737. *
  738. * @param User $user Object user that modify
  739. * @param string $reasondeclinenote Reason decline
  740. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  741. * @return int Return integer <0 if KO, 0=Nothing done, >0 if OK
  742. */
  743. public function refused($user, $reasondeclinenote = '', $notrigger = 0)
  744. {
  745. // Protection
  746. if ($this->status == self::STATUS_REFUSED) {
  747. return 0;
  748. }
  749. $this->status = self::STATUS_REFUSED;
  750. $this->reason_decline_or_cancel = $reasondeclinenote;
  751. $result = $this->update($user);
  752. if ($result) {
  753. $this->reason_decline_or_cancel = $reasondeclinenote;
  754. return 1;
  755. }
  756. return -1;
  757. }
  758. /**
  759. * Set cancel status
  760. *
  761. * @param User $user Object user that modify
  762. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  763. * @return int Return integer <0 if KO, 0=Nothing done, >0 if OK
  764. */
  765. public function cancel($user, $notrigger = 0)
  766. {
  767. // Protection
  768. if ($this->status != self::STATUS_APPROVED) {
  769. return 0;
  770. }
  771. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->write))
  772. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership_advance->validate))))
  773. {
  774. $this->error='Permission denied';
  775. return -1;
  776. }*/
  777. return $this->setStatusCommon($user, self::STATUS_CANCELED, $notrigger, 'PARTNERSHIP_CANCEL');
  778. }
  779. /**
  780. * Set back to validated status
  781. *
  782. * @param User $user Object user that modify
  783. * @param int $notrigger 1=Does not execute triggers, 0=Execute triggers
  784. * @return int Return integer <0 if KO, 0=Nothing done, >0 if OK
  785. */
  786. public function reopen($user, $notrigger = 0)
  787. {
  788. // Protection
  789. if ($this->status != self::STATUS_CANCELED && $this->status != self::STATUS_REFUSED) {
  790. return 0;
  791. }
  792. /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership->write))
  793. || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->partnership_advance->validate))))
  794. {
  795. $this->error='Permission denied';
  796. return -1;
  797. }*/
  798. return $this->setStatusCommon($user, self::STATUS_APPROVED, $notrigger, 'PARTNERSHIP_REOPEN');
  799. }
  800. /**
  801. * getTooltipContentArray
  802. *
  803. * @param array $params ex option, infologin
  804. * @since v18
  805. * @return array
  806. */
  807. public function getTooltipContentArray($params)
  808. {
  809. global $langs;
  810. $langs->load('partnership');
  811. $datas = [];
  812. $datas['picto'] = img_picto('', $this->picto).' <u>'.$langs->trans("Partnership").'</u>';
  813. if (isset($this->status)) {
  814. $datas['picto'] .= ' '.$this->getLibStatut(5);
  815. }
  816. $datas['ref'] = '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
  817. return $datas;
  818. }
  819. /**
  820. * Return a link to the object card (with optionaly the picto)
  821. *
  822. * @param int $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
  823. * @param string $option On what the link point to ('nolink', ...)
  824. * @param int $notooltip 1=Disable tooltip
  825. * @param string $morecss Add more css on link
  826. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  827. * @return string String with URL
  828. */
  829. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  830. {
  831. global $conf, $langs, $hookmanager;
  832. if (!empty($conf->dol_no_mouse_hover)) {
  833. $notooltip = 1; // Force disable tooltips
  834. }
  835. $result = '';
  836. $params = [
  837. 'id' => $this->id,
  838. 'objecttype' => $this->element,
  839. 'option' => $option,
  840. ];
  841. $classfortooltip = 'classfortooltip';
  842. $dataparams = '';
  843. if (getDolGlobalInt('MAIN_ENABLE_AJAX_TOOLTIP')) {
  844. $classfortooltip = 'classforajaxtooltip';
  845. $dataparams = ' data-params="'.dol_escape_htmltag(json_encode($params)).'"';
  846. $label = '';
  847. } else {
  848. $label = implode($this->getTooltipContentArray($params));
  849. }
  850. $url = DOL_URL_ROOT.'/partnership/partnership_card.php?id='.$this->id;
  851. if ($option != 'nolink') {
  852. // Add param to save lastsearch_values or not
  853. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  854. if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  855. $add_save_lastsearch_values = 1;
  856. }
  857. if ($add_save_lastsearch_values) {
  858. $url .= '&save_lastsearch_values=1';
  859. }
  860. }
  861. $linkclose = '';
  862. if (empty($notooltip)) {
  863. if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
  864. $label = $langs->trans("ShowPartnership");
  865. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  866. }
  867. $linkclose .= ($label ? ' title="'.dol_escape_htmltag($label, 1).'"' : ' title="tocomplete"');
  868. $linkclose .= $dataparams.' class="'.$classfortooltip.($morecss ? ' '.$morecss : '').'"';
  869. } else {
  870. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  871. }
  872. if ($option == 'nolink') {
  873. $linkstart = '<span';
  874. } else {
  875. $linkstart = '<a href="'.$url.'"';
  876. }
  877. $linkstart .= $linkclose.'>';
  878. if ($option == 'nolink') {
  879. $linkend = '</span>';
  880. } else {
  881. $linkend = '</a>';
  882. }
  883. $result .= $linkstart;
  884. if (empty($this->showphoto_on_popup)) {
  885. if ($withpicto) {
  886. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : $dataparams.' class="'.(($withpicto != 2) ? 'paddingright ' : '').$classfortooltip.'"'), 0, 0, $notooltip ? 0 : 1);
  887. }
  888. } else {
  889. if ($withpicto) {
  890. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  891. list($class, $module) = explode('@', $this->picto);
  892. $upload_dir = $conf->$module->multidir_output[$conf->entity]."/$class/".dol_sanitizeFileName($this->ref);
  893. $filearray = dol_dir_list($upload_dir, "files");
  894. $filename = $filearray[0]['name'];
  895. if (!empty($filename)) {
  896. $pospoint = strpos($filearray[0]['name'], '.');
  897. $pathtophoto = $class.'/'.$this->ref.'/thumbs/'.substr($filename, 0, $pospoint).'_mini'.substr($filename, $pospoint);
  898. if (!getDolGlobalString(strtoupper($module.'_'.$class).'_FORMATLISTPHOTOSASUSERS')) {
  899. $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><div class="photoref"><img class="photo'.$module.'" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div></div>';
  900. } else {
  901. $result .= '<div class="floatleft inline-block valignmiddle divphotoref"><img class="photouserphoto userphoto" alt="No photo" border="0" src="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$module.'&entity='.$conf->entity.'&file='.urlencode($pathtophoto).'"></div>';
  902. }
  903. $result .= '</div>';
  904. } else {
  905. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  906. }
  907. }
  908. }
  909. if ($withpicto != 2) {
  910. $result .= $this->ref;
  911. }
  912. $result .= $linkend;
  913. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  914. global $action, $hookmanager;
  915. $hookmanager->initHooks(array('partnershipdao'));
  916. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  917. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  918. if ($reshook > 0) {
  919. $result = $hookmanager->resPrint;
  920. } else {
  921. $result .= $hookmanager->resPrint;
  922. }
  923. return $result;
  924. }
  925. /**
  926. * Return the label of the status
  927. *
  928. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  929. * @return string Label of status
  930. */
  931. public function getLibStatut($mode = 0)
  932. {
  933. return $this->LibStatut($this->status, $mode);
  934. }
  935. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  936. /**
  937. * Return the status
  938. *
  939. * @param int $status Id status
  940. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  941. * @return string Label of status
  942. */
  943. public function LibStatut($status, $mode = 0)
  944. {
  945. // phpcs:enable
  946. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  947. global $langs;
  948. //$langs->load("partnership");
  949. $this->labelStatus[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  950. $this->labelStatus[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  951. $this->labelStatus[self::STATUS_APPROVED] = $langs->transnoentitiesnoconv('Approved');
  952. $this->labelStatus[self::STATUS_REFUSED] = $langs->transnoentitiesnoconv('Refused');
  953. $this->labelStatus[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Terminated');
  954. $this->labelStatusShort[self::STATUS_DRAFT] = $langs->transnoentitiesnoconv('Draft');
  955. $this->labelStatusShort[self::STATUS_VALIDATED] = $langs->transnoentitiesnoconv('Validated');
  956. $this->labelStatusShort[self::STATUS_APPROVED] = $langs->transnoentitiesnoconv('Approved');
  957. $this->labelStatusShort[self::STATUS_REFUSED] = $langs->transnoentitiesnoconv('Refused');
  958. $this->labelStatusShort[self::STATUS_CANCELED] = $langs->transnoentitiesnoconv('Terminated');
  959. }
  960. $statusType = 'status'.$status;
  961. if ($status == self::STATUS_APPROVED) {
  962. $statusType = 'status4';
  963. }
  964. if ($status == self::STATUS_REFUSED) {
  965. $statusType = 'status9';
  966. }
  967. if ($status == self::STATUS_CANCELED) {
  968. $statusType = 'status6';
  969. }
  970. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  971. }
  972. /**
  973. * Load the info information in the object
  974. *
  975. * @param int $id Id of object
  976. * @return void
  977. */
  978. public function info($id)
  979. {
  980. $sql = 'SELECT rowid, date_creation as datec, tms as datem,';
  981. $sql .= ' fk_user_creat, fk_user_modif';
  982. $sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as t';
  983. $sql .= ' WHERE t.rowid = '.((int) $id);
  984. $result = $this->db->query($sql);
  985. if ($result) {
  986. if ($this->db->num_rows($result)) {
  987. $obj = $this->db->fetch_object($result);
  988. $this->id = $obj->rowid;
  989. $this->user_creation_id = $obj->fk_user_creat;
  990. $this->user_modification_id = $obj->fk_user_modif;
  991. $this->date_creation = $this->db->jdate($obj->datec);
  992. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  993. }
  994. $this->db->free($result);
  995. } else {
  996. dol_print_error($this->db);
  997. }
  998. }
  999. /**
  1000. * Initialise object with example values
  1001. * Id must be 0 if object instance is a specimen
  1002. *
  1003. * @return void
  1004. */
  1005. public function initAsSpecimen()
  1006. {
  1007. $this->initAsSpecimenCommon();
  1008. }
  1009. /**
  1010. * Create an array of lines
  1011. *
  1012. * @return array|int array of lines if OK, <0 if KO
  1013. */
  1014. public function getLinesArray()
  1015. {
  1016. $this->lines = array();
  1017. $objectline = new PartnershipLine($this->db);
  1018. $result = $objectline->fetchAll('ASC', 'position', 0, 0, array('customsql'=>'fk_partnership = '.((int) $this->id)));
  1019. if (is_numeric($result)) {
  1020. $this->error = $objectline->error;
  1021. $this->errors = $objectline->errors;
  1022. return $result;
  1023. } else {
  1024. $this->lines = $result;
  1025. return $this->lines;
  1026. }
  1027. }
  1028. /**
  1029. * Returns the reference to the following non used object depending on the active numbering module.
  1030. *
  1031. * @return string Object free reference
  1032. */
  1033. public function getNextNumRef()
  1034. {
  1035. global $langs, $conf;
  1036. $langs->load("partnership");
  1037. if (!getDolGlobalString('PARTNERSHIP_ADDON')) {
  1038. $conf->global->PARTNERSHIP_ADDON = 'mod_partnership_standard';
  1039. }
  1040. if (getDolGlobalString('PARTNERSHIP_ADDON')) {
  1041. $mybool = false;
  1042. $file = getDolGlobalString('PARTNERSHIP_ADDON') . ".php";
  1043. $classname = $conf->global->PARTNERSHIP_ADDON;
  1044. // Include file with class
  1045. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  1046. foreach ($dirmodels as $reldir) {
  1047. $dir = dol_buildpath($reldir."core/modules/partnership/");
  1048. // Load file with numbering class (if found)
  1049. $mybool |= @include_once $dir.$file;
  1050. }
  1051. if ($mybool === false) {
  1052. dol_print_error('', "Failed to include file ".$file);
  1053. return '';
  1054. }
  1055. if (class_exists($classname)) {
  1056. $obj = new $classname();
  1057. $numref = $obj->getNextValue($this);
  1058. if ($numref != '' && $numref != '-1') {
  1059. return $numref;
  1060. } else {
  1061. $this->error = $obj->error;
  1062. //dol_print_error($this->db,get_class($this)."::getNextNumRef ".$obj->error);
  1063. return "";
  1064. }
  1065. } else {
  1066. print $langs->trans("Error")." ".$langs->trans("ClassNotFound").' '.$classname;
  1067. return "";
  1068. }
  1069. } else {
  1070. print $langs->trans("ErrorNumberingModuleNotSetup", $this->element);
  1071. return "";
  1072. }
  1073. }
  1074. /**
  1075. * Create a document onto disk according to template module.
  1076. *
  1077. * @param string $modele Force template to use ('' to not force)
  1078. * @param Translate $outputlangs objet lang a utiliser pour traduction
  1079. * @param int $hidedetails Hide details of lines
  1080. * @param int $hidedesc Hide description
  1081. * @param int $hideref Hide ref
  1082. * @param null|array $moreparams Array to provide more information
  1083. * @return int 0 if KO, 1 if OK
  1084. */
  1085. public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams = null)
  1086. {
  1087. global $conf, $langs;
  1088. $result = 0;
  1089. $includedocgeneration = 0;
  1090. $langs->load("partnership");
  1091. if (!dol_strlen($modele)) {
  1092. $modele = 'standard_partnership';
  1093. if (!empty($this->model_pdf)) {
  1094. $modele = $this->model_pdf;
  1095. } elseif (getDolGlobalString('PARTNERSHIP_ADDON_PDF')) {
  1096. $modele = $conf->global->PARTNERSHIP_ADDON_PDF;
  1097. }
  1098. }
  1099. $modelpath = "core/modules/partnership/doc/";
  1100. if ($includedocgeneration && !empty($modele)) {
  1101. $result = $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  1102. }
  1103. return $result;
  1104. }
  1105. /**
  1106. * Action executed by scheduler
  1107. * CAN BE A CRON TASK. In such a case, parameters come from the schedule job setup field 'Parameters'
  1108. * Use public function doScheduledJob($param1, $param2, ...) to get parameters
  1109. *
  1110. * @return int 0 if OK, <>0 if KO (this function is used also by cron so only 0 is OK)
  1111. */
  1112. public function doScheduledJob()
  1113. {
  1114. //$conf->global->SYSLOG_FILE = 'DOL_DATA_ROOT/dolibarr_mydedicatedlofile.log';
  1115. $error = 0;
  1116. $this->output = '';
  1117. $this->error = '';
  1118. dol_syslog(__METHOD__, LOG_DEBUG);
  1119. //$now = dol_now();
  1120. $this->db->begin();
  1121. // ...
  1122. $this->db->commit();
  1123. return $error;
  1124. }
  1125. /**
  1126. * Return a thumb for kanban views
  1127. *
  1128. * @param string $option Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
  1129. * @param array $arraydata Array of data
  1130. * @return string HTML Code for Kanban thumb.
  1131. */
  1132. public function getKanbanView($option = '', $arraydata = null)
  1133. {
  1134. $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
  1135. $return = '<div class="box-flex-item box-flex-grow-zero">';
  1136. $return .= '<div class="info-box info-box-sm">';
  1137. $return .= '<span class="info-box-icon bg-infobox-action">';
  1138. $return .= img_picto('', $this->picto);
  1139. $return .= '</span>';
  1140. $return .= '<div class="info-box-content">';
  1141. $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl() : $this->ref).'</span>';
  1142. if ($selected >= 0) {
  1143. $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
  1144. }
  1145. if (property_exists($this, 'label')) {
  1146. $return .= ' <div class="inline-block opacitymedium valignmiddle tdoverflowmax100">'.$this->label.'</div>';
  1147. }
  1148. if (property_exists($this, 'thirdparty') && is_object($this->thirdparty)) {
  1149. $return .= '<br><div class="info-box-ref tdoverflowmax150">'.$this->thirdparty->getNomUrl(1).'</div>';
  1150. }
  1151. if (method_exists($this, 'getLibStatut')) {
  1152. $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
  1153. }
  1154. $return .= '</div>';
  1155. $return .= '</div>';
  1156. $return .= '</div>';
  1157. return $return;
  1158. }
  1159. }
  1160. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobjectline.class.php';
  1161. /**
  1162. * Class PartnershipLine. You can also remove this and generate a CRUD class for lines objects.
  1163. */
  1164. class PartnershipLine extends CommonObjectLine
  1165. {
  1166. // To complete with content of an object PartnershipLine
  1167. // We should have a field rowid, fk_partnership and position
  1168. /**
  1169. * @var int Does object support extrafields ? 0=No, 1=Yes
  1170. */
  1171. public $isextrafieldmanaged = 0;
  1172. /**
  1173. * Constructor
  1174. *
  1175. * @param DoliDb $db Database handler
  1176. */
  1177. public function __construct(DoliDB $db)
  1178. {
  1179. $this->db = $db;
  1180. }
  1181. }