PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/core/models/file_set.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 398 lines | 290 code | 51 blank | 57 comment | 39 complexity | abf85070ab47ccfcdf64b387af480d2d MD5 | raw file
  1. <?php defined('C5_EXECUTE') or die("Access Denied.");
  2. class Concrete5_Model_FileSetList extends DatabaseItemList {
  3. protected $itemsPerPage = 10;
  4. public function filterByKeywords($kw) {
  5. $db = Loader::db();
  6. $this->filter(false, "(FileSets.fsName like " . $db->qstr('%' . $kw . '%') . ")");
  7. }
  8. function __construct() {
  9. $this->setQuery("select FileSets.fsID from FileSets");
  10. $this->sortBy('fsName', 'asc');
  11. }
  12. public function filterByType($fsType) {
  13. switch($fsType) {
  14. case FileSet::TYPE_PRIVATE:
  15. $u = new User();
  16. $this->filter('FileSets.uID', $u->getUserID());
  17. break;
  18. }
  19. $this->filter('FileSets.fsType', $fsType);
  20. }
  21. public function get($itemsToGet = 0, $offset = 0) {
  22. $sets = array();
  23. $r = parent::get($itemsToGet, $offset);
  24. foreach($r as $row) {
  25. $fs = FileSet::getByID($row['fsID']);
  26. if (is_object($fs)) {
  27. $sets[] = $fs;
  28. }
  29. }
  30. return $sets;
  31. }
  32. }
  33. class Concrete5_Model_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. $fs->Set($row);
  58. $sets[] = $fs;
  59. }
  60. return $sets;
  61. }
  62. public function getPermissionObjectIdentifier() {
  63. return $this->getFileSetID();
  64. }
  65. public function getMySets($u = false) {
  66. if ($u == false) {
  67. $u = new User();
  68. }
  69. $db = Loader::db();
  70. $sets = array();
  71. $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()));
  72. while ($row = $r->FetchRow()) {
  73. $fs = new FileSet();
  74. $fs->Set($row);
  75. $fsp = new Permissions($fs);
  76. if ($fsp->canSearchFiles()) {
  77. $sets[] = $fs;
  78. }
  79. }
  80. return $sets;
  81. }
  82. public function updateFileSetDisplayOrder($files) {
  83. $db = Loader::db();
  84. $db->Execute('update FileSetFiles set fsDisplayOrder = 0 where fsID = ?', $this->getFileSetID());
  85. $i = 0;
  86. if (is_array($files)) {
  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. /**
  94. * Get a file set object by a file set's id
  95. * @param int $fsID
  96. * @return FileSet
  97. */
  98. public static function getByID($fsID) {
  99. $db = Loader::db();
  100. $row = $db->GetRow('select * from FileSets where fsID = ?', array($fsID));
  101. if (is_array($row)) {
  102. $fs = new FileSet();
  103. $fs->Set($row);
  104. if ($row['fsType'] == FileSet::TYPE_SAVED_SEARCH) {
  105. $row2 = $db->GetRow('select fsSearchRequest, fsResultColumns from FileSetSavedSearches where fsID = ?', array($fsID));
  106. $fs->fsSearchRequest = @unserialize($row2['fsSearchRequest']);
  107. $fs->fsResultColumns = @unserialize($row2['fsResultColumns']);
  108. }
  109. return $fs;
  110. }
  111. }
  112. /**
  113. * Get a file set object by a file name
  114. * @param string $fsName
  115. * @return FileSet
  116. */
  117. public static function getByName($fsName) {
  118. $db = Loader::db();
  119. $row = $db->GetRow('select * from FileSets where fsName = ?', array($fsName));
  120. if (is_array($row) && count($row)) {
  121. $fs = new FileSet();
  122. $fs->Set($row);
  123. return $fs;
  124. }
  125. }
  126. public function getFileSetID() {
  127. if ($this->fsID) {
  128. return $this->fsID;
  129. }
  130. return 0;
  131. }
  132. public function overrideGlobalPermissions() {return $this->fsOverrideGlobalPermissions;}
  133. public function getFileSetName() {return $this->fsName;}
  134. /**
  135. * Creats a new fileset if set doesn't exists
  136. *
  137. * If we find a multiple groups with the same properties,
  138. * we return an array containing each group
  139. * @param string $fs_name
  140. * @param int $fs_type
  141. * @param int $fs_uid
  142. * @return Mixed
  143. *
  144. * Dev Note: This will create duplicate sets with the same name if a set exists owned by another user!!!
  145. */
  146. public static function createAndGetSet($fs_name, $fs_type, $fs_uid=false) {
  147. if (!$fs_uid) {
  148. $u = new User();
  149. $fs_uid = $u->uID;
  150. }
  151. $file_set = new FileSet();
  152. $criteria = array($fs_name,$fs_type,$fs_uid);
  153. $matched_sets = $file_set->Find('fsName=? AND fsType=? and uID=?',$criteria);
  154. if (1 === count($matched_sets) ) {
  155. return $matched_sets[0];
  156. }
  157. else if (1 < count($matched_sets)) {
  158. return $matched_sets;
  159. }
  160. else{
  161. //AS: Adodb Active record is complaining a ?/value array mismatch unless
  162. //we explicatly set the primary key ID field to null
  163. $file_set->fsID = null;
  164. $file_set->fsName = $fs_name;
  165. $file_set->fsOverrideGlobalPermissions = 0;
  166. $file_set->fsType = $fs_type;
  167. $file_set->uID = $fs_uid;
  168. $file_set->save();
  169. $db = Loader::db();
  170. $fsID = $db->Insert_Id();
  171. $fs = FileSet::getByID($fsID);
  172. Events::fire('on_file_set_add', $fs);
  173. return $fs;
  174. }
  175. }
  176. /**
  177. * Adds the file to the set
  178. * @param type $fID //accepts an ID or a File object
  179. * @return object
  180. */
  181. public function addFileToSet($f_id) {
  182. if (is_object($f_id)) {
  183. $f_id = $f_id->getFileID();
  184. }
  185. $file_set_file = FileSetFile::createAndGetFile($f_id,$this->fsID);
  186. Events::fire('on_file_added_to_set', $f_id, $this->getFileSetID());
  187. return $file_set_file;
  188. }
  189. public function getSavedSearchRequest() {
  190. return $this->fsSearchRequest;
  191. }
  192. public function getSavedSearchColumns() {
  193. return $this->fsResultColumns;
  194. }
  195. public function removeFileFromSet($f_id){
  196. if (is_object($f_id)) {
  197. $f_id = $f_id->fID;
  198. }
  199. $db = Loader::db();
  200. $db->Execute('DELETE FROM FileSetFiles
  201. WHERE fID = ?
  202. AND fsID = ?', array($f_id, $this->getFileSetID()));
  203. Events::fire('on_file_removed_from_set', $f_id, $this->getFileSetID());
  204. }
  205. /**
  206. * Get a list of files asociated with this set
  207. *
  208. * Can obsolete this when we get version of ADOdB with one/many support
  209. * @return type $var_name
  210. */
  211. private function populateFiles(){
  212. $utility = new FileSetFile();
  213. $this->fileSetFiles = $utility->Find('fsID=?',array($this->fsID));
  214. }
  215. public function hasFileID($f_id){
  216. if (!is_array($this->fileSetFiles)) {
  217. $this->populateFiles();
  218. }
  219. foreach ($this->fileSetFiles as $file) {
  220. if($file->fID == $f_id){
  221. return true;
  222. }
  223. }
  224. }
  225. /**
  226. * Returns an array of File objects from the current set
  227. * @return array
  228. */
  229. public function getFiles() {
  230. if (!$this->fileSetFiles) { $this->populateFiles(); }
  231. $files = array();
  232. foreach ($this->fileSetFiles as $file) {
  233. $files[] = File::getByID($file->fID);
  234. }
  235. return $files;
  236. }
  237. /**
  238. * Static method to return an array of File objects by the set id
  239. * @param int $fsID
  240. * @return array
  241. */
  242. public static function getFilesBySetID($fsID) {
  243. if (intval($fsID) > 0) {
  244. $fileset = self::getByID($fsID);
  245. if ($fileset instanceof FileSet) {
  246. return $fileset->getFiles();
  247. }
  248. }
  249. }
  250. /**
  251. * Static method to return an array of File objects by the set name
  252. * @param string $fsName
  253. * @return array
  254. */
  255. public static function getFilesBySetName($fsName) {
  256. if (!empty($fsName)) {
  257. $fileset = self::getByName($fsName);
  258. if ($fileset instanceof FileSet) {
  259. return $fileset->getFiles();
  260. }
  261. }
  262. }
  263. public function delete() {
  264. parent::delete();
  265. $db = Loader::db();
  266. $db->Execute('delete from FileSetSavedSearches where fsID = ?', array($this->fsID));
  267. }
  268. public function resetPermissions() {
  269. $db = Loader::db();
  270. $db->Execute('delete from FileSetPermissionAssignments where fsID = ?', array($this->fsID));
  271. }
  272. public function acquireBaseFileSetPermissions() {
  273. $this->resetPermissions();
  274. $db = Loader::db();
  275. $q = "select fsID, paID, pkID from FileSetPermissionAssignments where fsID = 0";
  276. $r = $db->query($q);
  277. while($row = $r->fetchRow()) {
  278. $v = array($this->fsID, $row['paID'], $row['pkID']);
  279. $q = "insert into FileSetPermissionAssignments (fsID, paID, pkID) values (?, ?, ?)";
  280. $db->query($q, $v);
  281. }
  282. }
  283. public function assignPermissions($userOrGroup, $permissions = array(), $accessType = FileSetPermissionKey::ACCESS_TYPE_INCLUDE) {
  284. $db = Loader::db();
  285. if ($this->fsID > 0) {
  286. $db->Execute("update FileSets set fsOverrideGlobalPermissions = 1 where fsID = ?", array($this->fsID));
  287. $this->fsOverrideGlobalPermissions = true;
  288. }
  289. if (is_array($userOrGroup)) {
  290. $pe = GroupCombinationPermissionAccessEntity::getOrCreate($userOrGroup);
  291. // group combination
  292. } else if ($userOrGroup instanceof User || $userOrGroup instanceof UserInfo) {
  293. $pe = UserPermissionAccessEntity::getOrCreate($userOrGroup);
  294. } else {
  295. // group;
  296. $pe = GroupPermissionAccessEntity::getOrCreate($userOrGroup);
  297. }
  298. foreach($permissions as $pkHandle) {
  299. $pk = PermissionKey::getByHandle($pkHandle);
  300. $pk->setPermissionObject($this);
  301. $pa = $pk->getPermissionAccessObject();
  302. if (!is_object($pa)) {
  303. $pa = PermissionAccess::create($pk);
  304. } else if ($pa->isPermissionAccessInUse()) {
  305. $pa = $pa->duplicate();
  306. }
  307. $pa->addListItem($pe, false, $accessType);
  308. $pt = $pk->getPermissionAssignmentObject();
  309. $pt->assignPermissionAccess($pa);
  310. }
  311. }
  312. }
  313. class Concrete5_Model_FileSetFile extends Model {
  314. public static function createAndGetFile($f_id, $fs_id){
  315. $file_set_file = new FileSetFile();
  316. $criteria = array($f_id,$fs_id);
  317. $matched_sets = $file_set_file->Find('fID=? AND fsID=?',$criteria);
  318. if (1 === count($matched_sets) ) {
  319. return $matched_sets[0];
  320. }
  321. else if (1 < count($matched_sets)) {
  322. return $matched_sets;
  323. }
  324. else{
  325. //AS: Adodb Active record is complaining a ?/value array mismatch unless
  326. //we explicatly set the primary key ID field to null
  327. $db = Loader::db();
  328. $fsDisplayOrder = $db->GetOne('select count(fID) from FileSetFiles where fsID = ?', $fs_id);
  329. $file_set_file->fsfID = null;
  330. $file_set_file->fID = $f_id;
  331. $file_set_file->fsID = $fs_id;
  332. $file_set_file->timestamp = null;
  333. $file_set_file->fsDisplayOrder = $fsDisplayOrder;
  334. $file_set_file->Save();
  335. return $file_set_file;
  336. }
  337. }
  338. }
  339. class Concrete5_Model_FileSetSavedSearch extends Concrete5_Model_FileSet {
  340. public static function add($name, $searchRequest, $searchColumnsObject) {
  341. $fs = parent::createAndGetSet($name, FileSet::TYPE_SAVED_SEARCH);
  342. $db = Loader::db();
  343. $v = array($fs->getFileSetID(), serialize($searchRequest), serialize($searchColumnsObject));
  344. $db->Execute('insert into FileSetSavedSearches (fsID, fsSearchRequest, fsResultColumns) values (?, ?, ?)', $v);
  345. return $fs;
  346. }
  347. }