SproutCMS

This is the code documentation for the SproutCMS project

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