PageRenderTime 41ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/class/nosqlDocument.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 1796 lines | 1288 code | 180 blank | 328 comment | 159 complexity | c1e5aac7e7836ad3b570fd4831869850 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2011-2013 Herve Prot <herve.prot@symeos.com>
  3. * Copyright (C) 2012-2013 Regis Houssin <regis.houssin@capnetworks.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 <http://www.gnu.org/licenses/>.
  17. */
  18. require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
  19. /**
  20. * Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
  21. */
  22. abstract class nosqlDocument extends CommonObject {
  23. protected $couchdb; // TODO must to be private !!!!!
  24. public $id;
  25. public $error;
  26. public $errors;
  27. public $canvas; // Contains canvas name if it is
  28. public $fk_extrafields;
  29. public $no_save = array("no_save", "global", "token", "id", "fk_extrafields", "couchdb", "db",
  30. "error", "errors", "childtables", "table_element", "element", "fk_element", "ismultientitymanaged",
  31. "dbversion", "oldcopy", "state", "country", "status", "statut", "import_key", "couchAdmin",
  32. "all_permissions_are_loaded", "right", "type2label");
  33. /**
  34. * class constructor
  35. *
  36. * @param couchClient $db Database handler
  37. */
  38. function __construct($db = null) {
  39. $this->class = get_class($this);
  40. $this->db = $db;
  41. $this->useDatabase();
  42. }
  43. /**
  44. * load couchdb parameters
  45. * @param $dbname string name of database
  46. * @return int
  47. *
  48. */
  49. public function useDatabase($dbname = "") {
  50. global $conf, $couch;
  51. if (empty($this->couchdb) && is_object($couch)) {
  52. $this->couchdb = clone $couch;
  53. }
  54. if (!empty($dbname) && is_object($couch))
  55. $this->couchdb->useDatabase($dbname);
  56. //else
  57. // $this->couchdb->useDatabase($conf->Couchdb->name);
  58. }
  59. /**
  60. *
  61. * @param unknown $rowid
  62. * @return number
  63. */
  64. function fetch($rowid) { // old dolibarr rowid
  65. if (is_int($rowid)) {
  66. try {
  67. $result = $this->getView("rowid", array("key" => intval($rowid)));
  68. $this->load($result->rows[0]->value);
  69. } catch (Exception $e) {
  70. $this->error = "Fetch : Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  71. dol_print_error($this->db, $this->error);
  72. return 0;
  73. }
  74. } else {
  75. try {
  76. $this->load($rowid);
  77. } catch (Exception $e) {
  78. $this->error = "Fetch : Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  79. dol_print_error($this->db, $this->error);
  80. return 0;
  81. }
  82. }
  83. return 1;
  84. }
  85. /**
  86. *
  87. * @param unknown $id
  88. * @return number
  89. */
  90. function simpleFetch($id) {
  91. global $conf;
  92. // Clean parametersadmin
  93. $id = trim($id);
  94. try {
  95. $this->values = $this->couchdb->getDoc($id);
  96. } catch (Exception $e) {
  97. return 0;
  98. }
  99. return 1;
  100. }
  101. /**
  102. *
  103. * @param unknown $user
  104. * @return Ambigous <object, boolean, NULL, unknown, mixed, multitype:>
  105. */
  106. function update($user) {
  107. if ($this->id) // only update
  108. $this->UserUpdate = $user->login;
  109. else { // Create
  110. $this->UserCreate = $user->login;
  111. $this->UserUpdate = $user->login;
  112. }
  113. return $this->record();
  114. }
  115. /**
  116. * Set a value and modify type for couchdb
  117. * @param string $key
  118. * @param string $value
  119. */
  120. public function set($key, $value) {
  121. if (isset($this->fk_extrafields->fields->$key->settype))
  122. settype($value, $this->fk_extrafields->fields->$key->settype);
  123. $this->$key = $value;
  124. $params = new stdClass();
  125. $params->field = $key;
  126. $params->value = $value;
  127. return $this->couchdb->updateDoc(get_class($this), "in-place", $params, $this->id);
  128. }
  129. /**
  130. * Get a value from a key
  131. * @param string $key
  132. * @return value
  133. */
  134. public function get($key) {
  135. return $this->values->$key;
  136. }
  137. /**
  138. * load a $id document in values
  139. * @param $id
  140. * @return $value
  141. */
  142. public function load($id, $cache = false) {
  143. global $conf;
  144. require_once DOL_DOCUMENT_ROOT . '/core/lib/memory.lib.php';
  145. $values = array();
  146. $found = false;
  147. if ($cache && !empty($conf->memcached)) {
  148. $values = dol_getcache($id);
  149. if (is_object($values)) {
  150. $found = true;
  151. }
  152. }
  153. if (!$found) {
  154. $values = $this->couchdb->getDoc($id); // load extrafields for class
  155. if ($cache && !empty($conf->memcached)) {
  156. dol_setcache($id, $values);
  157. }
  158. }
  159. if (!empty($values)) {
  160. $this->id = $values->_id;
  161. foreach (get_object_vars($values) as $key => $aRow)
  162. $this->$key = $aRow;
  163. }
  164. return $values;
  165. }
  166. /**
  167. * save values object document
  168. *
  169. * @param bool $cache if true remove element from cache
  170. * @return object value of storeDoc
  171. */
  172. public function record($cache = false) {
  173. global $conf, $user;
  174. $values = new stdClass();
  175. foreach (get_object_vars($this) as $key => $aRow)
  176. if (!in_array($key, $this->no_save)) {
  177. $values->$key = $aRow;
  178. if (isset($this->fk_extrafields->fields->$key->settype) &&
  179. $this->fk_extrafields->fields->$key->settype != "date") // transtypage
  180. settype($values->$key, $this->fk_extrafields->fields->$key->settype);
  181. // If empty set default value
  182. if (empty($values->$key) && isset($this->fk_extrafields->fields->$key->default))
  183. $values->$key = $this->fk_extrafields->fields->$key->default;
  184. }
  185. if (empty($values->_id) && !empty($this->id))
  186. $values->_id = $this->id;
  187. $values->class = get_class($this);
  188. $values->tms = dol_now();
  189. // Specific for users
  190. if ($values->class == "User")
  191. unset($values->rights);
  192. else
  193. $values->entity = $conf->Couchdb->name;
  194. // Save Status and TMS for history
  195. if (isset($this->fk_extrafields) && $this->fk_extrafields->history) {
  196. if (!isset($values->history))
  197. $values->history = array();
  198. $history = new stdClass();
  199. $history->date = $values->tms;
  200. $history->author = new stdClass();
  201. $history->author->id = $user->id;
  202. $history->author->name = $user->name;
  203. $history->Status = $values->Status;
  204. $history->rev = $values->_rev;
  205. $values->history[] = $history;
  206. }
  207. try {
  208. $this->couchdb->clean($values);
  209. $result = $this->couchdb->storeDoc($values);
  210. $this->id = $result->id;
  211. $this->_id = $result->id;
  212. $this->_rev = $result->rev;
  213. $values->_id = $this->_id;
  214. $values->id = $this->_id;
  215. $values->_rev = $this->_rev;
  216. if ($cache && !empty($conf->memcached)) {
  217. dol_setcache($this->id, $values);
  218. }
  219. } catch (Exception $e) {
  220. dol_print_error("", $e->getMessage());
  221. exit;
  222. }
  223. return $result;
  224. }
  225. /**
  226. * save values objects documents
  227. * @param $obj array of objects
  228. * @return value of storeDoc
  229. */
  230. public function storeDocs($obj) {
  231. $this->couchdb->clean($obj);
  232. return $this->couchdb->storeDocs($obj);
  233. }
  234. /**
  235. * delete values objects documents
  236. * @param $obj array of objects
  237. * @return value of storeDoc
  238. */
  239. public function deleteDocs($obj) {
  240. return $this->couchdb->deleteDocs($obj);
  241. }
  242. /**
  243. * save values of one object documents
  244. * @param $obj object
  245. * @return value of storeDoc
  246. */
  247. public function getDoc($obj) {
  248. return $this->couchdb->getDoc($obj);
  249. }
  250. /**
  251. * save values of one object documents
  252. * @param $obj object
  253. * @return value of storeDoc
  254. */
  255. public function storeDoc($obj) {
  256. return $this->couchdb->storeDoc($obj);
  257. }
  258. /**
  259. * delete a object document
  260. * @param $obj object
  261. * @return value of storeDoc
  262. */
  263. public function deleteDoc($obj = null) {
  264. if (empty($obj)) {
  265. $obj = new stdClass();
  266. $obj->_id = $this->_id;
  267. $obj->_rev = $this->_rev;
  268. }
  269. return $this->couchdb->deleteDoc($obj);
  270. }
  271. /**
  272. * delete object
  273. */
  274. public function delete() {
  275. if (!empty($this->_id))
  276. $this->deleteDoc($this);
  277. }
  278. /**
  279. * store a file in document
  280. * @param $name Name of the variable
  281. * @return value of storeAttachment
  282. */
  283. public function storeFile($name = 'addedfile', $cache = false) {
  284. global $_FILES;
  285. $result = $this->couchdb->storeAttachment($this, $_FILES[$name]['tmp_name'], $_FILES[$name]['type'], $_FILES[$name]['name']);
  286. $this->_rev = $result->rev;
  287. if ($cache)
  288. dol_delcache($this->id);
  289. return 1;
  290. }
  291. /**
  292. * get an attachement file in base64
  293. * @param $filename Name of the file
  294. * @return value of storeAttachment
  295. */
  296. public function getFileBase64($filename) {
  297. return $this->couchdb->getAttachment($this, $filename);
  298. }
  299. /**
  300. * get URL a of file in document
  301. * @return value URL of storeAttachment
  302. */
  303. public function getFile($filename) {
  304. $url_server = "db/" . $this->couchdb->getDatabaseName();
  305. return $url_server . "/" . $this->id . "/" . $filename;
  306. }
  307. /**
  308. * delete a file in document
  309. * @param $filename name of the file
  310. * @return value of storeAttachment
  311. */
  312. public function deleteFile($filename, $cache = false) {
  313. $result = $this->couchdb->deleteAttachment($this, $filename);
  314. $this->_rev = $result->rev;
  315. if ($cache)
  316. dol_delcache($this->id);
  317. return 1;
  318. }
  319. /**
  320. * Return id of document
  321. * @return string
  322. */
  323. public function id() {
  324. return $this->id;
  325. }
  326. /** Call a view on couchdb
  327. *
  328. * @param $name string name of the view
  329. * @param $params array params ['group'],['level'],['key'],...
  330. * @param $cache bool load from cache
  331. * @return array
  332. */
  333. public function getView($name, $params = array(), $cache = false) {
  334. global $conf;
  335. $found = false;
  336. if ($cache) {
  337. $result = dol_getcache(get_class($this) . ":" . $name);
  338. if (is_object($result)) {
  339. $found = true;
  340. }
  341. }
  342. if (!$found) {
  343. $result = new stdClass();
  344. try {
  345. if (is_array($params))
  346. $this->couchdb->setQueryParameters($params);
  347. $result = $this->couchdb->getView(get_class($this), $name);
  348. if ($cache) {
  349. dol_setcache(get_class($this) . ":" . $name, $result);
  350. }
  351. } catch (Exception $e) {
  352. error_log($e->getMessage());
  353. $result->total_rows = 0;
  354. }
  355. }
  356. return $result;
  357. }
  358. /** Call an Indexed view with lucene on couchdb
  359. *
  360. * @param $name string name of the view
  361. * @param $params array params ['group'],['level'],['key'],...
  362. * @return array
  363. */
  364. public function getIndexedView($name, $params = array()) {
  365. global $conf;
  366. $result = new stdClass();
  367. try {
  368. /* if (!empty($conf->view_limit))
  369. $params['limit'] = $conf->global->MAIN_SIZE_LISTE_LIMIT; */
  370. //$params['limit'] = $conf->view_limit;
  371. $params['include_docs'] = true;
  372. $this->couchdb->setQueryParameters($params);
  373. $result = $this->couchdb->getIndexedView(get_class($this), $name);
  374. } catch (Exception $e) {
  375. error_log($e->getMessage());
  376. $result->total_rows = 0;
  377. }
  378. return $result;
  379. }
  380. /**
  381. * Return label of status (activity, closed)
  382. *
  383. * @return string Libelle
  384. */
  385. function getLibStatus($key = "Status") {
  386. return $this->LibStatus($this->$key, array("key" => $key));
  387. }
  388. /**
  389. * returns a/some universally unique identifier(s)
  390. *
  391. *
  392. * @param integer $count the number of uuids to return
  393. * @return array|false an array of uuids on success, false on failure.
  394. */
  395. public function getUuids($count) {
  396. return $this->couchdb->getUuids($count);
  397. }
  398. /**
  399. * Flush the cache
  400. * @param $id key to delete, nothing flush_all
  401. * @return string Libelle
  402. */
  403. function flush($id = '') {
  404. if (!empty($id))
  405. return dol_delcache($id);
  406. else
  407. return dol_flushcache();
  408. }
  409. /**
  410. * Renvoi le libelle d'un statut donne
  411. *
  412. * @param int $statut Id statut
  413. * @param date $expiration_date Automatic Status with an expiration date (expired or actived)
  414. * @return string Libelle du statut
  415. */
  416. function LibStatus($status, $params = array()) {
  417. global $langs, $conf;
  418. if (empty($params["key"]))
  419. $key = "Status";
  420. else
  421. $key = $params["key"];
  422. if (empty($status))
  423. $status = $this->fk_extrafields->fields->$key->default;
  424. if (isset($params["dateEnd"]) && isset($this->fk_extrafields->fields->$key->values->$status->dateEnd)) {
  425. if ($params["dateEnd"] < dol_now())
  426. $status = $this->fk_extrafields->fields->$key->values->$status->dateEnd[0];
  427. else
  428. $status = $this->fk_extrafields->fields->$key->values->$status->dateEnd[1];
  429. }
  430. if (isset($this->fk_extrafields->fields->$key->values->$status->label))
  431. $label = $langs->trans($this->fk_extrafields->fields->$key->values->$status->label);
  432. else
  433. $label = $langs->trans($status);
  434. if (isset($params["maxlen"]))
  435. $label = dol_trunc($label, $params["maxlen"]);
  436. if ($this->fk_extrafields->fields->$key->status) // Is a type status with defined color
  437. $color = $this->fk_extrafields->fields->$key->values->$status->cssClass;
  438. else
  439. $color = "anthracite-gradient";
  440. return '<small class="tag ' . $color . ' glossy">' . $label . '</small> ';
  441. }
  442. /**
  443. * For menu Add/Remove a datatable
  444. *
  445. * @param $ref_css name of #list
  446. * @return string
  447. */
  448. public function datatablesEdit($ref_css, $title = "") {
  449. global $langs, $user;
  450. $class = strtolower(get_class($this));
  451. if (!$user->rights->$class->edit && !$user->rights->$class->creer)
  452. return null;
  453. if (count($this->fk_extrafields->createList)) {
  454. print '<form id="' . $ref_css . '_formAddNewRow" class="block" title="' . $title . '">';
  455. //print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  456. print '<input type="hidden" name="json" id="json" value="add" />';
  457. print '<input type="hidden" name="class" id="class" value="' . get_class($this) . '" />';
  458. foreach ($this->fk_extrafields->createList as $aRow) {
  459. print '<p class="button-height block-label">';
  460. $label = $langs->trans($this->fk_extrafields->fields->$aRow->label);
  461. if (empty($label))
  462. $label = $langs->trans($aRow);
  463. print '<label for = "' . $aRow . '" class="label">' . $label . '</label>';
  464. print $this->select_fk_extrafields($aRow, $aRow, null, true, 40, "full-width");
  465. print '</p>';
  466. }
  467. print '</form>';
  468. if ($user->rights->$class->edit || $user->rights->$class->creer)
  469. print '<button id="' . $ref_css . '_btnAddNewRow">' . $langs->trans("Add") . '</button> ';
  470. }
  471. /* if ($user->rights->$class->delete)
  472. print '<button id="' . $ref_css . '_btnDeleteRow">' . $langs->trans("Delete") . '</button>'; */
  473. print '<p class="button-height "></p>';
  474. }
  475. /**
  476. * For Generate a datatable
  477. *
  478. * @param $obj object of aocolumns parameters
  479. * @param $ref_css name of #list
  480. * @return string
  481. */
  482. public function datatablesCreate($obj, $ref_css, $json = false, $ColSearch = false) {
  483. global $conf, $langs, $user;
  484. $class = strtolower(get_class($this));
  485. ?>
  486. <script type="text/javascript" charset="utf-8">
  487. $(document).ready(function() {
  488. var oTable = $('#<?php echo $ref_css ?>').dataTable({
  489. "aoColumns" : [
  490. <?php
  491. $nb = count($obj->aoColumns);
  492. foreach ($obj->aoColumns as $i => $aRow):
  493. ?>
  494. {
  495. <?php foreach ($aRow as $key => $fields): ?>
  496. <?php if ($key == "mDataProp" || $key == "sClass" || $key == "sDefaultContent" || $key == "sType" || $key == "sWidth") : ?>
  497. "<?php echo $key; ?>":"<?php echo $fields; ?>",
  498. <?php elseif ($key == "fnRender") : ?>
  499. "<?php echo $key; ?>": <?php echo $fields; ?>,
  500. <?php else : ?>
  501. "<?php echo $key; ?>": <?php echo ($fields ? "true" : "false"); ?>,
  502. <?php endif; ?>
  503. <?php
  504. endforeach;
  505. if ($nb - 1 == $i)
  506. echo "}";
  507. else
  508. echo"},";
  509. ?>
  510. <?php endforeach; ?>
  511. ],
  512. <?php if (!isset($obj->aaSorting)) : ?>
  513. "aaSorting" : [[1, "asc"]],
  514. <?php else : ?>
  515. "aaSorting" : <?php echo json_encode($obj->aaSorting); ?>,
  516. <?php endif; ?>
  517. <?php if ($json) : ?>
  518. <?php if (!empty($obj->sAjaxSource)): ?>
  519. "sAjaxSource": "<?php echo $obj->sAjaxSource; ?>",
  520. <?php else : ?>
  521. "sAjaxSource" : "<?php echo DOL_URL_ROOT . '/core/ajax/listdatatables.php'; ?>?json=list&bServerSide=<?php echo $obj->bServerSide; ?>&class=<?php echo get_class($this); ?>",
  522. <?php endif; ?>
  523. <?php endif; ?>
  524. <?php if (!empty($obj->iDisplayLength)): ?>
  525. "iDisplayLength": <?php echo $obj->iDisplayLength; ?>,
  526. <?php else : ?>
  527. "iDisplayLength": <?php echo (int) $conf->global->MAIN_SIZE_LISTE_LIMIT; ?>,
  528. <?php endif; ?>
  529. "aLengthMenu": [[5, 10, 25, 50, 100], [5, 10, 25, 50, 100]],
  530. "bProcessing": true,
  531. "bJQueryUI": true,
  532. "bAutoWidth": false,
  533. /*"sScrollY": "500px",
  534. "oScroller": {
  535. "loadingIndicator": true
  536. },*/
  537. <?php if ($obj->bServerSide) : ?>
  538. "bServerSide": true,
  539. <?php else : ?>
  540. "bServerSide": false,
  541. <?php endif; ?>
  542. "bDeferRender": true,
  543. "oLanguage": { "sUrl": "<?php echo DOL_URL_ROOT . '/core/js/datatables.js.php?lang=' . ($langs->defaultlang ? $langs->defaultlang : "en_US"); ?>"},
  544. /*$obj->sDom = '<\"top\"Tflpi<\"clear\">>rt<\"bottom\"pi<\"clear\">>';*/
  545. /*$obj->sPaginationType = 'full_numbers';*/
  546. /*$obj->sDom = 'TC<\"clear\">lfrtip';*/
  547. "oColVis": { "buttonText" : 'Voir/Cacher',
  548. "aiExclude": [0, 1] // Not cacheable _id and name
  549. },
  550. //$obj->oColVis->bRestore = true;
  551. //$obj->oColVis->sAlign = 'left';
  552. // Avec export Excel
  553. <?php if (!empty($obj->sDom)) : ?>
  554. //"sDom": "Cl<fr>t<\"clear\"rtip>",
  555. "sDom": "<?php echo $obj->sDom; ?>",
  556. <?php else : ?>
  557. //"sDom": "C<\"clear\"fr>lt<\"clear\"rtip>",
  558. "sDom": "<\"dataTables_header\"lfr>t<\"dataTables_footer\"ip>",
  559. //"sDom": "C<\"clear\"fr>tiS",
  560. //"sDom": "TC<\"clear\"fr>lt<\"clear\"rtip>",
  561. <?php endif; ?>
  562. // bottons
  563. <?php if ($obj->oTableTools->aButtons != null) : ?>
  564. "oTableTools" : { "aButtons": [
  565. <?php foreach ($obj->oTableTools->aButtons as $i => $aRow): ?>
  566. <?php if (is_array($aRow)): ?>
  567. {
  568. <?php foreach ($aRow as $key => $fields): ?>
  569. <?php if ($key == "fnClick" || $key == "fnAjaxComplete") : ?>
  570. "<?php echo $key; ?>": <?php echo $fields; ?>,
  571. <?php else : ?>
  572. "<?php echo $key; ?>":"<?php echo $fields; ?>",
  573. <?php endif; ?>
  574. <?php endforeach; ?>
  575. },
  576. <?php else : ?>
  577. {
  578. "sExtends": "<?php echo $aRow; ?>",
  579. "sFieldBoundary": '"',
  580. //"sFieldSeperator": "-",
  581. "sCharSet": "utf8",
  582. "sFileName": "export.csv",
  583. "bSelectedOnly": false
  584. },
  585. <?php endif; ?>
  586. <?php endforeach; ?>
  587. ],
  588. "sSwfPath": "<?php echo DOL_URL_ROOT . '/includes/jquery/plugins/datatables/extras/TableTools/media/swf/copy_csv_xls.swf'; ?>",
  589. "sRowSelect": "multi"
  590. },
  591. <?php endif; ?>
  592. <?php if (isset($obj->fnRowCallback)): ?>
  593. "fnRowCallback": <?php echo $obj->fnRowCallback; ?>,
  594. <?php endif; ?>
  595. <?php if (isset($obj->fnFooterCallback)): ?>
  596. "fnFooterCallback": <?php echo $obj->fnFooterCallback; ?>,
  597. <?php endif; ?>
  598. <?php if (!defined('NOLOGIN')) : ?>
  599. <?php if (isset($obj->fnDrawCallback)): ?>
  600. "fnDrawCallback": <?php echo $obj->fnDrawCallback; ?>,
  601. <?php endif; ?>
  602. <?php endif; ?>
  603. <?php if ($user->rights->$class->edit || $user->rights->$class->creer || $user->admin) : ?>
  604. }).makeEditable({
  605. sUpdateURL: urlSaveInPlace,
  606. sAddURL: urlAddInPlace,
  607. "aoColumns": [<?php
  608. $nb = count($obj->aoColumns);
  609. foreach ($obj->aoColumns as $i => $aRow) {
  610. $idx = $aRow->mDataProp;
  611. if (isset($aRow->bVisible) && $aRow->bVisible == false)
  612. continue;
  613. if (!empty($idx) && $aRow->editable && isset($this->fk_extrafields->fields->$idx->type)) {
  614. print "{";
  615. //print "event:'click',";
  616. print "indicator: indicatorInPlace,";
  617. print "tooltip: tooltipInPlace,";
  618. print "placeholder : '',";
  619. print "submit: submitInPlace,";
  620. print "onblur: 'cancel',";
  621. print "height: '14px',";
  622. //print 'ajaxoptions: {"dataType":"json"},';
  623. switch ($this->fk_extrafields->fields->$idx->type) {
  624. case "select" :
  625. print "type: 'select',";
  626. print "loadurl : urlLoadInPlace,";
  627. ?>loaddata: function (value, settings) {
  628. return {
  629. "id": oTable.fnGetData(this.parentNode, 0),
  630. "element_class" : "<?php echo get_class($this); ?>",
  631. "type":"select",
  632. "key": "editval_<?php echo $idx; ?>"
  633. };
  634. },<?php
  635. break;
  636. case "text":
  637. print "type: 'text',";
  638. break;
  639. case "date":
  640. print "type: 'datepicker',";
  641. print "cancel: cancelInPlace,";
  642. break;
  643. default :
  644. print "type: 'text',";
  645. break;
  646. }
  647. ?>submitdata: function (value, settings) {
  648. return { "id": oTable.fnGetData(this.parentNode, 0),
  649. "element_class" : "<?php echo get_class($this); ?>",
  650. "type": "<?php echo $this->fk_extrafields->fields->$idx->type; ?>",
  651. "key": "editval_<?php echo $idx; ?>"
  652. };
  653. },
  654. callback: function(sValue, y) {
  655. //var aPos = oTable.fnGetPosition( this );
  656. //oTable.fnAddData( sValue, aPos[0], aPos[1] ); // doesn't work with server-side
  657. //oTable.fnDraw();
  658. $(this).html(sValue);
  659. },<?php
  660. if (isset($this->fk_extrafields->fields->$idx->validate)) {
  661. print 'oValidationOptions : { rules:{ value: {';
  662. foreach ($this->fk_extrafields->fields->$idx->validate as $key => $value)
  663. if ($key != "cssclass")
  664. print $key . ":" . $value . ",";
  665. print '} } },';
  666. if (isset($this->fk_extrafields->fields->$idx->validate->cssclass))
  667. print 'cssclass: "' . $this->fk_extrafields->fields->$idx->validate->cssclass . '",';
  668. }
  669. print "},";
  670. }
  671. else
  672. print "null,";
  673. }
  674. ?>
  675. ],
  676. fnOnNewRowPosted: function(data) {
  677. var rtn = oTable.fnAddData(JSON.parse(data));
  678. return true;
  679. },
  680. fnOnAdding: function() {
  681. oTable.fnDraw(false);
  682. return true;
  683. },
  684. oAddNewRowOkButtonOptions: {
  685. label: "<?php echo $langs->trans("Create"); ?>",
  686. icons: { primary: 'ui-icon-check' },
  687. name: "action",
  688. value: "add-new"
  689. },
  690. oAddNewRowCancelButtonOptions: {
  691. label: "<?php echo $langs->trans("Undo"); ?>",
  692. class: "back-class",
  693. name: "action",
  694. value: "cancel-add",
  695. icons: { primary: 'ui-icon-close' }
  696. },
  697. oAddNewRowFormOptions: {
  698. show: "blind",
  699. hide: "blind"
  700. },
  701. sAddNewRowFormId: "<?php echo $ref_css ?>_formAddNewRow",
  702. sAddNewRowButtonId: "<?php echo $ref_css ?>_btnAddNewRow",
  703. sAddNewRowOkButtonId: "<?php echo $ref_css ?>_btnAddNewRowOk",
  704. sAddNewRowCancelButtonId: "<?php echo $ref_css ?>_btnAddNewRowCancel",
  705. sDeleteRowButtonId: "<?php echo $ref_css ?>_btnDeleteRow"
  706. <?php endif; ?>
  707. });
  708. <?php if ($ColSearch) : ?>
  709. $("tfoot input").keyup(function () {
  710. /* Filter on the column */
  711. var id = $(this).parent().attr("id");
  712. oTable.fnFilter(this.value, id);
  713. });
  714. /*send selected level value to server */
  715. $("tfoot #level").change(function () {
  716. /* Filter on the column */
  717. var id = $(this).parent().attr("id");
  718. var value = $(this).val();
  719. oTable.fnFilter(value, id);
  720. });
  721. /*send selected stcomm value to server */
  722. $("tfoot .flat").change(function () {
  723. /* Filter on the column */
  724. var id = $(this).parent().attr("id");
  725. var value = $(this).val();
  726. oTable.fnFilter(value, id);
  727. });
  728. <?php endif; ?>
  729. // Select_all
  730. $('.chSel_all').click(function () {
  731. $(this).closest('table').find('input[name=row_sel]').attr('checked', this.checked);
  732. });
  733. $("tbody tr td .delEnqBtn").live('click', function(){
  734. var aPos = oTable.fnGetPosition(this.parentNode);
  735. var aData = oTable.fnGetData(aPos[0]);
  736. if (aData["name"] === undefined)
  737. var text = aData["label"];
  738. else
  739. var text = aData["name"];
  740. var answer = confirm("<?php echo $langs->trans("Delete"); ?> '" + text + "' ?");
  741. if (answer) {
  742. $.ajax({
  743. type: "POST",
  744. url: "<?php echo DOL_URL_ROOT . '/core/ajax/deleteinplace.php'; ?>",
  745. data: "json=delete&class=<?php echo get_class($this); ?>&id=" + aData["_id"],
  746. success: function(msg){
  747. oTable.fnDeleteRow(aPos[0]);
  748. }
  749. });
  750. }
  751. return false;
  752. });
  753. });
  754. </script>
  755. <?php
  756. //$output.= "});"; // ATTENTION AUTOFILL NOT COMPATIBLE WITH COLVIS !!!!
  757. /* $output.= 'new AutoFill( oTable, {
  758. "aoColumnDefs": [
  759. {
  760. "bEnable":false,
  761. "aTargets": [ 0,1,2,3,5,6,8]
  762. },
  763. {
  764. "fnCallback": function ( ao ) {
  765. var n = document.getElementById(\'output\');
  766. for ( var i=0, iLen=ao.length ; i<iLen ; i++ ) {
  767. n.innerHTML += "Update: old value: {"+
  768. ao[i].oldValue+"} - new value: {"+ao[i].newValue+"}<br>";
  769. }
  770. n.scrollTop = n.scrollHeight;
  771. },
  772. "bEnable" : true,
  773. "aTargets": [ 4,7 ]
  774. }]
  775. } );'; */
  776. return;
  777. }
  778. /**
  779. * Contruct a HTML From for a fields
  780. *
  781. * @param array $aRow parameter of the field
  782. * @param string $key Name of the field
  783. * @param string $cssClass CSS Classe for the form
  784. * @return string
  785. */
  786. public function form($aRow, $key, $cssClass) {
  787. global $langs, $conf;
  788. $form = new Form($this->db);
  789. $rtr = "";
  790. if ($aRow->enable) {
  791. $rtr.= '<div class="formRow elVal">' . "\n";
  792. $label = $langs->transcountry($key, $this->Country);
  793. if (!$label)
  794. $label = $langs->trans($key);
  795. $rtr.= '<label for="' . $key . '">' . $label . '</label>' . "\n";
  796. switch ($aRow->type) {
  797. case "textarea" :
  798. $rtr.= '<textarea maxlength="' . $aRow->length . '" class="' . $cssClass . '" id="' . $key . '" name="' . $key . '" cols="1" rows="4">' . $this->$key . '</textarea>';
  799. $rtr.= '<script> $(document).ready(function() { $("#' . $key . '").counter({ goal: 120 });}); </script>';
  800. break;
  801. case "select" :
  802. if ($cssClass == "small")
  803. $style = "width:200px;";
  804. else
  805. $style = "width:400px;";
  806. $rtr.= '<select data-placeholder="' . $langs->trans($key) . '&hellip;" class="chzn-select expand" style="' . $style . '" id="' . $key . '" name="' . $key . '" >';
  807. if (isset($aRow->dict)) {
  808. require_once(DOL_DOCUMENT_ROOT . "/admin/class/dict.class.php");
  809. // load from dictionnary
  810. try {
  811. $dict = new Dict($this->db);
  812. $values = $dict->load($aRow->dict, true);
  813. //filter for country
  814. foreach ($values->values as $idx => $row) {
  815. if (empty($row->pays_code) || $this->Country == $row->pays_code)
  816. $aRow->values[$idx] = $row;
  817. }
  818. } catch (Exception $e) {
  819. dol_print_error('', $e->getMessage());
  820. }
  821. }
  822. if (empty($this->$key))
  823. $this->$key = $aRow->default;
  824. foreach ($aRow->values as $idx => $row) {
  825. if ($row->enable) {
  826. $rtr.= '<option value="' . $idx . '"';
  827. if ($this->$key == $idx)
  828. $rtr.= ' selected="selected"';
  829. $rtr.= '>';
  830. if (isset($row->label))
  831. $rtr.= $langs->trans($row->label);
  832. else
  833. $rtr.= $langs->trans($idx);
  834. $rtr.='</option>';
  835. }
  836. }
  837. $rtr.= '</select>';
  838. break;
  839. case "checkbox" :
  840. if (isset($this->$key))
  841. $value = $this->$key;
  842. else
  843. $value = $aRow->default;
  844. if ($value)
  845. $rtr.= '<input type="checkbox" id="' . $key . '" name="' . $key . '" checked="checked"/>';
  846. else
  847. $rtr.= '<input type="checkbox" id="' . $key . '" name="' . $key . '" />';
  848. break;
  849. case "uploadfile" :
  850. $rtr.= '<input type="file" class="flat" name="' . $key . '" id="' . $key . '">';
  851. break;
  852. default :
  853. if (isset($aRow->mask))
  854. $rtr.= '<input type="text" maxlength="' . $aRow->length . '" id="' . $key . '" name="' . $key . '" value="' . $this->$key . '" class="input-text ' . $aRow->css . " " . $cssClass . '" mask="' . $key . '"/>' . "\n";
  855. else
  856. $rtr.= '<input type="text" maxlength="' . $aRow->length . '" id="' . $key . '" name="' . $key . '" value="' . $this->$key . '" class="input-text ' . $aRow->css . " " . $cssClass . '"/>' . "\n";
  857. }
  858. $rtr.= '</div>' . "\n";
  859. }
  860. return $rtr;
  861. }
  862. /**
  863. * For Generate fnRender param for a datatable parameter
  864. *
  865. * @param $obj object of aocolumns parameters
  866. * @param $ref_css name of #list
  867. * @return string
  868. */
  869. public function datatablesFnRender($key, $type, $params = array()) {
  870. global $langs, $conf;
  871. switch ($type) {
  872. case "url":
  873. if (empty($params['url'])) // default url
  874. $url = strtolower(get_class($this)) . '/fiche.php?id=';
  875. else
  876. $url = $params['url'];
  877. if (empty($params['id']))
  878. $params['id'] = "_id";
  879. $rtr = 'function(obj) {
  880. var ar = [];
  881. ';
  882. if (strpos($key, ".") > 0)
  883. $rtr.='if(obj.aData.' . substr($key, 0, strpos($key, ".")) . '=== undefined)
  884. return ar.join("");
  885. if(obj.aData.' . $key . ' === undefined)
  886. if(obj.aData.' . $params["id"] . ' === undefined)
  887. return ar.join("");
  888. else
  889. obj.aData.' . $key . ' = obj.aData.' . $params["id"] . ';
  890. ';
  891. if (!empty($this->fk_extrafields->ico)) {
  892. $rtr.= 'ar[ar.length] = "<span class=\"' . $this->fk_extrafields->ico . '\" title=\"' . $langs->trans("See " . get_class($this)) . ' : " + obj.aData.' . (!empty($params['title'])?$params['title']:$key) . ' + "\">";';
  893. }
  894. $rtr.= 'if(obj.aData.' . $params["id"] . ' === undefined) {
  895. ar[ar.length] = "<span>";
  896. ar[ar.length] = obj.aData.' . $key . '.toString();
  897. ar[ar.length] = "</span>";
  898. } else {
  899. ar[ar.length] = "<a href=\"' . $url . '";
  900. ar[ar.length] = obj.aData.' . $params["id"] . ';
  901. ar[ar.length] = "\">";
  902. ar[ar.length] = obj.aData.' . $key . '.toString();
  903. ar[ar.length] = "</a>";
  904. }
  905. var str = ar.join("");
  906. return str;
  907. }';
  908. break;
  909. case "email":
  910. $rtr = 'function(obj) {
  911. var ar = [];
  912. if(obj.aData.' . $key . ' === undefined)
  913. return ar.join("");
  914. ar[ar.length] = "<a href=\"mailto:";
  915. ar[ar.length] = obj.aData.' . $key . '.toString();
  916. ar[ar.length] = "\">";
  917. ar[ar.length] = obj.aData.' . $key . '.toString();
  918. ar[ar.length] = "</a>";
  919. var str = ar.join("");
  920. return str;
  921. }';
  922. break;
  923. case "date":
  924. $rtr = 'function(obj) {
  925. if(obj.aData.' . $key . ')
  926. {
  927. var date = new Date(Date.parse(obj.aData.' . $key . '));
  928. return date.toLocaleDateString();
  929. }
  930. else
  931. return null;
  932. }';
  933. break;
  934. case "datetime" :
  935. $rtr = 'function(obj) {
  936. if(obj.aData.' . $key . ')
  937. {
  938. var date = new Date(obj.aData.' . $key . ');
  939. return date.toLocaleDateString() +" "+date.toLocaleTimeString();
  940. }
  941. else
  942. return null;
  943. }';
  944. break;
  945. case "status":
  946. $rtr = 'function(obj) {
  947. var now = Math.round(+new Date());
  948. var status = new Array();
  949. var expire = new Array();
  950. var statusDateEnd = "";
  951. var stat = obj.aData.' . $key . ';
  952. if(stat === undefined)
  953. stat = "' . $this->fk_extrafields->fields->$key->default . '";';
  954. if (!empty($this->fk_extrafields->fields->$key->values)) {
  955. foreach ($this->fk_extrafields->fields->$key->values as $key1 => $aRow) {
  956. if (isset($aRow->label))
  957. $rtr.= 'status["' . $key1 . '"]= new Array("' . $langs->trans($aRow->label) . '","' . $aRow->cssClass . '");';
  958. else
  959. $rtr.= 'status["' . $key1 . '"]= new Array("' . $langs->trans($key1) . '","' . $aRow->cssClass . '");';
  960. if (isset($aRow->dateEnd)) {
  961. $rtr.= 'expire["' . $key1 . '"]="' . $aRow->dateEnd . '";';
  962. }
  963. }
  964. }
  965. if (isset($params["dateEnd"])) {
  966. $rtr.= 'if(obj.aData.' . $params["dateEnd"] . ' === undefined)
  967. obj.aData.' . $params["dateEnd"] . ' = "";';
  968. $rtr.= 'if(obj.aData.' . $params["dateEnd"] . ' != ""){';
  969. $rtr.= 'var dateEnd = new Date(obj.aData.' . $params["dateEnd"] . ').getTime();';
  970. $rtr.= 'if(dateEnd < now)';
  971. $rtr.= 'if(expire[stat] !== undefined)
  972. stat = expire[stat];';
  973. $rtr.= '}';
  974. }
  975. $rtr.= 'if(status[stat]===undefined)
  976. stat = "ERROR";';
  977. $rtr.= 'var ar = [];
  978. ar[ar.length] = "<small class=\"tag ";
  979. ar[ar.length] = status[stat][1];
  980. ar[ar.length] = " glossy\">";
  981. ar[ar.length] = status[stat][0];
  982. ar[ar.length] = "</small>";
  983. var str = ar.join("");
  984. return str;
  985. }';
  986. break;
  987. case "attachment" :
  988. $url_server = "/db/" . $this->couchdb->getDatabaseName();
  989. $rtr = 'function(obj) {
  990. var ar = [];
  991. ar[ar.length] = "<img src=\"theme/' . $conf->theme . $this->fk_extrafields->ico . '\" border=\"0\" alt=\"' . $langs->trans("See " . get_class($this)) . ' : ";
  992. ar[ar.length] = obj.aData.' . $key . '.toString();
  993. ar[ar.length] = "\" title=\"' . $langs->trans("See " . get_class($this)) . ' : ";
  994. ar[ar.length] = obj.aData.' . $key . '.toString();
  995. ar[ar.length] = "\"></a> <a href=\"' . $url_server . '/";
  996. ar[ar.length] = obj.aData._id;
  997. ar[ar.length] = "/";
  998. ar[ar.length] = obj.aData.' . $key . '.toString();
  999. ar[ar.length] = "\">";
  1000. ar[ar.length] = obj.aData.' . $key . '.toString();
  1001. ar[ar.length] = "</a>";
  1002. var str = ar.join("");
  1003. return str;
  1004. }';
  1005. break;
  1006. case "sizeMo":
  1007. $rtr = 'function(obj) {
  1008. var ar = [];
  1009. if(obj.aData.' . $key . ')
  1010. {
  1011. var size = obj.aData.' . $key . '/1000000;
  1012. size = (Math.round(size*100))/100;
  1013. ar[ar.length] = size;
  1014. ar[ar.length] = " Mo";
  1015. var str = ar.join("");
  1016. return str;
  1017. }
  1018. else
  1019. {
  1020. ar[ar.length] = "0 Mo";
  1021. var str = ar.join("");
  1022. return str;
  1023. }
  1024. }';
  1025. break;
  1026. case "price":
  1027. $rtr = 'function(obj) {
  1028. var ar = [];
  1029. if(obj.aData.' . $key . ' === undefined || obj.aData.' . $key . ' == "") {
  1030. var str = ar.join("");
  1031. return str;
  1032. } else {
  1033. var price = obj.aData.' . $key . ';
  1034. price = ((Math.round(price*100))/100).toFixed(2);
  1035. ar[ar.length] = price;
  1036. ar[ar.length] = " €";
  1037. var str = ar.join("");
  1038. return str;
  1039. }
  1040. }';
  1041. break;
  1042. case "pourcentage":
  1043. $rtr = 'function(obj) {
  1044. var ar = [];
  1045. if(obj.aData.' . $key . ')
  1046. {
  1047. var total = obj.aData.' . $key . ';
  1048. price = ((Math.round(total*100))/100).toFixed(2);
  1049. ar[ar.length] = total;
  1050. ar[ar.length] = " %";
  1051. var str = ar.join("");
  1052. return str;
  1053. }
  1054. else
  1055. {
  1056. ar[ar.length] = "0.00 %";
  1057. var str = ar.join("");
  1058. return str;
  1059. }
  1060. }';
  1061. break;
  1062. case "tag":
  1063. $rtr = 'function(obj) {
  1064. var ar = [];
  1065. if(typeof(obj.aData.' . $key . ')=="string") {
  1066. ar[ar.length] = "<small class=\"tag anthracite-gradient glossy";
  1067. ar[ar.length] = " \">";
  1068. ar[ar.length] = obj.aData.' . $key . '.toString();
  1069. ar[ar.length] = "</small> ";
  1070. } else {
  1071. for (var i in obj.aData.' . $key . ') {
  1072. ar[ar.length] = "<small class=\"tag anthracite-gradient glossy";
  1073. ar[ar.length] = " \">";
  1074. ar[ar.length] = obj.aData.' . $key . '[i].toString();
  1075. ar[ar.length] = "</small> ";
  1076. }
  1077. }
  1078. var str = ar.join("");
  1079. return str;
  1080. }';
  1081. break;
  1082. default :
  1083. dol_print_error($db, "Type of fnRender must be url, date, datetime, attachment or status");
  1084. exit;
  1085. }
  1086. return $rtr;
  1087. }
  1088. /**
  1089. * Function for ajax inbox to create an new object
  1090. * @param $url string url of the create page
  1091. * @return string
  1092. */
  1093. /* public function buttonCreate($url) {
  1094. global $langs;
  1095. print '<a href="#fd_input" class="gh_button pill icon add" id="fd3">' . $langs->trans("Create") . '</a>';
  1096. ?>
  1097. <div style="display:none">
  1098. <div id="inlineDialog">
  1099. <div id="fd_input">
  1100. <div class="fd3_pane">
  1101. <form action="<?php echo $url; ?>" class="nice" style="width:220px">
  1102. <label><?php echo $this->fk_extrafields->labelCreate; ?></label>
  1103. <input type="text" class="input-text fd3_name_input expand" name="id" />
  1104. <a href="#" class="gh_button small pill fd3_submit">Create</a>
  1105. </form>
  1106. </div>
  1107. </div>
  1108. </div>
  1109. </div>
  1110. <?php ?>
  1111. <script type="text/javascript" charset="utf-8">
  1112. $(document).ready(function() {
  1113. $("#fd3").fancybox({
  1114. 'overlayOpacity' : '0.2',
  1115. 'transitionIn' : 'elastic',
  1116. 'transitionOut' : 'fade',
  1117. 'onCleanup' : function() {
  1118. if($('.fd3_pane:first').is(':hidden')){$('.fd3_pane').toggle();$.fancybox.resize();}
  1119. $('.fd3_pane label.error').remove();
  1120. }
  1121. });
  1122. });
  1123. </script>
  1124. <?php
  1125. return 1;
  1126. } */
  1127. /**
  1128. * Compare function for sorting two aaData rows in datatable
  1129. */
  1130. public function sortDatatable(&$array, $key, $dir) {
  1131. if ($dir == "desc")
  1132. usort($array, function($a, $b) use ($key) {
  1133. return $a->$key > $b->$key ? -1 : 1;
  1134. });
  1135. else
  1136. usort($array, function($a, $b) use ($key) {
  1137. return $a->$key > $b->$key ? 1 : -1;
  1138. });
  1139. }
  1140. /**
  1141. * Show object list
  1142. *
  1143. * @param string $view Requested view
  1144. */
  1145. public function showList($view = 'list') {
  1146. require DOL_DOCUMENT_ROOT . '/core/class/autoloader.php';
  1147. //$data_source = "core/ajax/listdatatables.php?json=list&class=" . get_class($this) . "&bServerSide=true";
  1148. $data_source = "core/ajax/listdatatables.php?json=" . $view . "&class=" . get_class($this);
  1149. $table = new datatables\Datatables(compact('data_source'));
  1150. $table->setSchema(new datatables\schemas\DefaultSchema);
  1151. $table->setConfig('object_class', get_class($this));
  1152. // Add default plugins
  1153. $table->plug(new datatables\plugins\Localization);
  1154. // Add plugins defined in database
  1155. if (!empty($this->fk_extrafields->pluginsList)) {
  1156. foreach ($this->fk_extrafields->pluginsList as $plugin) {
  1157. $classname = 'datatables\plugins\\' . $plugin;
  1158. if (class_exists($classname))
  1159. $table->plug(new $classname);
  1160. }
  1161. }
  1162. // render view
  1163. //var_dump(compact('table'));
  1164. return $table->render();
  1165. }
  1166. /**
  1167. * Return list of tags in an object
  1168. *
  1169. * @return array List of types of members
  1170. */
  1171. function listTag() {
  1172. global $conf, $langs;
  1173. $list = array();
  1174. $result = $this->getView('tag', array("group" => true));
  1175. if (count($result->rows) > 0)
  1176. foreach ($result->rows as $aRow) {
  1177. $list[] = $langs->trans($aRow->key);
  1178. }
  1179. return $list;
  1180. }
  1181. /**
  1182. * Renvoie tags list clicable (avec eventuellement le picto)
  1183. *
  1184. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  1185. * @param int $maxlen length max libelle
  1186. * @return string String with URL
  1187. */
  1188. function getTagUrl($withpicto = 0, $maxlen = 0) {
  1189. global $langs;
  1190. $result = '';
  1191. if (count($this->Tag)) {
  1192. for ($i = 0; $i < count($this->Tag); $i++) {
  1193. if (get_class($this) == "Adherent") {
  1194. $lien = '<a href="' . DOL_URL_ROOT . '/adherent/type.php?id=' . $this->Tag[$i] . '">';
  1195. $lienfin = '</a> ';
  1196. } else {
  1197. $lien = '<small>';
  1198. $lienfin = '</small> ';
  1199. }
  1200. $picto = 'group';
  1201. $label = $langs->trans("ShowTypeCard", $this->Tag[$i]);
  1202. if ($withpicto)
  1203. $result.=($lien . img_object($label, $picto) . $lienfin);
  1204. if ($withpicto && $withpicto != 2)
  1205. $result.=' ';
  1206. $result.=$lien . ($maxlen ? dol_trunc($this->Tag[$i], $maxlen) : $this->Tag[$i]) . $lienfin;
  1207. }
  1208. }
  1209. return $result;
  1210. }
  1211. /**
  1212. * Renvoie tags list clicable (avec eventuellement le picto)
  1213. *
  1214. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  1215. * @param int $maxlen length max libelle
  1216. * @return string String with URL
  1217. */
  1218. function LibTag($values, $params = array()) {
  1219. global $langs, $conf;
  1220. if (empty($params["key"]))
  1221. $key = "Tag";
  1222. else
  1223. $key = $params["key"];
  1224. $result = '';
  1225. if (count($values)) {
  1226. for ($i = 0; $i < count($values); $i++) {
  1227. $result.= $this->LibStatus($values[$i], $params);
  1228. }
  1229. }
  1230. return $result;
  1231. }
  1232. function directory($key) {
  1233. $couchdb = clone $this->couchdb;
  1234. $couchdb->useDatabase("directory");
  1235. $couchdb->setQueryParameters(array("key" => $key));
  1236. $result = $couchdb->getView("Directory", "mail");
  1237. return $result->rows[0]->value;
  1238. }
  1239. /**
  1240. * Print a select HTML for fields in extrafields
  1241. *
  1242. * @param string $key name of the field
  1243. * @param string $htmlname HTML name
  1244. * @param string $value if needed : value of the key
  1245. * @param int $lengh max number of characters in label select
  1246. * @param boolean $returnIndex return index or value from select
  1247. * @param string $cssClass Specific css class for input "ex. full-width"
  1248. * @return string String with URL
  1249. */
  1250. function select_fk_extrafields($key, $htmlname, $value = null, $returnIndex = true, $lengh = 40, $cssClass = "") {
  1251. global $langs, $mysoc;
  1252. $aRow = $this->fk_extrafields->fields->$key;
  1253. if (isset($aRow->label))
  1254. $title = $langs->trans($aRow->label);
  1255. else
  1256. $title = $langs->trans($key);
  1257. switch ($aRow->type) {
  1258. case "select" :
  1259. if (GETPOST($htmlname))
  1260. $selected = GETPOST($htmlname);
  1261. elseif (!empty($value)) // Using value from the function
  1262. $selected = $value;
  1263. else
  1264. $selected = $this->$key;
  1265. $rtr = "";
  1266. $rtr.= '<select data-placeholder="' . $title . '&hellip;" class="select ' . $aRow->validate->cssclass . '" id="' . $htmlname . '" name="' . $htmlname . '">';
  1267. if (isset($aRow->class)) { // Is an object
  1268. $class = $aRow->class;
  1269. $object = new $class($this->db);
  1270. $params = array();
  1271. if (count($aRow->params))
  1272. foreach ($aRow->params as $idx => $row) {
  1273. eval("\$row = $row;");
  1274. if (!empty($row))
  1275. $params[$idx] = $row;
  1276. }
  1277. try {
  1278. $result = $object->getView($aRow->view, $params);
  1279. } catch (Exception $e) {
  1280. $this->error = "Fetch : Something weird happened: " . $e->getMessage() . " (errcode=" . $e->getCode() . ")\n";
  1281. dol_print_error($this->db, $this->error);
  1282. return 0;
  1283. }
  1284. $aRow->values[0] = new stdClass();
  1285. $aRow->values[0]->label = "-";
  1286. $aRow->values[0]->enable = true;
  1287. if (!empty($result->rows)) {
  1288. foreach ($result->rows as $row) {
  1289. $aRow->values[$row->value->_id] = new stdClass();
  1290. $aRow->values[$row->value->_id]->label = $row->value->name;
  1291. $aRow->values[$row->value->_id]->enable = true;
  1292. }
  1293. }
  1294. $selected = $this->$key->id; // Index of key
  1295. }
  1296. if (empty($selected)) {
  1297. if (!empty($aRow->default))
  1298. eval('$selected = ' . $aRow->default . ';');
  1299. }
  1300. if (count($aRow->values)) {
  1301. foreach ($aRow->values as $idx => $row) {
  1302. $enable = $row->enable;
  1303. // Apply filter
  1304. if ($enable && !$row->default && count($aRow->filters)) {
  1305. foreach ($aRow->filters as $keyf => $rowf) {
  1306. if ($this->$rowf != $row->$keyf)
  1307. $enable = false;
  1308. }
  1309. }
  1310. if ($enable) {
  1311. if (!empty($row->label))
  1312. $tab_result[$idx] = dol_trunc($langs->trans($row->label), $lengh);
  1313. else
  1314. $tab_result[$idx] = dol_trunc($langs->trans($idx), $lengh);
  1315. }
  1316. }
  1317. // Tri
  1318. if ($aRow->sort) {
  1319. asort($tab_result);
  1320. }
  1321. foreach ($tab_result as $idx => $row) {
  1322. if ($returnIndex)
  1323. $rtr.= '<option value="' . $idx . '"';
  1324. else
  1325. $rtr.= '<option value="' . $row . '"';
  1326. if ($returnIndex) {
  1327. if ($selected == $idx)
  1328. $rtr.= ' selected="selected"';
  1329. } else {
  1330. if ($selected == $row)
  1331. $rtr.= ' selected="selected"';
  1332. }
  1333. $rtr.= '>';
  1334. $rtr.= $row;
  1335. $rtr.='</option>';
  1336. }
  1337. }
  1338. $rtr.= '</select>';
  1339. break;
  1340. case "text":
  1341. $rtr .= '<input type="text" name="' . $htmlname . '" id="' . $htmlname . '" class="input ' . $cssClass . " " . $aRow->validate->cssclass . '" value="' . $this->$key . '" placeholder="' . $title . '"/>';
  1342. break;
  1343. case "textarea":
  1344. $rtr .= '<textarea name="' . $htmlname . '" id="' . $htmlname . '" class="input ' . $cssClass . " " . $aRow->validate->cssclass . '" placeholder="' . $title . '">' . $this->$key . '</textarea>';
  1345. break;
  1346. case "date":
  1347. $rtr .= '<input type="text" class="input ' . $cssClass . " " . $aRow->validate->cssclass . '" name="' . $htmlname . '" id="' . $htmlname . '" value="' . $this->print_fk_extrafields($key) . '" placeholder="' . $title . '"/>';
  1348. $rtr .= '<script>$("input#' . $htmlname . '").datepicker();</script>';
  1349. break;
  1350. case "datetime":
  1351. $rtr .= '<input type="text" class="input ' . $cssClass . " " . $aRow->validate->cssclass . '" name="' . $htmlname . '" id="' . $htmlname . '" value="' . $this->print_fk_extrafields($key) . '" placeholder="' . $title . '"/>';
  1352. $rtr .= '<script>$("input#' . $htmlname . '").datetimepicker();</script>';
  1353. break;
  1354. }
  1355. return $rtr;
  1356. }
  1357. /**
  1358. * Print a value field from extrafields
  1359. *
  1360. * @param string $key name of the field
  1361. * @return string String with URL
  1362. */
  1363. function print_fk_extrafields($key) {
  1364. global $langs;
  1365. $aRow = $this->fk_extrafields->fields->$key;
  1366. $value = $this->$key;
  1367. if (empty($this->$key))
  1368. return null;
  1369. if (is_object($this->$key) && empty($this->$key->id))
  1370. return null;
  1371. if (isset($aRow->class) && empty($aRow->getkey)) { // Is an object
  1372. $class = $aRow->class;
  1373. dol_include_once("/" . strtolower($class) . "/class/" . strtolower($class) . ".class.php");
  1374. $object = new $class($this->db);
  1375. $object->name = $this->$key->name;
  1376. $object->id = $this->$key->id;
  1377. return $object->getNomUrl(1);
  1378. } elseif (isset($aRow->status)) { // Is a status
  1379. return $this->LibStatus($value, array("key" => $key));
  1380. }
  1381. switch ($aRow->type) {
  1382. case "select":
  1383. if (isset($aRow->values->$value->label)) {
  1384. $out.= $langs->trans($aRow->values->$value->label);
  1385. } else {
  1386. if (!is_array($value))
  1387. $out.= $langs->trans($value);
  1388. }
  1389. break;
  1390. case "text":
  1391. $out.= $value;
  1392. break;
  1393. case "textarea":
  1394. $out.= $value;
  1395. break;
  1396. case "email":
  1397. $out.= '<a href="mailto:' . $value . '">' . $value . '</a>';
  1398. break;
  1399. case "date":
  1400. $out .= dol_print_date($value, "%d/%m/%Y");
  1401. break;
  1402. case "datetime":
  1403. $out .= dol_print_date($value, "%d/%m/%Y");
  1404. break;
  1405. case "image":
  1406. if (!empty($value))
  1407. $out.='<img alt="' . $aRow->alt . '" border="0" width="' . $aRow->width . '" src="' . $this->getFile($value) . '">';
  1408. else
  1409. $out.='<img alt="No photo" border="0" width="' . $aRow->width . '" src="' . DOL_URL_ROOT . '/theme/common/nophoto.jpg">';
  1410. break;
  1411. }
  1412. return $out;
  1413. }
  1414. /**
  1415. * return div with block note
  1416. *
  1417. * @return @string
  1418. */
  1419. function show_notes($edit = true) {
  1420. global $conf, $user, $langs;
  1421. //$out = start_box($langs->trans("Notes"), "icon-info-round");
  1422. $out.= '<div class="standard-tabs">';
  1423. // Tabs
  1424. $out.= '<ul class="tabs">';
  1425. $out.= '<li class="active"><a href="#tab-private-note">' . $langs->trans('PrivateNotes') . '</a></li>';
  1426. $out.= '<li><a href="#tab-public-note">' . $langs->trans('PublicNotes') . '</a></li>';
  1427. $out.= '</ul>';
  1428. // Contents
  1429. $out.= '<div class="tabs-content">';
  1430. $out.= '<div id="tab-private-note" class="with-padding">';
  1431. // Notes
  1432. if ($edit) {
  1433. $out.= '<input id="element_id_notes" type="hidden" value="' . $this->id . '"/>';
  1434. $out.= '<input id="element_class_notes" type="hidden" value="' . get_class($this) . '"/>';
  1435. $out.= '<div class="wrapped no-margin-bottom left-icon icon-info-round">';
  1436. $out.= '<h4 class="no-margin-bottom">' . $langs->trans("Notes") . '</h4>';
  1437. $out.= '<div id="editval_notes" class="edit_wysiwyg with-tooltip">';
  1438. $out.= $this->notes . '</div>';
  1439. $out.= '</div>';
  1440. } else {
  1441. $out.= '<div class="wrapped no-margin-bottom left-icon icon-info-round">';
  1442. $out.= '<h4 class="no-margin-bottom">' . $langs->trans("Notes") . '</h4>';
  1443. $out.= $this->notes;
  1444. $out.= '</div>';
  1445. }
  1446. $out.= '</div>';
  1447. $out.= '<div id="tab-public-note" class="with-padding">';
  1448. // Notes
  1449. if ($edit) {
  1450. $out.= '<input id="element_id_public_notes" type="hidden" value="' . $this->id . '"/>';
  1451. $out.= '<input id="element_class_public_notes" type="hidden" value="' . get_class($this) . '"/>';
  1452. $out.= '<div class="wrapped no-margin-bottom left-icon icon-info-round">';
  1453. $out.= '<h4 class="no-margin-bottom">' . $langs->trans("Notes") . '</h4>';
  1454. $out.= '<div id="editval_public_notes" class="edit_wysiwyg with-tooltip">';
  1455. $out.= $this->public_notes . '</div>';
  1456. $out.= '</div>';
  1457. } else {
  1458. $out.= '<div class="wrapped no-margin-bottom left-icon icon-info-round">';
  1459. $out.= '<h4 class="no-margin-bottom">' . $langs->trans("Notes") . '</h4>';
  1460. $out.= $this->public_notes;
  1461. $out.= '</div>';
  1462. }
  1463. $out.= '</div>';
  1464. $out.= '</div>';
  1465. $out.= '</div>';
  1466. //$out.= end_box();
  1467. return $out;
  1468. }
  1469. /**
  1470. * Test if an object is changed before a record()
  1471. * return boolean
  1472. */
  1473. function isChanged($oldObj) {
  1474. $change = false;
  1475. if (is_object($oldObj)) {
  1476. if (get_class($this) != get_class($oldObj))
  1477. return true;
  1478. }
  1479. else
  1480. return true;
  1481. foreach ($this->fk_extrafields->fields as $key => $aRow) {
  1482. if ($this->$key != $oldObj->$key)
  1483. $change = true;
  1484. }
  1485. return $change;
  1486. }
  1487. /**
  1488. * Show history record
  1489. *
  1490. * @param int $max Max nb of records
  1491. * @return void
  1492. */
  1493. function show_history($max = 5) {
  1494. global $langs, $conf, $user, $db, $bc;
  1495. $titre = $langs->trans("History");
  1496. print start_box($titre, "icon-calendar");
  1497. $i = 0;
  1498. $obj = new stdClass();
  1499. $societe = new Societe($this->db);
  1500. print '<table class="display dt_act" id="history_datatable" >';
  1501. // Ligne des titres
  1502. print '<thead>';
  1503. print'<tr>';
  1504. print'<th class="essential">';
  1505. print $langs->trans("Date");
  1506. print'</th>';
  1507. $obj->aoColumns[$i] = new stdClass();
  1508. $obj->aoColumns[$i]->mDataProp = "date";
  1509. $obj->aoColumns[$i]->bUseRendered = false;
  1510. $obj->aoColumns[$i]->bSearchable = true;
  1511. $obj->aoColumns[$i]->fnRender = $this->datatablesFnRender("date", "datetime");
  1512. $i++;
  1513. print'<th class="essential">';
  1514. print $langs->trans('User');
  1515. print'</th>';
  1516. $userstatic = new User();
  1517. $obj->aoColumns[$i] = new stdClass();
  1518. $obj->aoColumns[$i]->mDataProp = "author";
  1519. $obj->aoColumns[$i]->sDefaultContent = "";
  1520. $obj->aoColumns[$i]->fnRender = $userstatic->datatablesFnRender("author.name", "url", array('id' => "author.id"))

Large files files are truncated, but you can click here to view the full file