PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/web/concrete/models/file_set.php

https://github.com/ayamyau/concrete5
PHP | 336 lines | 254 code | 41 blank | 41 comment | 30 complexity | 854fd535065d20d81e4ac5fcf7ae5cf7 MD5 | raw file
  1. <?php
  2. class FileSetList extends DatabaseItemList {
  3. public $sets = array();
  4. protected $itemsPerPage = 10;
  5. public function filterByKeywords($kw) {
  6. $db = Loader::db();
  7. $this->filter(false, "(FileSets.fsName like " . $db->qstr('%' . $kw . '%') . ")");
  8. }
  9. function __construct() {
  10. $this->setQuery("select FileSets.fsID from FileSets");
  11. $this->sortBy('fsName', 'asc');
  12. }
  13. public function filterByType($fsType) {
  14. switch($fsType) {
  15. case FileSet::TYPE_PRIVATE:
  16. $u = new User();
  17. $this->filter('FileSets.uID', $u->getUserID());
  18. break;
  19. }
  20. $this->filter('FileSets.fsType', $fsType);
  21. }
  22. public function get($itemsToGet = 0, $offset = 0) {
  23. $r = parent::get($itemsToGet, $offset);
  24. foreach($r as $row) {
  25. $fs = FileSet::getByID($row['fsID']);
  26. if (is_object($fs)) {
  27. $this->sets[] = $fs;
  28. }
  29. }
  30. return $this->sets;
  31. }
  32. }
  33. class FileSet extends Model {
  34. const TYPE_PRIVATE = 0;
  35. const TYPE_PUBLIC = 1;
  36. const TYPE_STARRED = 2;
  37. const TYPE_SAVED_SEARCH = 3;
  38. protected $fileSetFiles;
  39. /**
  40. * Returns an object mapping to the global file set, fsID = 0.
  41. * This is really only used for permissions mapping
  42. */
  43. public function getGlobal() {
  44. $fs = new FileSet;
  45. $fs->fsID = 0;
  46. return $fs;
  47. }
  48. public function getFileSetUserID() {return $this->uID;}
  49. public function getFileSetType() {return $this->fsType;}
  50. public function getSavedSearches() {
  51. $db = Loader::db();
  52. $sets = array();
  53. $u = new User();
  54. $r = $db->Execute('select * from FileSets where fsType = ? and uID = ? order by fsName asc', array(FileSet::TYPE_SAVED_SEARCH, $u->getUserID()));
  55. while ($row = $r->FetchRow()) {
  56. $fs = new FileSet();
  57. foreach($row as $key => $value) {
  58. $fs->{$key} = $value;
  59. }
  60. $sets[] = $fs;
  61. }
  62. return $sets;
  63. }
  64. public function getMySets($u = false) {
  65. if ($u == false) {
  66. $u = new User();
  67. }
  68. $db = Loader::db();
  69. $sets = array();
  70. $r = $db->Execute('select * from FileSets where fsType = ? or (fsType in (?, ?) and uID = ?) order by fsName asc', array(FileSet::TYPE_PUBLIC, FileSet::TYPE_STARRED, FileSet::TYPE_PRIVATE, $u->getUserID()));
  71. while ($row = $r->FetchRow()) {
  72. $fs = new FileSet();
  73. foreach($row as $key => $value) {
  74. $fs->{$key} = $value;
  75. }
  76. $fsp = new Permissions($fs);
  77. if ($fsp->canSearchFiles()) {
  78. $sets[] = $fs;
  79. }
  80. }
  81. return $sets;
  82. }
  83. public function updateFileSetDisplayOrder($files) {
  84. $db = Loader::db();
  85. $db->Execute('update FileSetFiles set fsDisplayOrder = 0 where fsID = ?', $this->getFileSetID());
  86. $i = 0;
  87. foreach($files as $fID) {
  88. $db->Execute('update FileSetFiles set fsDisplayOrder = ? where fsID = ? and fID = ?', array($i, $this->getFileSetID(), $fID));
  89. $i++;
  90. }
  91. }
  92. /**
  93. * Get a file set object by a file set's id
  94. * @param int $fsID
  95. * @return FileSet
  96. */
  97. public function getByID($fsID) {
  98. $db = Loader::db();
  99. $row = $db->GetRow('select * from FileSets where fsID = ?', array($fsID));
  100. if (is_array($row)) {
  101. $fs = new FileSet();
  102. foreach($row as $key => $value) {
  103. $fs->{$key} = $value;
  104. }
  105. if ($row['fsType'] == FileSet::TYPE_SAVED_SEARCH) {
  106. $row2 = $db->GetRow('select fsSearchRequest, fsResultColumns from FileSetSavedSearches where fsID = ?', array($fsID));
  107. $fs->fsSearchRequest = @unserialize($row2['fsSearchRequest']);
  108. $fs->fsResultColumns = @unserialize($row2['fsResultColumns']);
  109. }
  110. return $fs;
  111. }
  112. }
  113. /**
  114. * Get a file set object by a file name
  115. * @param string $fsName
  116. * @return FileSet
  117. */
  118. public function getByName($fsName) {
  119. $db = Loader::db();
  120. $row = $db->GetRow('select * from FileSets where fsName = ?', array($fsName));
  121. if (is_array($row) && count($row)) {
  122. $fs = new FileSet();
  123. foreach($row as $key => $value) {
  124. $fs->{$key} = $value;
  125. }
  126. return $fs;
  127. }
  128. }
  129. public function getFileSetID() {return $this->fsID;}
  130. public function overrideGlobalPermissions() {return $this->fsOverrideGlobalPermissions;}
  131. public function getFileSetName() {return $this->fsName;}
  132. /**
  133. * Creats a new fileset if set doesn't exists
  134. *
  135. * If we find a multiple groups with the same properties,
  136. * we return an array containing each group
  137. * @param string $fs_name
  138. * @param int $fs_type
  139. * @param int $fs_uid
  140. * @return Mixed
  141. *
  142. * Dev Note: This will create duplicate sets with the same name if a set exists owned by another user!!!
  143. */
  144. public static function createAndGetSet($fs_name, $fs_type, $fs_uid=false) {
  145. if (!$fs_uid) {
  146. $u = new User();
  147. $fs_uid = $u->uID;
  148. }
  149. $file_set = new FileSet();
  150. $criteria = array($fs_name,$fs_type,$fs_uid);
  151. $matched_sets = $file_set->Find('fsName=? AND fsType=? and uID=?',$criteria);
  152. if (1 === count($matched_sets) ) {
  153. return $matched_sets[0];
  154. }
  155. else if (1 < count($matched_sets)) {
  156. return $matched_sets;
  157. }
  158. else{
  159. //AS: Adodb Active record is complaining a ?/value array mismatch unless
  160. //we explicatly set the primary key ID field to null
  161. $file_set->fsID = null;
  162. $file_set->fsName = $fs_name;
  163. $file_set->fsOverrideGlobalPermissions = 0;
  164. $file_set->fsType = $fs_type;
  165. $file_set->uID = $fs_uid;
  166. $file_set->save();
  167. return $file_set;
  168. }
  169. }
  170. /**
  171. * Adds the file to the set
  172. * @param type $fID //accepts an ID or a File object
  173. * @return object
  174. */
  175. public function addFileToSet($f_id) {
  176. if (is_object($f_id)) {
  177. $f_id = $f_id->getFileID();
  178. }
  179. $file_set_file = FileSetFile::createAndGetFile($f_id,$this->fsID);
  180. return $file_set_file;
  181. }
  182. public function getSavedSearchRequest() {
  183. return $this->fsSearchRequest;
  184. }
  185. public function getSavedSearchColumns() {
  186. return $this->fsResultColumns;
  187. }
  188. public function removeFileFromSet($f_id){
  189. if (is_object($f_id)) {
  190. $f_id = $f_id->fID;
  191. }
  192. $db = Loader::db();
  193. $db->Execute('DELETE FROM FileSetFiles
  194. WHERE fID = ?
  195. AND fsID = ?', array($f_id, $this->getFileSetID()));
  196. }
  197. /**
  198. * Get a list of files asociated with this set
  199. *
  200. * Can obsolete this when we get version of ADOdB with one/many support
  201. * @return type $var_name
  202. */
  203. private function populateFiles(){
  204. $utility = new FileSetFile();
  205. $this->fileSetFiles = $utility->Find('fsID=?',array($this->fsID));
  206. }
  207. public function hasFileID($f_id){
  208. if (!is_array($this->fileSetFiles)) {
  209. $this->populateFiles();
  210. }
  211. foreach ($this->fileSetFiles as $file) {
  212. if($file->fID == $f_id){
  213. return true;
  214. }
  215. }
  216. }
  217. public function delete() {
  218. parent::delete();
  219. $db = Loader::db();
  220. $db->Execute('delete from FileSetSavedSearches where fsID = ?', $this->fsID);
  221. }
  222. public function resetPermissions() {
  223. $db = Loader::db();
  224. $db->Execute('delete from FileSetPermissions where fsID = ?', array($this->fsID));
  225. $db->Execute('delete from FilePermissionFileTypes where fsID = ?', array($this->fsID));
  226. }
  227. public function setPermissions($obj, $canSearch, $canRead, $canWrite, $canAdmin, $canAdd, $extensions = array()) {
  228. $fsID = $this->fsID;
  229. $uID = 0;
  230. $gID = 0;
  231. $db = Loader::db();
  232. if (is_a($obj, 'UserInfo')) {
  233. $uID = $obj->getUserID();
  234. } else {
  235. $gID = $obj->getGroupID();
  236. }
  237. if ($canSearch == FilePermissions::PTYPE_NONE) {
  238. $canWrite = FilePermissions::PTYPE_NONE;
  239. $canAdd = FilePermissions::PTYPE_NONE;
  240. $canAdmin = FilePermissions::PTYPE_NONE;
  241. }
  242. $db->Replace('FileSetPermissions', array(
  243. 'fsID' => $fsID,
  244. 'uID' => $uID,
  245. 'gID' => $gID,
  246. 'canRead' => $canRead,
  247. 'canSearch' => $canSearch,
  248. 'canWrite' => $canWrite,
  249. 'canAdmin' => $canAdmin,
  250. 'canAdd' => $canAdd
  251. ),
  252. array('fsID', 'gID', 'uID'), true);
  253. $db->Execute("delete from FilePermissionFileTypes where fsID = ? and gID = ? and uID = ?", array($fsID, $uID, $gID));
  254. if ($canAdd == FilePermissions::PTYPE_CUSTOM && is_array($extensions)) {
  255. foreach($extensions as $e) {
  256. $db->Execute('insert into FilePermissionFileTypes (fsID, gID, uID, extension) values (?, ?, ?, ?)', array($fsID, $gID, $uID, $e));
  257. }
  258. }
  259. }
  260. }
  261. class FileSetFile extends Model {
  262. public static function createAndGetFile($f_id, $fs_id){
  263. $file_set_file = new FileSetFile();
  264. $criteria = array($f_id,$fs_id);
  265. $matched_sets = $file_set_file->Find('fID=? AND fsID=?',$criteria);
  266. if (1 === count($matched_sets) ) {
  267. return $matched_sets[0];
  268. }
  269. else if (1 < count($matched_sets)) {
  270. return $matched_sets;
  271. }
  272. else{
  273. //AS: Adodb Active record is complaining a ?/value array mismatch unless
  274. //we explicatly set the primary key ID field to null
  275. $db = Loader::db();
  276. $fsDisplayOrder = $db->GetOne('select count(fID) from FileSetFiles where fsID = ?', $fs_id);
  277. $file_set_file->fsfID = null;
  278. $file_set_file->fID = $f_id;
  279. $file_set_file->fsID = $fs_id;
  280. $file_set_file->timestamp = null;
  281. $file_set_file->fsDisplayOrder = $fsDisplayOrder;
  282. $file_set_file->Save();
  283. return $file_set_file;
  284. }
  285. }
  286. }
  287. class FileSetSavedSearch extends FileSet {
  288. public static function add($name, $searchRequest, $searchColumnsObject) {
  289. $fs = parent::createAndGetSet($name, FileSet::TYPE_SAVED_SEARCH);
  290. $db = Loader::db();
  291. $v = array($fs->getFileSetID(), serialize($searchRequest), serialize($searchColumnsObject));
  292. $db->Execute('insert into FileSetSavedSearches (fsID, fsSearchRequest, fsResultColumns) values (?, ?, ?)', $v);
  293. return $fs;
  294. }
  295. }