/webapp-php/application/libraries/MY_QueryFormHelper.php

https://github.com/AlinT/socorro · PHP · 144 lines · 68 code · 11 blank · 65 comment · 4 complexity · e563ff6e0094fa47a3cc05b042063f82 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is Socorro Crash Reporter
  16. *
  17. * The Initial Developer of the Original Code is
  18. * The Mozilla Foundation.
  19. * Portions created by the Initial Developer are Copyright (C) 2006
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Austin King <aking@mozilla.com> (Original Author)
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either the GNU General Public License Version 2 or later (the "GPL"), or
  27. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. require_once(Kohana::find_file('libraries', 'versioncompare', TRUE, 'php'));
  39. require_once(Kohana::find_file('libraries', 'release', TRUE, 'php'));
  40. /**
  41. * Common code between query and home controllers. Prepares data for query form.
  42. * NOTE: This is not a 'helper' in the sense of Kohana's helper classes (static
  43. * methods to be used from the view)
  44. */
  45. class QueryFormHelper
  46. {
  47. /**
  48. * Prepare the branch and platform data for the view.
  49. *
  50. * @param Model The Branch Model
  51. * @param Model The Platform Model
  52. * @return array An array consisting of all of the products, branches, versions, versions by product and platforms.
  53. */
  54. public function prepareCommonViewData($branch_model, $platform_model)
  55. {
  56. $branch_data = $branch_model->getBranchData();
  57. $platforms = $platform_model->getAll();
  58. $versions_by_product = array();
  59. foreach($branch_data['versions'] as $version){
  60. if (!array_key_exists($version->product, $versions_by_product)) {
  61. $versions_by_product[$version->product] = array();
  62. }
  63. array_push($versions_by_product[$version->product], $version);
  64. }
  65. $versions_by_product_reversed = array();
  66. foreach ($versions_by_product as $product => $versions) {
  67. $versions_by_product_reversed[$product] = array_reverse($versions);
  68. }
  69. return array(
  70. 'all_products' => $branch_data['products'],
  71. 'all_branches' => $branch_data['branches'],
  72. 'all_versions' => $branch_data['versions'],
  73. 'versions_by_product' => $versions_by_product,
  74. 'all_platforms' => $platforms
  75. );
  76. }
  77. /**
  78. * Prepare the branch and platform data for the view.
  79. *
  80. * @param Model The Branch Model
  81. * @return array An array of versions ordered by product
  82. */
  83. public function prepareAllProducts($branch_model)
  84. {
  85. $branch_data = $branch_model->getBranchData();
  86. $versionCompare = new VersioncompareComponent();
  87. $versions_by_product = array();
  88. foreach($branch_data['products'] as $product){
  89. $versions_by_product[$product] = array();
  90. }
  91. foreach($branch_data['versions'] as $version){
  92. if (!array_key_exists($version->product, $versions_by_product)) {
  93. $versions_by_product[$version->product] = array();
  94. }
  95. array_push($versions_by_product[$version->product], $version->version);
  96. }
  97. foreach ($versions_by_product as $versions) {
  98. $versionCompare->sortAppversionArray($versions);
  99. }
  100. return $versions_by_product;
  101. }
  102. /**
  103. * Given an array with the format product to version list,
  104. * this function will return an array of the current released
  105. * versions of each products.
  106. *
  107. * @param array - Input Example: {'Firefox': ['3.5', '3.0.10'], ...}
  108. * @return array -
  109. * Output Example: {'Firefox': {'major': '3.5',
  110. * 'milestone': '3.5b99',
  111. * 'development': '3.6pre'} ...}
  112. */
  113. public function currentProducts($products2versions)
  114. {
  115. $current = array();
  116. $release = new Release;
  117. foreach ($products2versions as $product => $versions) {
  118. if (count($versions) > 0) {
  119. foreach (array_reverse($versions) as $v) {
  120. $release_type = $release->typeOfRelease($v);
  121. if (
  122. ! array_key_exists($product, $current) ||
  123. ! array_key_exists($release_type, $current[$product])
  124. ) {
  125. $current[$product][$release_type] = $v;
  126. }
  127. }
  128. }
  129. }
  130. uksort($current, 'strcasecmp');
  131. return $current;
  132. }
  133. }