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@interface WETableMatrixContentImp : WODynamicElement
25{
26  WOAssociation *elementName;
27  WOAssociation *rowspan;     /* a write variable */
28  WOAssociation *colspan;     /* a write variable */
29
30  WOElement     *template;
31}
32@end
33
34@interface WETableMatrixContent : WETableMatrixContentImp
35@end
36
37@interface WETableMatrixNoContent : WETableMatrixContentImp
38@end
39
40#include <NGObjWeb/NGObjWeb.h>
41#include "common.h"
42
43@implementation WETableMatrixContentImp
44
45- (id)initWithName:(NSString *)_name
46  associations:(NSDictionary *)_config
47  template:(WOElement *)_subs
48{
49  if ((self = [super initWithName:_name associations:_config template:_subs])) {
50    self->elementName = WOExtGetProperty(_config, @"elementName");
51    self->rowspan     = WOExtGetProperty(_config, @"rowspan");
52    self->colspan     = WOExtGetProperty(_config, @"colspan");
53
54    self->template = [_subs retain];
55  }
56  return self;
57}
58
59- (void)dealloc {
60  RELEASE(self->rowspan);
61  RELEASE(self->colspan);
62  RELEASE(self->elementName);
63  RELEASE(self->template);
64  [super dealloc];
65}
66
67/* accessors */
68
69- (NSString *)modeKey {
70  [self logWithFormat:@"ERROR: subclasses should override -modeKey!"];
71  return nil;
72}
73
74/* response generation */
75
76- (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
77  id       tmp;
78  NSString *tag;
79  int      cspan, rspan;
80
81  if ([_ctx isRenderingDisabled]) {
82    [self->template appendToResponse:_response inContext:_ctx];
83    return;
84  }
85
86  if ((tmp = [_ctx valueForKey:@"WETableMatrix_Query"])) {
87    [tmp addObject:[self modeKey]];
88    return;
89  }
90
91  if ((tmp = [_ctx valueForKey:@"WETableMatrix_Mode"]) == nil)
92    return;
93
94  if (![tmp isEqualToString:[self modeKey]])
95    return;
96
97  cspan = [[_ctx objectForKey:@"WETableMatrix_ColSpan"] intValue];
98  if (cspan < 1) cspan = 1;
99  rspan = [[_ctx objectForKey:@"WETableMatrix_RowSpan"] intValue];
100  if (rspan < 1) rspan = 1;
101
102  if ([self->colspan isValueSettable]) {
103    [self->colspan setValue:[NSNumber numberWithInt:cspan]
104                   inComponent:[_ctx component]];
105  }
106  if ([self->rowspan isValueSettable]) {
107    [self->rowspan setValue:[NSNumber numberWithInt:rspan]
108                   inComponent:[_ctx component]];
109  }
110
111  tag = [self->elementName stringValueInComponent:[_ctx component]];
112
113  if (tag) {
114    NSString *s;
115    char buf[64];
116
117    [_response appendContentString:@"<"];
118    [_response appendContentString:tag];
119
120    if (cspan > 1) {
121      sprintf(buf, "%i", cspan);
122      s = [[NSString alloc] initWithCString:buf];
123      [_response appendContentString:@" colspan=\""];
124      [_response appendContentString:s];
125      [_response appendContentString:@"\""];
126      [s release];
127    }
128    if (rspan > 1) {
129      sprintf(buf, "%i", rspan);
130      s = [[NSString alloc] initWithCString:buf];
131      [_response appendContentString:@" rowspan=\""];
132      [_response appendContentString:s];
133      [_response appendContentString:@"\""];
134      [s release];
135    }
136
137    [self appendExtraAttributesToResponse:_response inContext:_ctx];
138
139    [_response appendContentString:@">"];
140  }
141
142  [self->template appendToResponse:_response inContext:_ctx];
143
144  if (tag) {
145    [_response appendContentString:@"</"];
146    [_response appendContentString:tag];
147    [_response appendContentString:@">"];
148  }
149}
150
151@end /* WETableMatrixContentImp */
152
153@implementation WETableMatrixContent
154
155- (NSString *)modeKey {
156  return @"content";
157}
158
159@end /* WETableMatrixContent */
160
161@implementation WETableMatrixNoContent
162
163- (NSString *)modeKey {
164  return @"empty";
165}
166
167@end /* WETableMatrixNoContent */
168