forked from jefflinwood/Drupal-Plugin-for-PhoneGap-for-iOS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DrupalPlugin.m
153 lines (113 loc) · 6.64 KB
/
DrupalPlugin.m
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
//
// DrupalPlugin.m
//
// Copyright 2011 Jeff Linwood.
// MIT License
#import "DrupalPlugin.h"
#import "DIOSNode.h"
#import "DIOSViews.h"
#import "DIOSFile.h"
@implementation DrupalPlugin
@synthesize currentUser;
@synthesize currentSession;
#pragma mark -
#pragma mark Private Methods
#pragma mark -
#pragma mark Public Methods
- (void) openAnonymousSession:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
self.currentSession = [[[DIOSConnect alloc] init] autorelease];
self.currentUser = nil;
if ([self.currentSession connResult]) {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self.currentSession connResult]];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[self.currentSession connResult]];
[super writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
}
- (void) login:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSLog(@"Logging in username: %@", [options objectForKey:@"username"]);
NSString* callbackId = [arguments objectAtIndex:0];
self.currentSession = [[[DIOSConnect alloc] init] autorelease];
DIOSUser *user = [[DIOSUser alloc] initWithSession:self.currentSession];
[user loginWithUsername:[options objectForKey:@"username"] andPassword:[options objectForKey:@"password"]];
//if the login was successful (we can check for a uid on the connection result user), set the current user on the Drupal plugin
if ([[[user connResult] objectForKey:@"user"] objectForKey:@"uid"]) {
self.currentUser = user;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[user connResult]];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[user connResult]];
[super writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
[user release];
}
- (void) logout:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
self.currentSession = [[[DIOSConnect alloc] init] autorelease];
DIOSUser *user = [[DIOSUser alloc] initWithSession:self.currentSession];
[user logout];
if ([user connResult]) {
self.currentUser = user;
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[user connResult]];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
} else {
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[user connResult]];
[super writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
}
[user release];
}
- (void) nodeGet:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
DIOSNode *diosNode = [[DIOSNode alloc] initWithSession:self.currentSession];
NSDictionary *node = [diosNode nodeGet:[options objectForKey:@"nid"]];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:node];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
[node release];
}
- (void) nodeSave:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
DIOSNode *diosNode = [[DIOSNode alloc] initWithSession:self.currentSession];
NSMutableDictionary *node = (NSMutableDictionary*) [options objectForKey:@"node"];
NSDictionary *result = [diosNode nodeSave:node];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
[result release];
}
- (void) nodeDelete:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
DIOSNode *diosNode = [[DIOSNode alloc] initWithSession:self.currentSession];
NSDictionary *result = [diosNode nodeDelete:[options objectForKey:@"nid"]];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
[result release];
}
- (void) nodeGetIndex:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
DIOSNode *diosNode = [[DIOSNode alloc] initWithSession:self.currentSession];
NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[diosNode nodeGetIndex],@"nodes", nil];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
[dict release];
}
- (void) viewGet:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
DIOSViews *diosViews = [[DIOSViews alloc] initWithSession:self.currentSession];
NSArray* view = (NSArray*) [diosViews viewsGet:[options objectForKey:@"viewName"]];
NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:view,@"view", nil];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dict];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
[dict release];
[view release];
}
- (void) fileSave:(NSMutableArray *)arguments withDict:(NSMutableDictionary *)options {
NSString* callbackId = [arguments objectAtIndex:0];
DIOSFile *diosFile = [[DIOSFile alloc] initWithSession:self.currentSession];
NSMutableDictionary *file = (NSMutableDictionary*) [options objectForKey:@"file"];
NSDictionary *result = [diosFile fileSave:file];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
[super writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
[result release];
}
@end