SproutCMS

This is the code documentation for the SproutCMS project

source of /modules/HomePage/Helpers/HomePages.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace SproutModules\Karmabunny\HomePage\Helpers;
  15.  
  16. use Sprout\Helpers\Pdb;
  17. use Sprout\Helpers\SubsiteSelector;
  18.  
  19.  
  20. /**
  21.  * Functions for dealing with promo and banner images on the home page(s)
  22.  */
  23. class HomePages
  24. {
  25. /**
  26.   * Return home page record for given sub-site
  27.   * @param int $subsite_id Sub-site record ID. Default of zero/auto
  28.   * @return array Single database row
  29.   * @throws QueryException
  30.   */
  31. public static function getForSubSite($subsite_id = 0)
  32. {
  33. $subsite_id = (int) $subsite_id;
  34. if ($subsite_id < 1) $subsite_id = SubsiteSelector::$subsite_id;
  35.  
  36. $q = "SELECT home.*
  37. FROM ~homepages AS home
  38. WHERE home.subsite_id = ?";
  39.  
  40. return Pdb::query($q, [$subsite_id], 'row');
  41. }
  42.  
  43.  
  44. /**
  45.   * Return list of active banners for given home page
  46.   *
  47.   * @param int $homepage_id Home page record ID
  48.   * @param int $limit Optional number of results. Default of zero/unlimited
  49.   * @return array Multiple database rows; heading, description, link, link_label, filename
  50.   */
  51. public static function getActiveBanners($homepage_id, $limit = 0)
  52. {
  53. $limit = (int) $limit;
  54.  
  55. $q = "SELECT
  56. banner.heading,
  57. banner.description,
  58. banner.link,
  59. banner.link_label,
  60. file.filename
  61. FROM
  62. ~homepage_banners AS banner
  63. LEFT JOIN
  64. ~files AS file
  65. ON file.id = banner.file_id
  66. WHERE
  67. banner.active != 0
  68. AND banner.homepage_id = ?
  69. GROUP BY
  70. banner.id
  71. ORDER BY
  72. banner.record_order";
  73.  
  74. if ($limit > 0) $q .= sprintf(' LIMIT %u', $limit);
  75.  
  76. return Pdb::query($q, [$homepage_id], 'arr');
  77. }
  78.  
  79.  
  80. /**
  81.   * Return list of active promos for given home page
  82.   *
  83.   * @param int $homepage_id Home page record ID
  84.   * @param int $limit Optional number of results. Default of zero/unlimited
  85.   * @return array Multiple database rows; heading, description, link, link_label, filename
  86.   */
  87. public static function getActivePromos($homepage_id, $limit)
  88. {
  89. $limit = (int) $limit;
  90.  
  91. $q = "SELECT
  92. promo.heading,
  93. promo.description,
  94. promo.link,
  95. promo.link_label,
  96. file.filename
  97. FROM
  98. ~homepage_promos AS promo
  99. LEFT JOIN
  100. ~files AS file
  101. ON file.id = promo.file_id
  102. WHERE
  103. promo.active != 0
  104. AND promo.homepage_id = ?
  105. GROUP BY
  106. promo.id
  107. ORDER BY
  108. promo.record_order";
  109.  
  110. if ($limit > 0) $q .= sprintf(' LIMIT %u', $limit);
  111.  
  112. return Pdb::query($q, [$homepage_id], 'arr');
  113. }
  114.  
  115.  
  116. /**
  117.   * Return a randomly selected active banner for given home page
  118.   *
  119.   * @param int $homepage_id Home page record ID
  120.   * @return array Single database row; heading, description, link, link_label, filename
  121.   * @throws QueryException
  122.   */
  123. public static function getRandomActiveBanner($homepage_id)
  124. {
  125. $q = "SELECT
  126. banner.heading,
  127. banner.description,
  128. banner.link,
  129. banner.link_label,
  130. file.filename
  131. FROM
  132. ~homepage_banners AS banner
  133. INNER JOIN
  134. ~files AS file
  135. ON file.id = banner.file_id
  136. WHERE
  137. banner.active != 0
  138. AND banner.homepage_id = ?
  139. GROUP BY
  140. banner.id
  141. ORDER BY
  142. RAND()
  143. LIMIT 1";
  144.  
  145. return Pdb::query($q, [$homepage_id], 'row');
  146. }
  147. }
  148.