partnership.class.php 46 KB

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