/modules/Settings/PickListDependency/models/Record.php

https://bitbucket.org/thomashii/vtigercrm-6-for-postgresql · PHP · 169 lines · 132 code · 24 blank · 13 comment · 11 complexity · 16f4f69fd3b5551653c13511e0126a97 MD5 · raw file

  1. <?php
  2. /*+***********************************************************************************
  3. * The contents of this file are subject to the vtiger CRM Public License Version 1.0
  4. * ("License"); You may not use this file except in compliance with the License
  5. * The Original Code is: vtiger CRM Open Source
  6. * The Initial Developer of the Original Code is vtiger.
  7. * Portions created by vtiger are Copyright (C) vtiger.
  8. * All Rights Reserved.
  9. *************************************************************************************/
  10. vimport('~~modules/PickList/DependentPickListUtils.php');
  11. class Settings_PickListDependency_Record_Model extends Settings_Vtiger_Record_Model {
  12. private $mapping = false;
  13. private $sourcePickListValues = false;
  14. private $targetPickListValues = false;
  15. private $nonMappedSourcePickListValues = false;
  16. /**
  17. * Function to get the Id
  18. * @return <Number>
  19. */
  20. public function getId() {
  21. return '';
  22. }
  23. public function getName() {
  24. return '';
  25. }
  26. public function getRecordLinks() {
  27. $soureModule = $this->get('sourceModule');
  28. $sourceField = $this->get('sourcefield');
  29. $targetField = $this->get('targetfield');
  30. $editLink = array(
  31. 'linkurl' => "javascript:Settings_PickListDependency_Js.triggerEdit(event, '$soureModule', '$sourceField', '$targetField')",
  32. 'linklabel' => 'LBL_EDIT',
  33. 'linkicon' => 'icon-pencil'
  34. );
  35. $editLinkInstance = Vtiger_Link_Model::getInstanceFromValues($editLink);
  36. $deleteLink = array(
  37. 'linkurl' => "javascript:Settings_PickListDependency_Js.triggerDelete(event, '$soureModule','$sourceField', '$targetField')",
  38. 'linklabel' => 'LBL_DELETE',
  39. 'linkicon' => 'icon-trash'
  40. );
  41. $deleteLinkInstance = Vtiger_Link_Model::getInstanceFromValues($deleteLink);
  42. return array($editLinkInstance,$deleteLinkInstance);
  43. }
  44. public function getAllPickListFields() {
  45. $db = PearDatabase::getInstance();
  46. $tabId = getTabid($this->get('sourceModule'));
  47. $query="select vtiger_field.fieldlabel,vtiger_field.fieldname FROM vtiger_field" .
  48. " where displaytype=1 and vtiger_field.tabid=? and vtiger_field.uitype in ('15','16') " .
  49. " and vtiger_field.presence in (0,2)";
  50. $result = $db->pquery($query, array($tabId));
  51. $noofrows = $db->num_rows($result);
  52. $fieldlist = array();
  53. if($noofrows > 0) {
  54. for($i=0; $i<$noofrows; ++$i) {
  55. $fieldlist[$db->query_result($result,$i,"fieldname")] = $db->query_result($result,$i,"fieldlabel");
  56. }
  57. }
  58. return $fieldlist;
  59. }
  60. public function getPickListDependency() {
  61. if(empty($this->mapping)) {
  62. $dependency = Vtiger_DependencyPicklist::getPickListDependency($this->get('sourceModule'), $this->get('sourcefield'), $this->get('targetfield'));
  63. $this->mapping = $dependency['valuemapping'];
  64. }
  65. return $this->mapping;
  66. }
  67. private function getPickListValues($fieldName) {
  68. //Need to decode the picklist values twice which are saved from old ui
  69. return array_map('decode_html', getAllPickListValues($fieldName));
  70. }
  71. public function getSourcePickListValues() {
  72. if(empty($this->sourcePickListValues)) {
  73. $this->sourcePickListValues = $this->getPickListValues($this->get('sourcefield'));
  74. }
  75. return $this->sourcePickListValues;
  76. }
  77. public function getTargetPickListValues() {
  78. if(empty($this->targetPickListValues)) {
  79. $this->targetPickListValues = $this->getPickListValues($this->get('targetfield'));
  80. }
  81. return $this->targetPickListValues;
  82. }
  83. public function getNonMappedSourcePickListValues() {
  84. if(empty($this->nonMappedSourcePickListValues)) {
  85. $sourcePickListValues = $this->getSourcePickListValues();
  86. $dependencyMapping = $this->getPickListDependency();
  87. foreach($dependencyMapping as $mappingDetails) {
  88. unset($sourcePickListValues[$mappingDetails['sourcevalue']]);
  89. }
  90. $this->nonMappedSourcePickListValues = $sourcePickListValues;
  91. }
  92. return $this->nonMappedSourcePickListValues;
  93. }
  94. public function save($mapping) {
  95. $dependencyMap = array();
  96. $dependencyMap['sourcefield'] = $this->get('sourcefield');
  97. $dependencyMap['targetfield'] = $this->get('targetfield');
  98. $dependencyMap['valuemapping'] = $mapping;
  99. Vtiger_DependencyPicklist::savePickListDependencies($this->get('sourceModule'), $dependencyMap);
  100. return true;
  101. }
  102. public function delete() {
  103. Vtiger_DependencyPicklist::deletePickListDependencies($this->get('sourceModule'), $this->get('sourcefield'), $this->get('targetfield'));
  104. return true;
  105. }
  106. private function loadFieldLabels() {
  107. $db = PearDatabase::getInstance();
  108. $tabId = getTabid($this->get('sourceModule'));
  109. $fieldNames = array($this->get('sourcefield'),$this->get('targetfield'));
  110. $query = 'SELECT fieldlabel,fieldname FROM vtiger_field WHERE fieldname IN ('.generateQuestionMarks($fieldNames).') AND tabid = ?';
  111. $params = array($fieldNames, $tabId);
  112. $result = $db->pquery($query, $params);
  113. $num_rows = $db->num_rows($result);
  114. for($i=0; $i<$num_rows; $i++) {
  115. $row = $db->query_result_rowdata($result,$i);
  116. $fieldName = $row['fieldname'];
  117. if($fieldName == $this->get('sourcefield')) {
  118. $this->set('sourcelabel', $row['fieldlabel']);
  119. }else{
  120. $this->set('targetlabel', $row['fieldlabel']);
  121. }
  122. }
  123. }
  124. public function getSourceFieldLabel() {
  125. $sourceFieldLabel = $this->get('sourcelabel');
  126. if(empty($sourceFieldLabel)) {
  127. $this->loadFieldLabels();
  128. }
  129. return vtranslate($this->get('sourcelabel'), $this->get('sourceModule'));
  130. }
  131. public function getTargetFieldLabel() {
  132. $targetFieldLabel = $this->get('targetlabel');
  133. if(empty($targetFieldLabel)) {
  134. $this->loadFieldLabels();
  135. }
  136. return vtranslate($this->get('targetlabel'), $this->get('sourceModule'));
  137. }
  138. public static function getInstance($module, $sourceField, $targetField) {
  139. $self = new self();
  140. $self->set('sourceModule', $module)
  141. ->set('sourcefield', $sourceField)
  142. ->set('targetfield', $targetField);
  143. return $self;
  144. }
  145. }