Example #1
0
 /**
  * Get file list (retrieve and sort)
  *
  * @param      array	$params
  *
  * @return     array
  */
 public function filelist($params = array())
 {
     // Accept params
     $filter = isset($params['filter']) ? $params['filter'] : NULL;
     $dirPath = isset($params['subdir']) ? $params['subdir'] : NULL;
     $showUntracked = isset($params['showUntracked']) ? $params['showUntracked'] : false;
     $remotes = isset($params['remoteConnections']) ? $params['remoteConnections'] : array();
     $sortdir = isset($params['sortdir']) && $params['sortdir'] == 'DESC' ? SORT_DESC : SORT_ASC;
     $sortby = isset($params['sortby']) ? $params['sortby'] : 'name';
     $getParents = isset($params['getParents']) ? $params['getParents'] : false;
     $getChildren = isset($params['getChildren']) ? $params['getChildren'] : false;
     $files = isset($params['files']) && is_array($params['files']) ? $params['files'] : array();
     // Get a list of files Git repo
     $files = empty($files) ? $this->_git->getFiles($dirPath, false) : $files;
     // Add untracked?
     $untracked = $showUntracked ? $this->_git->getFiles($dirPath, true) : array();
     if (!empty($untracked)) {
         $files = array_merge($files, $untracked);
     }
     // Include remote connections?
     if (!empty($remotes)) {
         foreach ($remotes as $name => $item) {
             $files[] = $name;
         }
     }
     $remote = $this->get('remote');
     // Output containers
     $items = array();
     $sorting = array();
     $folders = array();
     if ($dirPath) {
         $folders[] = $dirPath;
     }
     // Go through items and get what we need
     foreach ($files as $item) {
         $item = urldecode($item);
         if (trim($item) == '') {
             continue;
         }
         // Load basic file metadata
         $file = new Models\File($item, $this->_path);
         // Search filter applied
         if ($filter && strpos(trim($file->get('localPath')), trim($filter)) === false && strpos(trim($file->get('localPath')), trim($filter)) === false) {
             continue;
         } elseif ($filter) {
             $getParents = false;
         }
         // Untracked?
         if (in_array($file->get('localPath'), $untracked)) {
             $file->set('untracked', true);
         }
         // Do we have a parent? Get folder information
         if ($file->get('dirname') && !in_array($file->get('dirname'), $folders)) {
             // Folder metadata
             $file->setFolder();
             // Add to list
             if (empty($items[$file->get('name')])) {
                 // Recursive check
                 if ($getChildren || !$getChildren && $file->getDirLevel($dirPath) > $file->getDirLevel($file->get('dirname'))) {
                     $folders[] = $file->get('localPath');
                     if (!$getParents) {
                         continue;
                     }
                     $items[$file->get('name')] = $file;
                     // Collect info for sorting
                     switch ($sortby) {
                         case 'size':
                             $sorting[] = $file->getSize();
                             break;
                         case 'modified':
                             // Need to get extra metadata (slower)
                             $sorting[] = '';
                             break;
                         case 'localpath':
                             $sorting[] = strtolower($file->get('localPath'));
                             break;
                         case 'name':
                         default:
                             $sorting[] = strtolower($file->get('name'));
                             break;
                     }
                 }
             }
         }
         // Getting parent information?
         if ($getParents) {
             $file->setParents();
         }
         // Do not recurse
         if (!$getChildren && $file->get('dirname')) {
             if (!$dirPath || $dirPath != $file->get('dirname')) {
                 continue;
             }
         }
         // Skip this
         if ($file->get('name') == '.gitignore') {
             continue;
         }
         // Check for remote connections
         $syncRecord = NULL;
         if (isset($remotes[$file->get('localPath')])) {
             // Pick up data from sync record
             $syncRecord = $remotes[$file->get('localPath')];
             $file->set('remote', $syncRecord->service);
             $file->set('author', $syncRecord->remote_author);
             $file->set('date', date('c', strtotime($syncRecord->remote_modified . ' UTC')));
             $file->set('mimeType', $syncRecord->remote_format);
         }
         // Add to list
         if (empty($items[$file->get('name')])) {
             $items[$file->get('name')] = $file;
             // Collect info for sorting
             switch ($sortby) {
                 case 'size':
                     $sorting[] = $file->getSize();
                     break;
                 case 'modified':
                     // Need to get extra metadata (slower)
                     $sorting[] = $this->_file($file, $dirPath, 'date');
                     break;
                 case 'localpath':
                     $sorting[] = strtolower($file->get('localPath'));
                     break;
                 case 'name':
                 default:
                     $sorting[] = strtolower($file->get('name'));
                     break;
             }
         }
     }
     // Sort
     array_multisort($sorting, $sortdir, $items);
     // Apply start and limit, get complete metadata and return
     return $this->_list($items, $params);
 }