Action.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace WorkflowBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. /**
  6. * @ORM\Table
  7. * @ORM\Entity(repositoryClass="WorkflowBundle\Repository\ActionRepository")
  8. */
  9. class Action
  10. {
  11. /**
  12. * @var int
  13. *
  14. * @ORM\Column(type="integer")
  15. * @ORM\Id
  16. * @ORM\GeneratedValue(strategy="AUTO")
  17. */
  18. private $id;
  19. /**
  20. * @var string
  21. *
  22. * @ORM\Column(type="string", length=255, nullable=false)
  23. *
  24. * @Assert\NotNull
  25. * @Assert\NotBlank
  26. */
  27. protected $name;
  28. /**
  29. * @var string
  30. *
  31. * @ORM\Column(type="string", length=255, nullable=true)
  32. *
  33. * @Assert\Callback({"WorkflowBundle\Validator\EventFieldsValidator", "validate"})
  34. */
  35. protected $workflowName;
  36. /**
  37. * @var string
  38. *
  39. * @ORM\Column(type="string", length=255, nullable=true)
  40. *
  41. * @Assert\Callback(
  42. * callback={"WorkflowBundle\Validator\EventFieldsValidator", "validate"},
  43. * payload={"doctrine"="true"}
  44. * )
  45. */
  46. protected $objectClass;
  47. /**
  48. * @var array
  49. *
  50. * @ORM\Column(type="array", nullable=true)
  51. */
  52. protected $event;
  53. /**
  54. * @var string
  55. *
  56. * @ORM\Column(type="string", length=255, nullable=true)
  57. * @Assert\Callback({"WorkflowBundle\Validator\EventFieldsValidator", "validate"})
  58. */
  59. protected $eventReference;
  60. /**
  61. * @var string
  62. *
  63. * @ORM\Column(type="text", nullable=false)
  64. *
  65. * @Assert\NotNull
  66. * @Assert\NotBlank
  67. */
  68. protected $template;
  69. /**
  70. * @var int
  71. *
  72. * @ORM\Column(type="integer", nullable=false, options={"default":1})
  73. */
  74. protected $tenancyId = 1;
  75. /**
  76. * Array con variables de contexto a utilizar en el render del template
  77. *
  78. * @var array
  79. */
  80. protected $twigParams;
  81. public function __construct()
  82. {
  83. $this->twigParams = array();
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function __toString()
  89. {
  90. return (string) $this->name;
  91. }
  92. /**
  93. * @return int
  94. */
  95. public function getId()
  96. {
  97. return $this->id;
  98. }
  99. /**
  100. * @param string $name
  101. *
  102. * @return Action
  103. */
  104. public function setName($name)
  105. {
  106. $this->name = $name;
  107. return $this;
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function getName()
  113. {
  114. return $this->name;
  115. }
  116. /**
  117. * @param array $event
  118. *
  119. * @return Action
  120. */
  121. public function setEvent($event = array())
  122. {
  123. $this->event = $event;
  124. return $this;
  125. }
  126. /**
  127. * @return array
  128. */
  129. public function getEvent()
  130. {
  131. return $this->event;
  132. }
  133. /**
  134. * @param text $template
  135. *
  136. * @return Action
  137. */
  138. public function setTemplate($template)
  139. {
  140. $this->template = $template;
  141. return $this;
  142. }
  143. /**
  144. * @return text
  145. */
  146. public function getTemplate()
  147. {
  148. return $this->template;
  149. }
  150. /**
  151. * Set workflowName
  152. *
  153. * @param string $workflowName
  154. *
  155. * @return Action
  156. */
  157. public function setWorkflowName($workflowName = null)
  158. {
  159. $this->workflowName = $workflowName;
  160. return $this;
  161. }
  162. /**
  163. * Get workflowName
  164. *
  165. * @return string
  166. */
  167. public function getWorkflowName()
  168. {
  169. return $this->workflowName;
  170. }
  171. /**
  172. * Set objectClass
  173. *
  174. * @param string $objectClass
  175. *
  176. * @return Action
  177. */
  178. public function setObjectClass($objectClass)
  179. {
  180. $this->objectClass = $objectClass;
  181. return $this;
  182. }
  183. /**
  184. * Get objectClass
  185. *
  186. * @return string
  187. */
  188. public function getObjectClass()
  189. {
  190. return $this->objectClass;
  191. }
  192. /**
  193. * Set eventReference
  194. *
  195. * @param string $eventReference
  196. *
  197. * @return Action
  198. */
  199. public function setEventReference($eventReference)
  200. {
  201. $this->eventReference = $eventReference;
  202. return $this;
  203. }
  204. /**
  205. * Get eventReference
  206. *
  207. * @return string
  208. */
  209. public function getEventReference()
  210. {
  211. return $this->eventReference;
  212. }
  213. /**
  214. * Set tenancyId
  215. *
  216. * @param integer $tenancyId
  217. *
  218. * @return Action
  219. */
  220. public function setTenancyId($tenancyId)
  221. {
  222. $this->tenancyId = $tenancyId;
  223. return $this;
  224. }
  225. /**
  226. * Get tenancyId
  227. *
  228. * @return integer
  229. */
  230. public function getTenancyId()
  231. {
  232. return $this->tenancyId;
  233. }
  234. /**
  235. * @return array
  236. */
  237. public function getTwigParams()
  238. {
  239. return $this->twigParams;
  240. }
  241. /**
  242. * @param array $twigParams
  243. *
  244. * @return $this
  245. */
  246. public function setTwigParams($twigParams)
  247. {
  248. $this->twigParams = $twigParams;
  249. return $this;
  250. }
  251. /**
  252. * @param Entity $entity
  253. *
  254. * @return string
  255. */
  256. public function render($entity)
  257. {
  258. $template = $this->template;
  259. $loader = new \Twig_Loader_Array(array(
  260. $this->name => $template
  261. ));
  262. $twig = new \Twig_Environment($loader, array(
  263. 'cache' => false,
  264. ));
  265. return $twig->render($this->name, array(
  266. 'entity' => $entity,
  267. 'object' => $entity,
  268. 'params' => $this->twigParams,
  269. ));
  270. }
  271. }