]> err.no Git - sope/blob - sope-appserver/WEExtensions/WEPageLink.m
added strict OSX bundle dependencies
[sope] / sope-appserver / WEExtensions / WEPageLink.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE 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   SOPE 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 SOPE; 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
22 #include <NGObjWeb/WODynamicElement.h>
23
24 /*
25   WEPageLink
26   
27   This element is pretty similiar to WOHyperlink with the "pageName" binding,
28   but in addition allows for parameters being passed to the page.
29
30   WEPageLink associations:
31     
32     pageName
33     sync     (either array of names or a string with names seperated by ',')
34 */
35
36 @class WOAssociation, WOElement;
37 @class NSDictionary;
38
39 @interface WEPageLink : WODynamicElement
40 {
41   // WODynamicElement: otherTagString
42 @protected
43   WOAssociation *pageName;
44   WOAssociation *syncBindings;
45   WOElement     *template;
46   unsigned      keyCount;
47   NSArray       *keys;
48   NSArray       *assocs;
49 }
50
51 @end
52
53 #include "common.h"
54
55 @implementation WEPageLink
56
57 + (int)version {
58   return [super version] + 0 /* v2 */;
59 }
60 + (void)initialize {
61   NSAssert2([super version] == 2,
62             @"invalid superclass (%@) version %i !",
63             NSStringFromClass([self superclass]), [super version]);
64 }
65
66 - (id)initWithName:(NSString *)_name
67   associations:(NSDictionary *)_config
68   template:(WOElement *)_t
69 {
70   if ((self = [super initWithName:_name associations:_config template:_t])) {
71     NSMutableArray *mkeys, *massocs;
72     NSEnumerator *e;
73     NSString *k;
74     
75     self->pageName     = WOExtGetProperty(_config, @"pageName");
76     self->syncBindings = WOExtGetProperty(_config, @"sync");
77     self->template = [_t retain];
78     
79     self->keyCount = [_config count];
80     mkeys   = [[NSMutableArray alloc] initWithCapacity:self->keyCount];
81     massocs = [[NSMutableArray alloc] initWithCapacity:self->keyCount];
82     
83     e = [_config keyEnumerator];
84     while ((k = [e nextObject])) {
85       [mkeys addObject:k];
86       [massocs addObject:[_config objectForKey:k]];
87     }
88     self->keys   = [mkeys   copy]; [mkeys   release];
89     self->assocs = [massocs copy]; [massocs release];
90     
91     [(NSMutableDictionary *)_config removeAllObjects];
92   }
93   return self;
94 }
95
96 - (void)dealloc {
97   [self->syncBindings release];
98   [self->template release];
99   [self->pageName release];
100   [self->keys     release];
101   [self->assocs   release];
102   [super dealloc];
103 }
104
105 /* handling requests */
106
107 - (id)invokeActionForRequest:(WORequest *)_rq inContext:(WOContext *)_ctx {
108   WOComponent *page, *sComponent;
109   NSString    *name;
110   unsigned    i;
111   id sync;
112   
113   if (![[_ctx elementID] isEqualToString:[_ctx senderID]])
114     /* link is not the active element */
115     return [self->template invokeActionForRequest:_rq inContext:_ctx];
116
117   /* link is the active element */
118   
119   sComponent = [_ctx component];
120   name = [self->pageName stringValueInComponent:sComponent];
121   page = [[_ctx application] pageWithName:name inContext:_ctx];
122   
123   if (page == nil) {
124     [self logWithFormat:@"did not find page with name '%@'.", name];
125     return nil;
126   }
127   
128   /* apply sync bindings */
129   
130   if ((sync = [self->syncBindings valueInComponent:sComponent])) {
131     unsigned count;
132     
133     if (![sync isKindOfClass:[NSArray class]])
134       sync = [[sync stringValue] componentsSeparatedByString:@","];
135     
136     // Note: we cannot use keypathes in a useful way here because we reuse
137     //       the key for assignment
138     for (i = 0, count = [sync count]; i < count; i++) {
139       NSString *key;
140       id value;
141       
142       key   = [sync objectAtIndex:i];
143       value = [sComponent valueForKey:key];
144       [page takeValue:value forKey:key];
145     }
146   }
147   
148   /* apply bindings */
149   
150   for (i = 0; i < self->keyCount; i++) {
151     id value;
152     
153     value = [[self->assocs objectAtIndex:i] valueInComponent:sComponent];
154     [page takeValue:value forKey:[self->keys objectAtIndex:i]];
155   }
156   
157   return page;
158 }
159
160 /* response generation */
161
162 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
163   NSString *href;
164   
165   href = [_ctx componentActionURL];
166   [_response appendContentCString:(unsigned char *)"<a href=\""];
167   [_response appendContentString:href];
168   [_response appendContentCharacter:'"'];
169   
170   if (self->otherTagString) {
171     [_response appendContentCharacter:' '];
172     [_response appendContentString:
173       [self->otherTagString stringValueInComponent:[_ctx component]]];
174   }
175   [_response appendContentCharacter:'>'];
176   
177   [self->template appendToResponse:_response inContext:_ctx];
178   [_response appendContentCString:(unsigned char *)"</a>"];
179 }
180
181 @end /* WEPageLink */