Workflow.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. namespace WorkflowBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
  8. use Symfony\Component\Yaml\Yaml;
  9. use Symfony\Bundle\FrameworkBundle\Console\Application;
  10. use Symfony\Component\Console\Input\ArrayInput;
  11. use Symfony\Component\Console\Output\BufferedOutput;
  12. /**
  13. * Workflow
  14. *
  15. * @ORM\Entity
  16. * @ORM\HasLifecycleCallbacks
  17. * @UniqueEntity(fields={"name", "tenancyId"}, message="errors.duplicate_key")
  18. * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="unique_idx", columns={"name", "tenancy_id"})})
  19. */
  20. class Workflow
  21. {
  22. /**
  23. * @var int
  24. *
  25. * @ORM\Column(name="id", type="integer", nullable=false)
  26. * @ORM\Id
  27. * @ORM\GeneratedValue(strategy="AUTO")
  28. */
  29. private $id;
  30. /**
  31. * @var string
  32. *
  33. * @ORM\Column(type="string", length=100, nullable=false)
  34. */
  35. protected $name;
  36. /**
  37. * @var string
  38. *
  39. * @ORM\Column(type="string", length=100, nullable=false, options={"default":"state_machine"})
  40. */
  41. protected $type = "state_machine";
  42. /**
  43. * @var string
  44. *
  45. * @ORM\Column(type="string", length=100, nullable=false, options={"default":"single_state"})
  46. */
  47. protected $markingType = "single_state";
  48. /**
  49. * @var string
  50. *
  51. * @ORM\Column(type="string", length=100, nullable=false, options={"default":"currentState"})
  52. */
  53. protected $markingName = "currentState";
  54. /**
  55. * @var string
  56. *
  57. * @ORM\Column(type="string", length=350, nullable=true)
  58. */
  59. protected $description;
  60. /**
  61. * @var text
  62. *
  63. * @ORM\Column(type="text", nullable=false)
  64. */
  65. protected $template;
  66. /**
  67. * @Gedmo\Timestampable(on="create")
  68. * @ORM\Column(type="datetime")
  69. */
  70. protected $created;
  71. /**
  72. * @Gedmo\Timestampable(on="update")
  73. * @ORM\Column(type="datetime")
  74. */
  75. protected $updated;
  76. /**
  77. * @var ArrayCollection
  78. *
  79. * @ORM\OneToMany(targetEntity="Action", mappedBy="workflow")
  80. */
  81. protected $actions;
  82. /**
  83. * @var int
  84. *
  85. * @ORM\Column(type="integer", nullable=false, options={"default":1})
  86. */
  87. protected $tenancyId = 1;
  88. /**
  89. * @ORM\Column(type="boolean", nullable=true, columnDefinition="BOOLEAN DEFAULT TRUE")
  90. */
  91. protected $enable = true;
  92. public function __construct()
  93. {
  94. $this->actions = new \Doctrine\Common\Collections\ArrayCollection();
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function __toString()
  100. {
  101. return $this->name;
  102. }
  103. /**
  104. * Get id
  105. *
  106. * @return int
  107. */
  108. public function getId()
  109. {
  110. return $this->id;
  111. }
  112. /**
  113. * Set name
  114. *
  115. * @param string $name
  116. *
  117. * @return Workflow
  118. */
  119. public function setName($name)
  120. {
  121. $_name = strtolower($name);
  122. $name = str_replace(" ","_",$_name);
  123. $this->name = $name;
  124. return $this;
  125. }
  126. /**
  127. * Get name
  128. *
  129. * @return string
  130. */
  131. public function getName()
  132. {
  133. return $this->name;
  134. }
  135. /**
  136. * Set type
  137. *
  138. * @param string $type
  139. *
  140. * @return Workflow
  141. */
  142. public function setType($type)
  143. {
  144. $this->type = $type;
  145. return $this;
  146. }
  147. /**
  148. * Get type
  149. *
  150. * @return string
  151. */
  152. public function getType()
  153. {
  154. return $this->type;
  155. }
  156. /**
  157. * Set markingType
  158. *
  159. * @param string $markingType
  160. *
  161. * @return Workflow
  162. */
  163. public function setMarkingType($markingType)
  164. {
  165. $this->markingType = $markingType;
  166. return $this;
  167. }
  168. /**
  169. * Get markingType
  170. *
  171. * @return string
  172. */
  173. public function getMarkingType()
  174. {
  175. return $this->markingType;
  176. }
  177. /**
  178. * Set markingName
  179. *
  180. * @param string $markingName
  181. *
  182. * @return Workflow
  183. */
  184. public function setMarkingName($markingName)
  185. {
  186. $this->markingName = $markingName;
  187. return $this;
  188. }
  189. /**
  190. * Get markingName
  191. *
  192. * @return string
  193. */
  194. public function getMarkingName()
  195. {
  196. return $this->markingName;
  197. }
  198. /**
  199. * Set description
  200. *
  201. * @param string $description
  202. *
  203. * @return Workflow
  204. */
  205. public function setDescription($description)
  206. {
  207. $this->description = $description;
  208. return $this;
  209. }
  210. /**
  211. * Get description
  212. *
  213. * @return string
  214. */
  215. public function getDescription()
  216. {
  217. return $this->description;
  218. }
  219. /**
  220. * Set template
  221. *
  222. * @param text $template
  223. *
  224. * @return Workflow
  225. */
  226. public function setTemplate($template)
  227. {
  228. $array = Yaml::parse($template);
  229. $text = Yaml::dump($array,100,2);
  230. $this->template = $text;
  231. return $this;
  232. }
  233. /**
  234. * Get template
  235. *
  236. * @return text
  237. */
  238. public function getTemplate()
  239. {
  240. return $this->template;
  241. }
  242. /**
  243. * Get created
  244. *
  245. * @return \DateTime
  246. */
  247. public function getCreated()
  248. {
  249. return $this->created;
  250. }
  251. /**
  252. * Get updated
  253. *
  254. * @return \DateTime
  255. */
  256. public function getUpdated()
  257. {
  258. return $this->updated;
  259. }
  260. /**
  261. * @param Action $action
  262. *
  263. * @return Workflow
  264. */
  265. public function addAction(Action $action)
  266. {
  267. $this->actions[] = $action;
  268. return $this;
  269. }
  270. /**
  271. * @param Action $action
  272. *
  273. * @return Workflow
  274. */
  275. public function removeAction(Action $action)
  276. {
  277. $this->actions->removeElement($action);
  278. return $this;
  279. }
  280. /**
  281. * @return Doctrine\Common\Collections\Collection
  282. */
  283. public function getActions()
  284. {
  285. return $this->actions;
  286. }
  287. /**
  288. * Set tenancyId
  289. *
  290. * @param int $tenancyId
  291. *
  292. * @return Workflow
  293. */
  294. public function setTenancyId($tenancyId)
  295. {
  296. $this->tenancyId = $tenancyId;
  297. return $this;
  298. }
  299. /**
  300. * Get tenancyId
  301. *
  302. * @return int
  303. */
  304. public function getTenancyId()
  305. {
  306. return $this->tenancyId;
  307. }
  308. /**
  309. * Set enable
  310. *
  311. * @param boolean $enable
  312. * @return Workflow
  313. */
  314. public function setEnable($enable)
  315. {
  316. $this->enable = $enable;
  317. return $this;
  318. }
  319. /**
  320. * Get enable
  321. *
  322. * @return boolean
  323. */
  324. public function getEnable()
  325. {
  326. return $this->enable;
  327. }
  328. /**
  329. * @ORM\PostPersist
  330. */
  331. public function postPersist(LifecycleEventArgs $event)
  332. {
  333. $this->updateWorkflows();
  334. $this->reloadCache();
  335. }
  336. /**
  337. * @ORM\PostUpdate
  338. */
  339. public function postUpdate(LifecycleEventArgs $event)
  340. {
  341. $this->updateWorkflows();
  342. $this->reloadCache();
  343. }
  344. /**
  345. * @ORM\PreRemove
  346. */
  347. public function preRemove(LifecycleEventArgs $event)
  348. {
  349. $this->updateWorkflows();
  350. $this->reloadCache();
  351. }
  352. public function __toString()
  353. {
  354. return $this->name;
  355. }
  356. public function updateWorkflows()
  357. {
  358. global $kernel;
  359. $em = $kernel->getContainer()->get("doctrine.orm.entity_manager");
  360. $workflows = $em->getRepository("WorkflowBundle:Workflow")->findBy(array("enable"=>1));
  361. $workflow_template = array();
  362. $workflow_template['framework'] = array();
  363. $templates = array();
  364. foreach($workflows as $k => $workflow) {
  365. $templates[$workflow->getName()] = Yaml::parse($workflow->getTemplate());
  366. }
  367. $workflow_file = $kernel->getRootDir()."/Resources/workflows/workflow_list.yml";
  368. $handle = fopen($workflow_file,"w+");
  369. if($templates) {
  370. $workflow_template['framework'] = array('workflows' => $templates);
  371. $yaml = Yaml::dump($workflow_template,100,2);
  372. fwrite($handle,$yaml);
  373. } else {
  374. fwrite($handle,"");
  375. }
  376. fclose($handle);
  377. chmod($workflow_file, 0777);
  378. }
  379. public function reloadCache()
  380. {
  381. global $kernel;
  382. $console = $kernel->getRootDir()."/../bin/console";
  383. exec("{$console} cache:clear --env=prod");
  384. }
  385. }