PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tasks/SilvercartProductExport.php

https://bitbucket.org/silvercart/silvercart/
PHP | 190 lines | 83 code | 17 blank | 90 comment | 13 complexity | 6450c6cb69559fced1580b224dd44a42 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011 pixeltricks GmbH
  4. *
  5. * This file is part of SilverCart.
  6. *
  7. * SilverCart is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SilverCart is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SilverCart. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package Silvercart
  21. * @subpackage Tasks
  22. */
  23. /**
  24. * Calls all due exports.
  25. *
  26. * This task should be called via sake on the command line:
  27. * "sake /SilvercartProductExport"
  28. *
  29. * You can set the following parameters:
  30. * "getAll": generate all product exports regardless of their update interval settings
  31. *
  32. * Example with parameters:
  33. *
  34. * Generate all exports regardless of their update interval settings:
  35. * sake /SilvercartProductExport getAll
  36. *
  37. * @package Silvercart
  38. * @subpackage Tasks
  39. * @author Sascha Koehler <skoehler@pixeltricks.de>
  40. * @copyright 2011 pixeltricks GmbH
  41. * @since 08.07.2011
  42. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  43. */
  44. class SilvercartProductExport extends ScheduledTask {
  45. /**
  46. * This method gets called from sake.
  47. *
  48. * You can give the argument "getAll" to generate all product exports
  49. * regardless of their update interval settings.
  50. *
  51. * It starts the export of all due SilvercartProductExporter objects.
  52. *
  53. * @return void
  54. *
  55. * @author Sascha Koehler <skoehler@pixeltricks.de>
  56. * @copyright 2011 pixeltricks GmbH
  57. * @since 08.07.2011
  58. */
  59. public function process() {
  60. $getAll = false;
  61. if (isset($_GET['args']) &&
  62. is_array($_GET['args'])) {
  63. foreach ($_GET['args'] as $argument) {
  64. if ($argument == 'getAll') {
  65. $getAll = true;
  66. }
  67. }
  68. }
  69. $referenceCurrentTimeStamp = time();
  70. $silvercartProductExporters = $this->getDueSilvercartProductExporters($referenceCurrentTimeStamp, $getAll);
  71. foreach ($silvercartProductExporters as $silvercartProductExporter) {
  72. $silvercartProductExporter->doExport($referenceCurrentTimeStamp);
  73. }
  74. return true;
  75. }
  76. /**
  77. * This method returns an array containing all SilvercartProductExporters
  78. * that should be run now (according to their update interval settings).
  79. *
  80. * @param Int $referenceCurrentTimeStamp The timestamp to use as reference
  81. * for calculating if an update is due.
  82. * @param Boolean $getAll Indicates wether all exporters should be returned or
  83. * only those whose update intervals are due.
  84. *
  85. * @return array
  86. *
  87. * @author Sascha Koehler <skoehler@pixeltricks.de>
  88. * @copyright 2011 pixeltricks GmbH
  89. * @since 08.07.2011
  90. */
  91. protected function getDueSilvercartProductExporters($referenceCurrentTimeStamp, $getAll = false) {
  92. $dueSilvercartProductExporters = array();
  93. $conversionTable = array(
  94. 'Minutes' => 1,
  95. 'Hours' => 60,
  96. 'Days' => 1440,
  97. 'Weeks' => 10080,
  98. 'Months' => 312480,
  99. 'Years' => 3749760
  100. );
  101. $silvercartProductExporters = DataObject::get(
  102. 'SilvercartProductExporter',
  103. "isActive = 1"
  104. );
  105. foreach ($silvercartProductExporters as $silvercartProductExport) {
  106. if ($getAll) {
  107. $dueSilvercartProductExporters[] = $silvercartProductExport;
  108. } else {
  109. // Get difference in minutes from now to the last export
  110. $lastExportTimeStamp = strtotime($silvercartProductExport->lastExportDateTime);
  111. $timeStampDifference = $referenceCurrentTimeStamp - $lastExportTimeStamp;
  112. $differenceInMinutes = $this->time_duration($timeStampDifference, 'm');
  113. // Get update interval in minutes
  114. $intervalLengthInMinutes = $silvercartProductExport->updateInterval * $conversionTable[$silvercartProductExport->updateIntervalPeriod];
  115. if ($differenceInMinutes > $intervalLengthInMinutes) {
  116. $dueSilvercartProductExporters[] = $silvercartProductExport;
  117. }
  118. }
  119. }
  120. return $dueSilvercartProductExporters;
  121. }
  122. /**
  123. * A function for making time periods readable
  124. *
  125. * @param int $seconds number of seconds elapsed
  126. * @param string $use which time periods to display
  127. * @param bool $zeros whether to show zero time periods
  128. *
  129. * @return string
  130. *
  131. * @author Aidan Lister <aidan@php.net>
  132. * @version 2.0.1
  133. * @link http://aidanlister.com/2004/04/making-time-periods-readable/
  134. * @since 08.07.2011
  135. */
  136. protected function time_duration($seconds, $use = null, $zeros = false) {
  137. // Define time periods
  138. $periods = array (
  139. 'years' => 31556926,
  140. 'Months' => 2629743,
  141. 'weeks' => 604800,
  142. 'days' => 86400,
  143. 'hours' => 3600,
  144. 'minutes' => 60,
  145. 'seconds' => 1
  146. );
  147. // Break into periods
  148. $seconds = (float) $seconds;
  149. $segments = array();
  150. foreach ($periods as $period => $value) {
  151. if ($use && strpos($use, $period[0]) === false) {
  152. continue;
  153. }
  154. $count = floor($seconds / $value);
  155. if ($count == 0 && !$zeros) {
  156. continue;
  157. }
  158. $segments[strtolower($period)] = $count;
  159. $seconds = $seconds % $value;
  160. }
  161. // Build the string
  162. $string = array();
  163. foreach ($segments as $key => $value) {
  164. $segment_name = substr($key, 0, -1);
  165. $segment = $value . ' ' . $segment_name;
  166. if ($value != 1) {
  167. $segment .= 's';
  168. }
  169. $string[] = $segment;
  170. }
  171. return implode(', ', $string);
  172. }
  173. }