1212
1313namespace OCA \Maps \Service ;
1414
15+ use OC \OCS \Exception ;
16+ use OCP \AppFramework \Http \DataResponse ;
17+ use OCP \Files \NotFoundException ;
1518use OCP \Files \Search \ISearchComparison ;
1619use OCP \IL10N ;
1720use OCP \ILogger ;
@@ -33,20 +36,48 @@ public function __construct (ILogger $logger, $userfolder, $userId) {
3336 $ this ->userId = $ userId ;
3437 }
3538
39+ public function addMyMap ($ newName ) {
40+ $ MapData = [
41+ 'name ' => $ newName ,
42+ ];
43+ if (!$ this ->userfolder ->nodeExists ('/Maps ' )) {
44+ $ this ->userfolder ->newFolder ('Maps ' );
45+ }
46+ if ($ this ->userfolder ->nodeExists ('/Maps ' )) {
47+ $ mapsFolder = $ this ->userfolder ->get ('/Maps ' );
48+ if ($ mapsFolder ->getType () !== \OCP \Files \FileInfo::TYPE_FOLDER ) {
49+ $ response = '/Maps is not a directory ' ;
50+ return $ response ;
51+ }
52+ else if (!$ mapsFolder ->isCreatable ()) {
53+ $ response = '/Maps is not writeable ' ;
54+ return $ response ;
55+ }
56+ }
57+ else {
58+ $ response = 'Impossible to create /Maps ' ;
59+ return $ response ;
60+ }
61+ $ mapFolder = $ mapsFolder ->newFolder ($ newName );
62+ $ MapData ['id ' ] = $ mapFolder ->getId ();
63+ $ mapFolder ->newFile (".maps " ,"{} " );
64+ return $ MapData ;
65+ }
66+
3667 public function getAllMyMaps (){
3768 $ MyMaps = [];
3869 $ MyMapsNodes = $ this ->userfolder ->search ('.maps ' );
3970 foreach ($ MyMapsNodes as $ node ) {
4071 if ($ node ->getType () === FileInfo::TYPE_FILE and $ node ->getName () === ".maps " ) {
41- $ MapData = json_decode ($ node ->getContent (), true );
42- if (isset ($ MapData ["name " ])){
43- $ name = $ MapData ["name " ];
72+ $ mapData = json_decode ($ node ->getContent (), true );
73+ if (isset ($ mapData ["name " ])){
74+ $ name = $ mapData ["name " ];
4475 } else {
4576 $ name = $ node ->getParent ()->getName ();
4677 }
4778 $ color = null ;
48- if (isset ($ MapData ["color " ])){
49- $ color = $ MapData ["color " ];
79+ if (isset ($ mapData ["color " ])){
80+ $ color = $ mapData ["color " ];
5081 }
5182 $ MyMap = [
5283 "id " =>$ node ->getParent ()->getId (),
@@ -59,4 +90,50 @@ public function getAllMyMaps(){
5990 }
6091 return $ MyMaps ;
6192 }
93+
94+ public function updateMyMap ($ id , $ values ) {
95+ $ folders = $ this ->userfolder ->getById ($ id );
96+ $ folder = array_shift ($ folders );
97+ try {
98+ $ file =$ folder ->get (".maps " );
99+ } catch (NotFoundException $ e ) {
100+ $ file =$ folder ->newFile (".maps " , $ content = '{} ' );
101+ }
102+ $ mapData = json_decode ($ file ->getContent (),true );
103+ foreach ($ values as $ key =>$ value ) {
104+ if ($ key === 'newName ' ) {
105+ $ key = 'name ' ;
106+ }
107+ if (is_null ($ value )) {
108+ unset($ mapData [$ key ]);
109+ } else {
110+ $ mapData [$ key ] = $ value ;
111+ }
112+ }
113+ $ file ->putContent (json_encode ($ mapData ,JSON_PRETTY_PRINT ));
114+ return $ mapData ;
115+ }
116+
117+ public function deleteMyMap ($ id ) {
118+ $ folders = $ this ->userfolder ->getById ($ id );
119+ $ folder = array_shift ($ folders );
120+ if ($ this ->userfolder ->nodeExists ('/Maps ' )) {
121+ $ mapsFolder = $ this ->userfolder ->get ('/Maps ' );
122+ if ($ folder ->getParent ()->getId () === $ mapsFolder ->getId () ) {
123+ try {
124+ $ folder ->delete ();
125+ } catch (Exception $ e ) {
126+ return 1 ;
127+ }
128+ }
129+ }
130+
131+ try {
132+ $ file =$ folder ->get (".maps " );
133+ $ file ->delete ();
134+ } catch (NotFoundException $ e ) {
135+ return 1 ;
136+ }
137+ return 0 ;
138+ }
62139}
0 commit comments