]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEPageLink.m
renamed packages as discussed in the developer list
[sope] / sope-appserver / WEExtensions / WEPageLink.m
1 /*
2   Copyright (C) 2000-2004 SKYRIX Software AG
3
4   This file is part of OpenGroupware.org.
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include <NGObjWeb/WODynamicElement.h>
24
25 /*
26   WEPageLink
27   
28   This element is pretty similiar to WOHyperlink with the "pageName" binding,
29   but in addition allows for parameters being passed to the page.
30
31   WEPageLink associations:
32     
33     pageName
34     sync     (either array of names or a string with names seperated by ',')
35 */
36
37 @class WOAssociation, WOElement;
38 @class NSDictionary;
39
40 @interface WEPageLink : WODynamicElement
41 {
42   // WODynamicElement: otherTagString
43 @protected
44   WOAssociation *pageName;
45   WOAssociation *syncBindings;
46   WOElement     *template;
47   unsigned      keyCount;
48   NSArray       *keys;
49   NSArray       *assocs;
50 }
51
52 @end
53
54 #include "common.h"
55
56 @implementation WEPageLink
57
58 + (int)version {
59   return [super version] + 0 /* v2 */;
60 }
61 + (void)initialize {
62   NSAssert2([super version] == 2,
63             @"invalid superclass (%@) version %i !",
64             NSStringFromClass([self superclass]), [super version]);
65 }
66
67 - (id)initWithName:(NSString *)_name
68   associations:(NSDictionary *)_config
69   template:(WOElement *)_t
70 {
71   if ((self = [super initWithName:_name associations:_config template:_t])) {
72     NSMutableArray *mkeys, *massocs;
73     NSEnumerator *e;
74     NSString *k;
75     
76     self->pageName     = WOExtGetProperty(_config, @"pageName");
77     self->syncBindings = WOExtGetProperty(_config, @"sync");
78     self->template = [_t retain];
79     
80     self->keyCount = [_config count];
81     mkeys   = [[NSMutableArray alloc] initWithCapacity:self->keyCount];
82     massocs = [[NSMutableArray alloc] initWithCapacity:self->keyCount];
83     
84     e = [_config keyEnumerator];
85     while ((k = [e nextObject])) {
86       [mkeys addObject:k];
87       [massocs addObject:[_config objectForKey:k]];
88     }
89     self->keys   = [mkeys   copy]; [mkeys   release];
90     self->assocs = [massocs copy]; [massocs release];
91     
92     [(NSMutableDictionary *)_config removeAllObjects];
93   }
94   return self;
95 }
96
97 - (void)dealloc {
98   [self->syncBindings release];
99   [self->template release];
100   [self->pageName release];
101   [self->keys     release];
102   [self->assocs   release];
103   [super dealloc];
104 }
105
106 /* handling requests */
107
108 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
109   WOComponent *page, *sComponent;
110   NSString    *name;
111   unsigned    i;
112   id sync;
113   
114   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
115     /* link is not the active element */
116     return [self->template invokeActionForRequest:_rq inContext:_ctx];
117
118   /* link is the active element */
119   
120   sComponent = [_ctx component];
121   name = [self->pageName stringValueInComponent:sComponent];
122   page = [[_ctx application] pageWithName:name inContext:_ctx];
123   
124   if (page == nil) {
125     [self logWithFormat:@"did not find page with name '%@'.", name];
126     return nil;
127   }
128   
129   /* apply sync bindings */
130   
131   if ((sync = [self->syncBindings valueInComponent:sComponent])) {
132     unsigned count;
133     
134     if (![sync isKindOfClass:[NSArray class]])
135       sync = [[sync stringValue] componentsSeparatedByString:@","];
136     
137     // Note: we cannot use keypathes in a useful way here because we reuse
138     //       the key for assignment
139     for (i = 0, count = [sync count]; i < count; i++) {
140       NSString *key;
141       id value;
142       
143       key   = [sync objectAtIndex:i];
144       value = [sComponent valueForKey:key];
145       [page takeValue:value forKey:key];
146     }
147   }
148   
149   /* apply bindings */
150   
151   for (i = 0; i < self->keyCount; i++) {
152     id value;
153     
154     value = [[self->assocs objectAtIndex:i] valueInComponent:sComponent];
155     [page takeValue:value forKey:[self->keys objectAtIndex:i]];
156   }
157   
158   return page;
159 }
160
161 /* response generation */
162
163 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
164   NSString *href;
165   
166   href = [_ctx componentActionURL];
167   [_response appendContentCString:"<a href=\""];
168   [_response appendContentString:href];
169   [_response appendContentCharacter:'"'];
170   
171   if (self->otherTagString) {
172     NSString *s;
173     
174     s = [self->otherTagString stringValueInComponent:[_ctx component]];
175     [_response appendContentString:s];
176   }
177   [_response appendContentCharacter:'>'];
178   
179   [self->template appendToResponse:_response inContext:_ctx];
180   [_response appendContentCString:"</a>"];
181 }
182
183 @end /* WEPageLink */