Cron.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace ColdTrick\BlogTools;
  3. /**
  4. * Cron handling
  5. *
  6. * @package ColdTrick
  7. * @subpackage BlogTools
  8. */
  9. class Cron {
  10. /**
  11. * Publish blogs based on advanced publication options
  12. *
  13. * @param string $hook 'cron'
  14. * @param string $type 'daily'
  15. * @param string $return_value optional stdout text
  16. * @param array $params supplied params
  17. *
  18. * @return void
  19. */
  20. public static function daily($hook, $type, $return_value, $params) {
  21. // only do if this is configured
  22. if (!blog_tools_use_advanced_publication_options()) {
  23. return $return_value;
  24. }
  25. $dbprefix = elgg_get_config("dbprefix");
  26. $publication_id = elgg_get_metastring_id("publication_date");
  27. $expiration_id = elgg_get_metastring_id("expiration_date");
  28. $time = elgg_extract("time", $params, time());
  29. $publish_options = array(
  30. "type" => "object",
  31. "subtype" => "blog",
  32. "limit" => false,
  33. "joins" => array(
  34. "JOIN " . $dbprefix . "metadata mdtime ON e.guid = mdtime.entity_guid",
  35. "JOIN " . $dbprefix . "metastrings mstime ON mdtime.value_id = mstime.id"
  36. ),
  37. "metadata_name_value_pairs" => array(
  38. array(
  39. "name" => "status",
  40. "value" => "draft"
  41. )
  42. ),
  43. "wheres" => array("((mdtime.name_id = " . $publication_id . ") AND (DATE(mstime.string) = DATE(NOW())))")
  44. );
  45. $unpublish_options = array(
  46. "type" => "object",
  47. "subtype" => "blog",
  48. "limit" => false,
  49. "joins" => array(
  50. "JOIN " . $dbprefix . "metadata mdtime ON e.guid = mdtime.entity_guid",
  51. "JOIN " . $dbprefix . "metastrings mstime ON mdtime.value_id = mstime.id"
  52. ),
  53. "metadata_name_values_pairs" => array(
  54. array(
  55. "name" => "status",
  56. "value" => "published"
  57. )
  58. ),
  59. "wheres" => array("((mdtime.name_id = " . $expiration_id . ") AND (DATE(mstime.string) = DATE(NOW())))")
  60. );
  61. // ignore access
  62. $ia = elgg_set_ignore_access(true);
  63. // get unpublished blogs that need to be published
  64. $entities = new \ElggBatch("elgg_get_entities_from_metadata", $publish_options);
  65. foreach ($entities as $entity) {
  66. // add river item
  67. elgg_create_river_item(array(
  68. "view" => "river/object/blog/create",
  69. "action_type" => "create",
  70. "subject_guid" => $entity->getOwnerGUID(),
  71. "object_guid" => $entity->getGUID(),
  72. ));
  73. // set correct time created
  74. $entity->time_created = $time;
  75. // publish blog
  76. $entity->status = "published";
  77. // revert access
  78. $entity->access_id = $entity->future_access;
  79. unset($entity->future_access);
  80. // send notifications when post published
  81. elgg_trigger_event('publish', 'object', $entity);
  82. // notify owner
  83. notify_user($entity->getOwnerGUID(),
  84. $entity->site_guid,
  85. elgg_echo("blog_tools:notify:publish:subject"),
  86. elgg_echo("blog_tools:notify:publish:message", array(
  87. $entity->title,
  88. $entity->getURL()
  89. ))
  90. );
  91. // save everything
  92. $entity->save();
  93. }
  94. // get published blogs that need to be unpublished
  95. $entities = new \ElggBatch("elgg_get_entities_from_metadata", $unpublish_options);
  96. foreach ($entities as $entity) {
  97. // remove river item
  98. elgg_delete_river(array(
  99. "object_guid" => $entity->getGUID(),
  100. "action_type" => "create",
  101. ));
  102. // unpublish blog
  103. $entity->status = "draft";
  104. // notify owner
  105. notify_user($entity->getOwnerGUID(),
  106. $entity->site_guid,
  107. elgg_echo("blog_tools:notify:expire:subject"),
  108. elgg_echo("blog_tools:notify:expire:message", array(
  109. $entity->title,
  110. $entity->getURL()
  111. ))
  112. );
  113. // save everything
  114. $entity->save();
  115. }
  116. // reset access
  117. elgg_set_ignore_access($ia);
  118. }
  119. }