SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/TreenodeHasWidgetMatcher.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 Sprout\Helpers;
  15.  
  16. /**
  17. * @package Tree
  18. **/
  19.  
  20. /**
  21. * Matches a node which has a specific key matching a specific value
  22. **/
  23. class TreenodeHasWidgetMatcher implements TreenodeMatcher
  24. {
  25. private $ids = array();
  26.  
  27.  
  28. /**
  29.   * Constructor
  30.   *
  31.   * @param string $type The type of widget to search
  32.   **/
  33. public function __construct($type)
  34. {
  35. $q = "SELECT rev.page_id
  36. FROM ~page_revisions AS rev
  37. INNER JOIN ~page_widgets AS widget ON widget.page_revision_id = rev.id AND widget.active = 1
  38. WHERE widget.type = ?
  39. AND rev.status = 'live'
  40. ORDER BY rev.page_id";
  41.  
  42. $this->ids = Pdb::query($q, [$type], 'col');
  43. }
  44.  
  45.  
  46. /**
  47.   * Does the match
  48.   *
  49.   * @param TreeNode $node The treenode to do matching against
  50.   * @return True if the node matches, false otherwise
  51.   **/
  52. public function match($node)
  53. {
  54. if (in_array($node['id'], $this->ids)) return true;
  55. return false;
  56. }
  57.  
  58. /**
  59.   * Returns true if the children of the specified node should be searched, false otherwise.
  60.   *
  61.   * @param TreeNode $node The treenode which is about to be descended into
  62.   * @return True descending should proceed, false otherwise
  63.   **/
  64. public function descend($node)
  65. {
  66. return true;
  67. }
  68.  
  69. /**
  70.   * Called after children have been processed
  71.   *
  72.   * @param TreeNode $node The treenode which has just ascended.
  73.   **/
  74. public function ascend($node) {}
  75. }
  76.  
  77.  
  78.