vendor/pimcore/pimcore/models/Element/Traits/ScheduledTasksTrait.php line 64

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Pimcore
  5. *
  6. * This source file is available under two different licenses:
  7. * - GNU General Public License version 3 (GPLv3)
  8. * - Pimcore Commercial License (PCL)
  9. * Full copyright and license information is available in
  10. * LICENSE.md which is distributed with this source code.
  11. *
  12. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13. * @license http://www.pimcore.org/license GPLv3 and PCL
  14. */
  15. namespace Pimcore\Model\Element\Traits;
  16. use Pimcore\Model\Element\Service;
  17. use Pimcore\Model\Schedule\Task;
  18. use Pimcore\Model\Schedule\Task\Listing;
  19. /**
  20. * @internal
  21. */
  22. trait ScheduledTasksTrait
  23. {
  24. /**
  25. * Contains all scheduled tasks
  26. *
  27. * @var Task[]|null
  28. */
  29. protected $scheduledTasks;
  30. /**
  31. * @return Task[] the $scheduledTasks
  32. */
  33. public function getScheduledTasks()
  34. {
  35. if ($this->scheduledTasks === null) {
  36. $taskList = new Listing();
  37. $ctype = Service::getElementType($this);
  38. $taskList->setCondition('`cid` = ? AND `ctype` = ?', [$this->getId(), $ctype]);
  39. $this->setScheduledTasks($taskList->load());
  40. }
  41. return $this->scheduledTasks;
  42. }
  43. /**
  44. * @param Task[] $scheduledTasks
  45. *
  46. * @return $this
  47. */
  48. public function setScheduledTasks($scheduledTasks)
  49. {
  50. $this->scheduledTasks = $scheduledTasks;
  51. return $this;
  52. }
  53. public function saveScheduledTasks()
  54. {
  55. $scheduledTasks = $this->getScheduledTasks();
  56. $ignoreIds = [];
  57. $ctype = Service::getElementType($this);
  58. foreach ($scheduledTasks as $task) {
  59. $task->setDao(null);
  60. $task->setCid($this->getId());
  61. $task->setCtype($ctype);
  62. $task->save();
  63. $ignoreIds[] = $task->getId();
  64. }
  65. $this->getDao()->deleteAllTasks($ignoreIds);
  66. }
  67. }