]> err.no Git - sope/blob - sope-appserver/NGObjWeb/DynamicElements/_WOSimpleActionHyperlink.m
started TAL element builder
[sope] / sope-appserver / NGObjWeb / DynamicElements / _WOSimpleActionHyperlink.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 "WOHyperlink.h"
23
24 /*
25   Class Hierachy
26     [WOHTMLDynamicElement]
27       [WOHyperlink]
28         _WOSimpleActionHyperlink
29           _WOSimpleStringActionHyperlink
30 */
31
32 @interface _WOSimpleActionHyperlink : WOHyperlink
33 {
34   WOAssociation *action;
35   WOElement     *template;
36 }
37 @end /* _WOSimpleActionHyperlink */
38
39 @interface _WOSimpleStringActionHyperlink : _WOSimpleActionHyperlink
40 {
41   WOAssociation *string;
42 }
43 @end /* _WOSimpleStringActionHyperlink */
44
45 #include "WOHyperlinkInfo.h"
46 #include "WOElement+private.h"
47 #include <NGObjWeb/WOAssociation.h>
48 #include "decommon.h"
49
50 @implementation _WOSimpleActionHyperlink
51
52 + (int)version {
53   return [super version] + 0 /* v4 */;
54 }
55 + (void)initialize {
56   NSAssert2([super version] == 4,
57             @"invalid superclass (%@) version %i !",
58             NSStringFromClass([self superclass]), [super version]);
59 }
60
61 - (id)initWithName:(NSString *)_name
62   hyperlinkInfo:(WOHyperlinkInfo *)_info
63   template:(WOElement *)_t
64 {
65   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
66     self->template     = RETAIN(_t);
67     self->containsForm = NO;
68     self->action       = _info->action;
69 #if DEBUG
70     NSAssert(self->action, @"missing action ?!");
71 #endif
72   }
73   return self;
74 }
75
76 - (void)dealloc {
77   [self->template release];
78   [self->action   release];
79   [super dealloc];
80 }
81
82 /* dynamic invocation */
83
84 - (id)invokeActionForRequest:(WORequest *)_request
85   inContext:(WOContext *)_ctx
86 {
87   if (![[_ctx elementID] isEqualToString:[_ctx senderID]]) {
88     /* link is not the active element */
89 #if DEBUG
90     NSLog(@"HYPERLINK is not active (%@ vs %@) !",
91           [_ctx elementID], [_ctx senderID]);
92 #endif
93     return [self->template invokeActionForRequest:_request inContext:_ctx];
94   }
95   
96   //NSLog(@"%s: invoke called ...", __PRETTY_FUNCTION__);
97   
98   /* link is active */
99   return [self executeAction:self->action inContext:_ctx];
100 }
101
102 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
103   if ([[_ctx request] isFromClientComponent])
104     return;
105   
106   WOResponse_AddCString(_response, "<a href=\"");
107   WOResponse_AddString(_response, [_ctx componentActionURL]);
108   
109   [_response appendContentCharacter:'"'];
110   
111   [self appendExtraAttributesToResponse:_response inContext:_ctx];
112   if (self->otherTagString) {
113     WOResponse_AddChar(_response, ' ');
114     WOResponse_AddString(_response,
115                          [self->otherTagString stringValueInComponent:
116                               [_ctx component]]);
117   }
118   [_response appendContentCharacter:'>'];
119   
120   /* content */
121   [self->template appendToResponse:_response inContext:_ctx];
122   
123   /* closing tag */
124   WOResponse_AddCString(_response, "</a>");
125 }
126
127 /* description */
128
129 - (NSString *)associationDescription {
130   return [NSString stringWithFormat:@"action=%@", self->action];
131 }
132
133 @end /* _WOSimpleActionHyperlink */
134
135 @implementation _WOSimpleStringActionHyperlink
136
137 - (id)initWithName:(NSString *)_name
138   hyperlinkInfo:(WOHyperlinkInfo *)_info
139   template:(WOElement *)_t
140 {
141   if ((self = [super initWithName:_name hyperlinkInfo:_info template:_t])) {
142     self->string = _info->string;
143   }
144   return self;
145 }
146
147 - (void)dealloc {
148   [self->string release];
149   [super dealloc];
150 }
151
152 /* HTML generation */
153
154 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
155   WOComponent *sComponent;
156   NSString *content;
157
158   if ([[_ctx request] isFromClientComponent])
159     return;
160   
161   sComponent = [_ctx component];
162   content = [self->string stringValueInComponent:sComponent];
163     
164   WOResponse_AddCString(_response, "<a href=\"");
165   WOResponse_AddString(_response, [_ctx componentActionURL]);
166     
167   [_response appendContentCharacter:'"'];
168     
169   [self appendExtraAttributesToResponse:_response inContext:_ctx];
170   
171   if (self->otherTagString) {
172     WOResponse_AddChar(_response, ' ');
173     WOResponse_AddString(_response,
174                          [self->otherTagString stringValueInComponent:
175                               sComponent]);
176   }
177   [_response appendContentCharacter:'>'];
178     
179   /* content */
180   [self->template appendToResponse:_response inContext:_ctx];
181   if (content) [_response appendContentHTMLString:content];
182
183   /* closing tag */
184   WOResponse_AddCString(_response, "</a>");
185 }
186
187 /* description */
188
189 - (NSString *)associationDescription {
190   return [NSString stringWithFormat:@"action=%@, string=%@",
191                      self->action, self->string];
192 }
193
194 @end /* _WOSimpleStringActionHyperlink */