Skip to content

Commit

Permalink
also show notes inside subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
Henni committed Oct 25, 2016
1 parent 2020a46 commit 0ed48c5
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions service/notesservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OCA\Notes\Service;

use OCP\Files\FileInfo;
use OCP\IL10N;
use OCP\Files\IRootFolder;
use OCP\Files\Folder;
Expand Down Expand Up @@ -42,13 +43,10 @@ public function __construct (IRootFolder $root, IL10N $l10n) {
* @return array with all notes in the current directory
*/
public function getAll ($userId){
$folder = $this->getFolderForUser($userId);
$files = $folder->getDirectoryListing();
$notes = $this->gatherNoteFiles($this->getFolderForUser($userId));
$filesById = [];
foreach($files as $file) {
if($this->isNote($file)) {
$filesById[$file->getId()] = $file;
}
foreach($notes as $note) {
$filesById[$note->getId()] = $note;
}
$tagger = \OC::$server->getTagManager()->load('files');
if($tagger==null) {
Expand Down Expand Up @@ -118,8 +116,9 @@ public function create ($userId) {
* @return \OCA\Notes\Db\Note the updated note
*/
public function update ($id, $content, $userId){
$folder = $this->getFolderForUser($userId);
$file = $this->getFileById($folder, $id);
$notesFolder = $this->getFolderForUser($userId);
$file = $this->getFileById($notesFolder, $id);
$folder = $file->getParent();

// generate content from the first line of the title
$splitContent = explode("\n", $content);
Expand All @@ -139,9 +138,10 @@ public function update ($id, $content, $userId){

// generate filename if there were collisions
$currentFilePath = $file->getPath();
$basePath = '/' . $userId . '/files/Notes/';
$basePath = pathinfo($file->getPath(), PATHINFO_DIRNAME);
\OCP\Util::writeLog('notes', $basePath, \OCP\Util::ERROR);
$fileExtension = pathinfo($file->getName(), PATHINFO_EXTENSION);
$newFilePath = $basePath . $this->generateFileName($folder, $title, $fileExtension, $id);
$newFilePath = $basePath . '/' . $this->generateFileName($folder, $title, $fileExtension, $id);

// if the current path is not the new path, the file has to be renamed
if($currentFilePath !== $newFilePath) {
Expand Down Expand Up @@ -257,6 +257,29 @@ private function generateFileName (Folder $folder, $title, $extension, $id) {
}
}


/**
* gather note files in given directory and all subdirectories
* @param Folder $folder
* @return array
*/
private function gatherNoteFiles ($folder) {
$notes = [];
$nodes = $folder->getDirectoryListing();
foreach($nodes as $node) {
\OCP\Util::writeLog('notes', $node->getType(), \OCP\Util::ERROR);
if($node->getType() === FileInfo::TYPE_FOLDER) {
$notes = array_merge($notes, $this->gatherNoteFiles($node));
continue;
}
if($this->isNote($node)) {
$notes[] = $node;
}
}
return $notes;
}


/**
* test if file is a note
*
Expand Down

0 comments on commit 0ed48c5

Please sign in to comment.