SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/RteLibrarySounds.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. * Richtext library for media repository sounds, they launch in a popup audio player
  18. **/
  19. class RteLibrarySounds extends RteLibrary
  20. {
  21. protected $name = 'Media repository - audio';
  22. private $db;
  23.  
  24.  
  25. public function __construct()
  26. {
  27. }
  28.  
  29.  
  30. /**
  31.   * Do a library browse
  32.   *
  33.   * @return array of RteLibContainer and RteLibObject objects
  34.   **/
  35. public function browse($path)
  36. {
  37. $path_parts = explode('/', $path);
  38.  
  39. if ($path == '') {
  40. // Get categories
  41. $q = "SELECT cat.id, cat.name
  42. FROM ~files_cat_list AS cat
  43. INNER JOIN ~files_cat_join AS joiner ON joiner.cat_id = cat.id
  44. INNER JOIN ~files AS files ON joiner.file_id = files.id
  45. WHERE files.type = ?
  46. AND cat.show_admin = 1
  47. GROUP BY cat.id
  48. ORDER BY cat.name";
  49. $res = Pdb::query($q, [FileConstants::TYPE_SOUND], 'pdo');
  50.  
  51. // Convert into 'Container' objects for rendering
  52. $out = array();
  53. foreach ($res as $row) {
  54. $out[] = new RteLibContainer($row['id'], $row['name']);
  55. }
  56. $res->closeCursor();
  57.  
  58. return $out;
  59.  
  60. } else if (count($path_parts) == 1) {
  61. // file list for files in category
  62. $cat_id = (int) $path_parts[0];
  63. $q = "SELECT files.name, files.filename, files.date_modified
  64. FROM ~files AS files
  65. INNER JOIN ~files_cat_join AS joiner ON joiner.file_id = files.id
  66. WHERE joiner.cat_id = ? AND files.type = ?
  67. ORDER BY files.name";
  68. $res = Pdb::query($q, [$cat_id, FileConstants::TYPE_SOUND], 'pdo');
  69.  
  70. // Convert to 'Object' objects for rendering
  71. $out = array();
  72. foreach ($res as $row) {
  73. $out[] = new RteLibObject(
  74. $row['filename'],
  75. $row['name'],
  76. 'href' => 'file/play_audio/' . $row['filename'],
  77. 'title' => $row['name'],
  78. ),
  79. 'date' => $row['date_modified'],
  80. 'size' => File::size($row['filename']),
  81. )
  82. );
  83. }
  84. $res->closeCursor();
  85.  
  86. return $out;
  87. }
  88. }
  89.  
  90.  
  91. /**
  92.   * Do a library search
  93.   *
  94.   * @return array of RteLibContainer and RteLibObject objects
  95.   **/
  96. public function search($term)
  97. {
  98. $conditions = array();
  99. $conditions[] = ['files.name', 'CONTAINS', $term];
  100. $conditions[] = ['files.filename', 'CONTAINS', $term];
  101.  
  102. $params = [FileConstants::TYPE_SOUND];
  103. $where = Pdb::buildClause($conditions, $params, 'OR');
  104.  
  105. $q = "SELECT files.name, files.filename, files.date_modified
  106. FROM ~files AS files
  107. INNER JOIN ~files_cat_join AS joiner ON joiner.file_id = files.id
  108. WHERE files.type = ?
  109. AND ({$where})
  110. ORDER BY files.name";
  111. $res = Pdb::query($q, $params, 'pdo');
  112.  
  113. // Convert to 'Object' objects for rendering
  114. $out = array();
  115. foreach ($res as $row) {
  116. $out[] = new RteLibObject(
  117. $row['filename'],
  118. $row['name'],
  119. 'href' => 'file/play_audio/' . $row['filename'],
  120. 'title' => $row['name'],
  121. ),
  122. 'date' => $row['date_modified'],
  123. 'size' => File::size($row['filename']),
  124. )
  125. );
  126. }
  127. $res->closeCursor();
  128.  
  129. return $out;
  130. }
  131.  
  132. }
  133.