Profile.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. namespace CablemodemBundle\Entity;
  3. use Base\AdminBundle\Traits\TenancyIdTrait;
  4. use Base\AdminBundle\Traits\TenancyIdTraitInterface;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use ExtraDataBundle\Entity\Traits\ExtraDataTrait;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Yaml\Yaml;
  10. use Base\AdminBundle\Interfaces\PreRemoveBlockCascadeInterface;
  11. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  12. use Sonata\AdminBundle\Exception\ModelManagerException;
  13. /**
  14. * @ORM\Entity
  15. * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name","tenancy_id"})})
  16. * @ORM\HasLifecycleCallbacks
  17. * @UniqueEntity(fields={"tenancyId","name"})
  18. */
  19. class Profile implements TenancyIdTraitInterface, PreRemoveBlockCascadeInterface
  20. {
  21. use TenancyIdTrait;
  22. use ExtraDataTrait;
  23. /**
  24. * @var bigint $id
  25. *
  26. * @ORM\Column(name="id", type="bigint", nullable=false)
  27. * @ORM\Id
  28. * @ORM\GeneratedValue(strategy="IDENTITY")
  29. */
  30. private $id;
  31. /**
  32. * @var string $name
  33. *
  34. * @ORM\Column(name="name", type="string", length=100)
  35. *
  36. * @Assert\NotNull
  37. * @Assert\Regex(
  38. * pattern="/[\w\s\d]+/",
  39. * match=true,
  40. * message="El campo contiene caracteres especiales"
  41. * )
  42. */
  43. protected $name;
  44. /**
  45. * @var bigint $downstream
  46. *
  47. * @ORM\Column(type="bigint", nullable=true)
  48. */
  49. protected $downstream;
  50. /**
  51. * @var bigint $upstream
  52. *
  53. * @ORM\Column(type="bigint", nullable=true)
  54. */
  55. protected $upstream;
  56. /**
  57. * @var bigint $filtroUpload
  58. *
  59. * @ORM\Column(type="bigint", nullable=true)
  60. */
  61. protected $filtroUpload;
  62. /**
  63. * @var bigint $filtroDownload
  64. *
  65. * @ORM\Column(type="bigint", nullable=true)
  66. */
  67. protected $filtroDownload;
  68. /**
  69. * @var bigint $maxCpe
  70. *
  71. * @ORM\Column(type="bigint", nullable=true)
  72. */
  73. protected $maxCpe;
  74. /**
  75. * @var string $template
  76. *
  77. * @ORM\Column(type="string", nullable=true)
  78. */
  79. protected $template;
  80. /**
  81. * @ORM\OneToMany(targetEntity="Cablemodem", mappedBy="profile", fetch="EXTRA_LAZY")
  82. */
  83. protected $cablemodems;
  84. public function __construct()
  85. {
  86. $this->downstream = $this->getDefault('downstream');
  87. $this->upstream = $this->getDefault('upstream');
  88. $this->template = $this->getDefault('template');
  89. $this->cablemodems = new \Doctrine\Common\Collections\ArrayCollection();
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function __toString()
  95. {
  96. return (string)$this->name;
  97. }
  98. /**
  99. * @return int
  100. */
  101. public function getId()
  102. {
  103. return $this->id;
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getName()
  109. {
  110. return $this->name;
  111. }
  112. /**
  113. * @return bigint
  114. */
  115. public function getDownstream()
  116. {
  117. return $this->downstream ? $this->downstream : $this->getDefault('downstream');
  118. }
  119. /**
  120. * @return bigint
  121. */
  122. public function getUpstream()
  123. {
  124. return $this->upstream ? $this->upstream : $this->getDefault('upstream');
  125. }
  126. /**
  127. * @return bigint
  128. */
  129. public function getFiltroUpload()
  130. {
  131. return $this->filtroUpload;
  132. }
  133. /**
  134. * @return bigint
  135. */
  136. public function getFiltroDownload()
  137. {
  138. return $this->filtroDownload;
  139. }
  140. /**
  141. * @return bigint
  142. */
  143. public function getMaxCpe()
  144. {
  145. return $this->maxCpe ? $this->maxCpe : $this->getDefault('max_cpe');
  146. }
  147. /**
  148. * @param string $name
  149. *
  150. * @return $this
  151. */
  152. public function setName($name)
  153. {
  154. $this->name = $name;
  155. return $this;
  156. }
  157. /**
  158. * @param bigint $downstream
  159. *
  160. * @return $this
  161. */
  162. public function setDownstream($downstream)
  163. {
  164. if(is_null($downstream)) {
  165. $this->downstream = $this->getDefault('downstream');
  166. } else {
  167. $this->downstream = $downstream;
  168. }
  169. return $this;
  170. }
  171. /**
  172. * @param bigint $upstream
  173. *
  174. * @return $this
  175. */
  176. public function setUpstream($upstream)
  177. {
  178. if(is_null($upstream)) {
  179. $this->upstream = $this->getDefault('upstream');
  180. } else {
  181. $this->upstream = $upstream;
  182. }
  183. return $this;
  184. }
  185. /**
  186. * @param bigint $filtroUpload
  187. *
  188. * @return $this
  189. */
  190. public function setFiltroUpload($filtroUpload)
  191. {
  192. $this->filtroUpload = $filtroUpload;
  193. return $this;
  194. }
  195. /**
  196. * @param bigint $filtroDownload
  197. *
  198. * @return $this
  199. */
  200. public function setFiltroDownload($filtroDownload)
  201. {
  202. $this->filtroDownload = $filtroDownload;
  203. return $this;
  204. }
  205. /**
  206. * @param bigint $maxCpe
  207. *
  208. * @return $this
  209. */
  210. public function setMaxCpe($maxCpe)
  211. {
  212. $this->maxCpe = $maxCpe;
  213. return $this;
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function getSipDevProxyServer()
  219. {
  220. return $this->getData('sipDevProxyServer') ? $this->getData('sipDevProxyServer') : $this->getDefault('sipDevProxyServer');
  221. }
  222. /**
  223. * @return string
  224. */
  225. public function getServerVoIP_IP()
  226. {
  227. return $this->getData('ServerVoIP_IP') ? $this->getData('ServerVoIP_IP') : $this->getDefault('ServerVoIP_IP');
  228. }
  229. /**
  230. * @return string
  231. */
  232. public function getSipDevRegistrar()
  233. {
  234. return $this->getData('sipDevRegistrar') ? $this->getData('sipDevRegistrar') : $this->getDefault('sipDevRegistrar');
  235. }
  236. /**
  237. * @param string $name
  238. *
  239. * @return mixed
  240. */
  241. public function getDefault($name = null)
  242. {
  243. global $kernel;
  244. $dir = $kernel->getProjectDir();
  245. $values = Yaml::parse(file_get_contents("{$dir}/app/config/profile.yml"));
  246. $value = null;
  247. if (!is_null($name) && isset($values[$name])) {
  248. $value = $values[$name];
  249. }
  250. return $value;
  251. }
  252. /**
  253. * @return string
  254. */
  255. public function getTemplate()
  256. {
  257. return $this->template;
  258. }
  259. /**
  260. * @param string $template
  261. *
  262. * @return Profile
  263. */
  264. public function setTemplate($template)
  265. {
  266. $this->template = $template;
  267. return $this;
  268. }
  269. /**
  270. * @param Cablemodem $cablemodem
  271. * @return Profile
  272. */
  273. public function addCablemodem(Cablemodem $cablemodem)
  274. {
  275. $this->cablemodems[] = $cablemodem;
  276. return $this;
  277. }
  278. /**
  279. * @param Cablemodem $cablemodem
  280. * @return Profile
  281. */
  282. public function removeCablemodem(Cablemodem $cablemodem)
  283. {
  284. $this->cablemodems->removeElement($cablemodem);
  285. return $this;
  286. }
  287. /**
  288. * @return Doctrine\Common\Collections\Collection
  289. */
  290. public function getCablemodems()
  291. {
  292. return $this->cablemodems;
  293. }
  294. /**
  295. * @return array
  296. */
  297. public function getEntitiesBlockRemove()
  298. {
  299. $entities = [];
  300. if ($this->cablemodems->count() != 0) {
  301. $entities['cablemodems'] = $this->cablemodems;
  302. }
  303. return $entities;
  304. }
  305. /**
  306. * @ORM\PreRemove
  307. */
  308. public function preRemove(LifecycleEventArgs $event)
  309. {
  310. $em = $event->getEntityManager();
  311. $entity = $event->getEntity();
  312. if (count($entity->getCablemodems()) > 0) {
  313. $error = "This profile has connected cablemodems, so it can't be removed.";
  314. throw new ModelManagerException($error);
  315. }
  316. }
  317. }