1<?php 2 3// Copyright (C) 2010-2018 Combodo SARL 4// 5// This file is part of iTop. 6// 7// iTop is free software; you can redistribute it and/or modify 8// it under the terms of the GNU Affero General Public License as published by 9// the Free Software Foundation, either version 3 of the License, or 10// (at your option) any later version. 11// 12// iTop is distributed in the hope that it will be useful, 13// but WITHOUT ANY WARRANTY; without even the implied warranty of 14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15// GNU Affero General Public License for more details. 16// 17// You should have received a copy of the GNU Affero General Public License 18// along with iTop. If not, see <http://www.gnu.org/licenses/> 19 20namespace Combodo\iTop\Form\Field; 21 22use Closure; 23use DBSearch; 24use DBObjectSet; 25use BinaryExpression; 26use FieldExpression; 27use ScalarExpression; 28use Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator; 29 30/** 31 * Description of SelectObjectField 32 * 33 * @author Romain Quetiez <romain.quetiez@combodo.com> 34 */ 35class SelectObjectField extends Field 36{ 37 protected $oSearch; 38 protected $iMaximumComboLength; 39 protected $iMinAutoCompleteChars; 40 protected $bHierarchical; 41 protected $iControlType; 42 protected $sSearchEndpoint; 43 44 const CONTROL_SELECT = 1; 45 const CONTROL_RADIO_VERTICAL = 2; 46 47 public function __construct($sId, Closure $onFinalizeCallback = null) 48 { 49 parent::__construct($sId, $onFinalizeCallback); 50 $this->oSearch = null; 51 $this->iMaximumComboLength = null; 52 $this->iMinAutoCompleteChars = 3; 53 $this->bHierarchical = false; 54 $this->iControlType = self::CONTROL_SELECT; 55 $this->sSearchEndpoint = null; 56 } 57 58 public function SetSearch(DBSearch $oSearch) 59 { 60 $this->oSearch = $oSearch; 61 return $this; 62 } 63 64 public function SetMaximumComboLength($iMaximumComboLength) 65 { 66 $this->iMaximumComboLength = $iMaximumComboLength; 67 return $this; 68 } 69 70 public function SetMinAutoCompleteChars($iMinAutoCompleteChars) 71 { 72 $this->iMinAutoCompleteChars = $iMinAutoCompleteChars; 73 return $this; 74 } 75 76 public function SetHierarchical($bHierarchical) 77 { 78 $this->bHierarchical = $bHierarchical; 79 return $this; 80 } 81 82 public function SetControlType($iControlType) 83 { 84 $this->iControlType = $iControlType; 85 } 86 87 public function SetSearchEndpoint($sSearchEndpoint) 88 { 89 $this->sSearchEndpoint = $sSearchEndpoint; 90 return $this; 91 } 92 93 /** 94 * Sets if the field is mandatory or not. 95 * Setting the value will automatically add/remove a MandatoryValidator to the Field 96 * 97 * @param boolean $bMandatory 98 * @return \Combodo\iTop\Form\Field\Field 99 */ 100 public function SetMandatory($bMandatory) 101 { 102 // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator 103 if ($bMandatory && !$this->bMandatory) 104 { 105 $this->AddValidator(new NotEmptyExtKeyValidator()); 106 } 107 108 if (!$bMandatory) 109 { 110 foreach ($this->aValidators as $iKey => $oValue) 111 { 112 if ($oValue::Getname() === NotEmptyExtKeyValidator::GetName()) 113 { 114 unset($this->aValidators[$iKey]); 115 } 116 } 117 } 118 119 $this->bMandatory = $bMandatory; 120 return $this; 121 } 122 123 /** 124 * @return \DBSearch 125 */ 126 public function GetSearch() 127 { 128 return $this->oSearch; 129 } 130 131 public function GetMaximumComboLength() 132 { 133 return $this->iMaximumComboLength; 134 } 135 136 public function GetMinAutoCompleteChars() 137 { 138 return $this->iMinAutoCompleteChars; 139 } 140 141 public function GetHierarchical() 142 { 143 return $this->bHierarchical; 144 } 145 146 public function GetControlType() 147 { 148 return $this->iControlType; 149 } 150 151 public function GetSearchEndpoint() 152 { 153 return $this->sSearchEndpoint; 154 } 155 156 /** 157 * Resets current value is not among allowed ones. 158 * By default, reset is done ONLY when the field is not read-only. 159 * 160 * @param boolean $bAlways Set to true to verify even when the field is read-only. 161 * 162 * @throws \CoreException 163 */ 164 public function VerifyCurrentValue($bAlways = false) 165 { 166 if(!$this->GetReadOnly() || $bAlways) 167 { 168 $oValuesScope = $this->GetSearch()->DeepClone(); 169 $oBinaryExp = new BinaryExpression(new FieldExpression('id', $oValuesScope->GetClassAlias()), '=', new ScalarExpression($this->currentValue)); 170 $oValuesScope->AddConditionExpression($oBinaryExp); 171 $oValuesSet = new DBObjectSet($oValuesScope); 172 173 if($oValuesSet->Count() === 0) 174 { 175 $this->currentValue = null; 176 } 177 } 178 } 179} 180