This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 
<?php defined('C5_EXECUTE') or die("Access Denied.");


    class Concrete5_Model_FileSetList extends DatabaseItemList {
    
        protected $itemsPerPage = 10;
        
        public function filterByKeywords($kw) {
            $db = Loader::db();
            $this->filter(false, "(FileSets.fsName like " . $db->qstr('%' . $kw . '%') . ")");
        }
        
        function __construct() {
            $this->setQuery("select FileSets.fsID from FileSets");
            $this->sortBy('fsName', 'asc');
        }
        
        public function filterByType($fsType) {
            switch($fsType) {
                case FileSet::TYPE_PRIVATE:
                    $u = new User();
                    $this->filter('FileSets.uID', $u->getUserID());
                    break;
            }
            $this->filter('FileSets.fsType', $fsType);
        }
        
        public function get($itemsToGet = 0, $offset = 0) {
            $sets = array();
            $r = parent::get($itemsToGet, $offset);
            foreach($r as $row) {
                $fs = FileSet::getByID($row['fsID']);
                if (is_object($fs)) {
                    $sets[] = $fs;
                }
            }
            return $sets;
        }

    }
    
    class Concrete5_Model_FileSet extends Model {
        const TYPE_PRIVATE  = 0;
        const TYPE_PUBLIC   = 1;
        const TYPE_STARRED  = 2;
        const TYPE_SAVED_SEARCH = 3;
        protected $fileSetFiles;
    
        /** 
         * Returns an object mapping to the global file set, fsID = 0.
         * This is really only used for permissions mapping
         */
         
        public function getGlobal() {
            $fs = new FileSet;
            $fs->fsID = 0;
            return $fs;
        }
        
        public function getFileSetUserID() {return $this->uID;}
        public function getFileSetType() {return $this->fsType;}
        
        public function getSavedSearches() {
            $db = Loader::db();
            $sets = array();
            $u = new User();
            $r = $db->Execute('select * from FileSets where fsType = ? and uID = ? order by fsName asc', array(FileSet::TYPE_SAVED_SEARCH, $u->getUserID()));
            while ($row = $r->FetchRow()) {
                $fs = new FileSet();
                $fs->Set($row);
                $sets[] = $fs;
            }
            return $sets;
        }

        public function getPermissionObjectIdentifier() {
            return $this->getFileSetID();
        }
        
        public function getMySets($u = false) {
            if ($u == false) {
                $u = new User();
            }
            $db = Loader::db();
            $sets = array();
            $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()));
            while ($row = $r->FetchRow()) {
                $fs = new FileSet();
                $fs->Set($row);
                $fsp = new Permissions($fs);
                if ($fsp->canSearchFiles()) {
                    $sets[] = $fs;
                }
            }
            return $sets;
        }
        
        public function updateFileSetDisplayOrder($files) {
            $db = Loader::db();
            $db->Execute('update FileSetFiles set fsDisplayOrder = 0 where fsID = ?', $this->getFileSetID());
            $i = 0;
            if (is_array($files)) { 
                foreach($files as $fID) {
                    $db->Execute('update FileSetFiles set fsDisplayOrder = ? where fsID = ? and fID = ?', array($i, $this->getFileSetID(), $fID));
                    $i++;
                }
            }
        }
        
        /**
         * Get a file set object by a file set's id
         * @param int $fsID
         * @return FileSet
         */
        public static function getByID($fsID) {
            $db = Loader::db();
            $row = $db->GetRow('select * from FileSets where fsID = ?', array($fsID));
            if (is_array($row)) {
                $fs = new FileSet();
                $fs->Set($row);
                if ($row['fsType'] == FileSet::TYPE_SAVED_SEARCH) {
                    $row2 = $db->GetRow('select fsSearchRequest, fsResultColumns from FileSetSavedSearches where fsID = ?', array($fsID));
                    $fs->fsSearchRequest = @unserialize($row2['fsSearchRequest']);
                    $fs->fsResultColumns = @unserialize($row2['fsResultColumns']);
                }
                return $fs;
            }
        }
        
        /**
         * Get a file set object by a file name
         * @param string $fsName
         * @return FileSet
         */
        public static function getByName($fsName) {
            $db = Loader::db();
            $row = $db->GetRow('select * from FileSets where fsName = ?', array($fsName));
            if (is_array($row) && count($row)) {
                $fs = new FileSet();
                $fs->Set($row);
                return $fs;
            }
        }           
        
        public function getFileSetID() {
            if ($this->fsID) {
                return $this->fsID;
            }
            return 0;
        }
        public function overrideGlobalPermissions() {return $this->fsOverrideGlobalPermissions;}
        
        public function getFileSetName() {return $this->fsName;}    
        
        /**
         * Creats a new fileset if set doesn't exists
         *
         * If we find a multiple groups with the same properties,
         * we return an array containing each group
         * @param string $fs_name
         * @param int $fs_type
         * @param int $fs_uid
         * @return Mixed 
         *
         * Dev Note: This will create duplicate sets with the same name if a set exists owned by another user!!! 
         */     
        public static function createAndGetSet($fs_name, $fs_type, $fs_uid=false) {
            if (!$fs_uid) {
                $u = new User();
                $fs_uid = $u->uID;
            }
            
            $file_set = new FileSet();
            $criteria = array($fs_name,$fs_type,$fs_uid);
            $matched_sets = $file_set->Find('fsName=? AND fsType=? and uID=?',$criteria);
            
            if (1 === count($matched_sets) ) {
                return $matched_sets[0];
            }
            else if (1 < count($matched_sets)) {
                return $matched_sets;
            }
            else{
                //AS: Adodb Active record is complaining a ?/value array mismatch unless
                //we explicatly set the primary key ID field to null                    
                $file_set->fsID     = null;
                $file_set->fsName   = $fs_name;
                $file_set->fsOverrideGlobalPermissions = 0;
                $file_set->fsType   = $fs_type;
                $file_set->uID      = $fs_uid;
                $file_set->save();

                $db = Loader::db();
                $fsID = $db->Insert_Id();
                $fs = FileSet::getByID($fsID);
                Events::fire('on_file_set_add', $fs);
                return $fs;
            }           
        }
        
        /**
        * Adds the file to the set
        * @param type $fID  //accepts an ID or a File object
        * @return object
        */      
        public function addFileToSet($f_id) {
            if (is_object($f_id)) {
                $f_id = $f_id->getFileID();
            }           
            $file_set_file = FileSetFile::createAndGetFile($f_id,$this->fsID);
            Events::fire('on_file_added_to_set', $f_id, $this->getFileSetID());
            return $file_set_file;
        }
        
        public function getSavedSearchRequest() {
            return $this->fsSearchRequest;
        }
        
        public function getSavedSearchColumns() {
            return $this->fsResultColumns;
        }
        public function removeFileFromSet($f_id){
            if (is_object($f_id)) {
                $f_id = $f_id->fID;
            }
            $db = Loader::db();
            $db->Execute('DELETE FROM FileSetFiles 
            WHERE fID = ? 
            AND   fsID = ?', array($f_id, $this->getFileSetID()));
            Events::fire('on_file_removed_from_set', $f_id, $this->getFileSetID());
        }

        /**
        * Get a list of files asociated with this set
        *
        * Can obsolete this when we get version of ADOdB with one/many support
        * @return type $var_name
        */      
        private function populateFiles(){           
            $utility            = new FileSetFile();
            $this->fileSetFiles = $utility->Find('fsID = ? ORDER BY fsDisplayOrder', array($this->fsID));
        }
        
        public function hasFileID($f_id){
            if (!is_array($this->fileSetFiles)) {
                $this->populateFiles();
            }           
            foreach ($this->fileSetFiles as $file) {
                if($file->fID == $f_id){
                    return true;
                }
            }
        }

        /**
         * Returns an array of File objects from the current set
         * @return array
         */
        public function getFiles() {
            if (!$this->fileSetFiles) { $this->populateFiles(); }
            $files = array();
            foreach ($this->fileSetFiles as $file) {
                $files[] = File::getByID($file->fID);
            }
            return $files;
        }

        /**
         * Static method to return an array of File objects by the set id
         * @param  int $fsID
         * @return array
         */
        public static function getFilesBySetID($fsID) {
            if (intval($fsID) > 0) {
                $fileset = self::getByID($fsID);
                if ($fileset instanceof FileSet) {
                    return $fileset->getFiles();
                }
            }
        }

        /**
         * Static method to return an array of File objects by the set name
         * @param  string $fsName
         * @return array
         */
        public static function getFilesBySetName($fsName) {
            if (!empty($fsName)) {
                $fileset = self::getByName($fsName);
                if ($fileset instanceof FileSet) {
                    return $fileset->getFiles();
                }
            }
        }
        
        public function delete() {
            parent::delete();
            $db = Loader::db();
            $db->Execute('delete from FileSetSavedSearches where fsID = ?', array($this->fsID));
        }
        
        public function resetPermissions() {
            $db = Loader::db();
            $db->Execute('delete from FileSetPermissionAssignments where fsID = ?', array($this->fsID));
        }
        
        public function acquireBaseFileSetPermissions() {
            $this->resetPermissions();

            $db = Loader::db();

            $q = "select fsID, paID, pkID from FileSetPermissionAssignments where fsID = 0";
            $r = $db->query($q);
            while($row = $r->fetchRow()) {
                $v = array($this->fsID, $row['paID'], $row['pkID']);
                $q = "insert into FileSetPermissionAssignments (fsID, paID, pkID) values (?, ?, ?)";
                $db->query($q, $v);
            }
    
        }
        
        public function assignPermissions($userOrGroup, $permissions = array(), $accessType = FileSetPermissionKey::ACCESS_TYPE_INCLUDE) {
            $db = Loader::db();
            if ($this->fsID > 0) { 
                $db->Execute("update FileSets set fsOverrideGlobalPermissions = 1 where fsID = ?", array($this->fsID));
                $this->fsOverrideGlobalPermissions = true;
            }
            
            if (is_array($userOrGroup)) { 
                $pe = GroupCombinationPermissionAccessEntity::getOrCreate($userOrGroup);
                // group combination
            } else if ($userOrGroup instanceof User || $userOrGroup instanceof UserInfo) { 
                $pe = UserPermissionAccessEntity::getOrCreate($userOrGroup);
            } else { 
                // group;
                $pe = GroupPermissionAccessEntity::getOrCreate($userOrGroup);
            }
            
            foreach($permissions as $pkHandle) { 
                $pk = PermissionKey::getByHandle($pkHandle);
                $pk->setPermissionObject($this);
                $pa = $pk->getPermissionAccessObject();
                if (!is_object($pa)) {
                    $pa = PermissionAccess::create($pk);
                } else if ($pa->isPermissionAccessInUse()) {
                    $pa = $pa->duplicate();
                }
                $pa->addListItem($pe, false, $accessType);
                $pt = $pk->getPermissionAssignmentObject();
                $pt->assignPermissionAccess($pa);
            }
        }


    }
    
    class Concrete5_Model_FileSetFile extends Model {
        public static function createAndGetFile($f_id, $fs_id){ 
            $file_set_file = new FileSetFile();
            $criteria = array($f_id,$fs_id);        
            
            $matched_sets = $file_set_file->Find('fID=? AND fsID=?',$criteria);
            
            if (1 === count($matched_sets) ) {
                return $matched_sets[0];
            }
            else if (1 < count($matched_sets)) {
                return $matched_sets;
            }
            else{
                //AS: Adodb Active record is complaining a ?/value array mismatch unless
                //we explicatly set the primary key ID field to null
                $db = Loader::db();
                $fsDisplayOrder = $db->GetOne('select count(fID) from FileSetFiles where fsID = ?', $fs_id);
                $file_set_file->fsfID = null;
                $file_set_file->fID =  $f_id;           
                $file_set_file->fsID = $fs_id;
                $file_set_file->timestamp = date('Y-m-d H:i:s');
                $file_set_file->fsDisplayOrder = $fsDisplayOrder;
                $file_set_file->Save();
                return $file_set_file;
            }           
        }
    }
    
    class Concrete5_Model_FileSetSavedSearch extends Concrete5_Model_FileSet {
        
        public static function add($name, $searchRequest, $searchColumnsObject) {
            $fs = parent::createAndGetSet($name, FileSet::TYPE_SAVED_SEARCH);
            $db = Loader::db();
            $v = array($fs->getFileSetID(), serialize($searchRequest), serialize($searchColumnsObject));
            $db->Execute('insert into FileSetSavedSearches (fsID, fsSearchRequest, fsResultColumns) values (?, ?, ?)', $v);
            return $fs;
        }
    
    }