BxDolCronQuery.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Database queries for Cron
  11. * @see BxDolCron
  12. */
  13. class BxDolCronQuery extends BxDolDb implements iBxDolSingleton
  14. {
  15. protected function __construct()
  16. {
  17. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  18. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  19. parent::__construct();
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. /**
  30. * Get singleton instance of the class
  31. */
  32. static function getInstance()
  33. {
  34. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolCronQuery();
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. public function getJobs()
  39. {
  40. return $this->fromCache('sys_cron_jobs', 'getAll', "SELECT * FROM `sys_cron_jobs` ORDER BY `timing` ASC");
  41. }
  42. public function updateJob($iId, $aFields = array())
  43. {
  44. $sQuery = $this->prepare("UPDATE `sys_cron_jobs` SET " . $this->arrayToSQL($aFields) . " WHERE `id` = ?", $iId);
  45. return $this->query($sQuery);
  46. }
  47. public function addTransientJobClass($sName, $sClass, $sFile)
  48. {
  49. $sQuery = $this->prepare("INSERT INTO `sys_cron_jobs` SET `name`=?, `time`='transient', `class`=?, `file`=?", $sName, $sClass, $sFile);
  50. return (int)$this->query($sQuery) > 0;
  51. }
  52. public function isTransientJobService($sName)
  53. {
  54. return (int)$this->getOne("SELECT `id` FROM `sys_cron_jobs` WHERE `name`=:name LIMIT 1", ['name' => $sName]) > 0;
  55. }
  56. public function addTransientJobService($sName, $mixedService)
  57. {
  58. if(is_array($mixedService))
  59. $mixedService = call_user_func_array('BxDolService::getSerializedService', $mixedService);
  60. if(!BxDolService::isSerializedService($mixedService))
  61. return false;
  62. $sQuery = $this->prepare("INSERT INTO `sys_cron_jobs` SET `name`=?, `time`='transient', `service_call`=?", $sName, $mixedService);
  63. return (int)$this->query($sQuery) > 0;
  64. }
  65. public function getTransientJobs()
  66. {
  67. return $this->getAllWithKey("SELECT * FROM `sys_cron_jobs` WHERE `time`='transient'", 'name');
  68. }
  69. public function deleteTransientJobs()
  70. {
  71. return $this->query("DELETE FROM `sys_cron_jobs` WHERE `time`='transient'");
  72. }
  73. }
  74. /** @} */