Subversion Repositories regexpline_backup

[/] [stable/] [1.0/] [datatypes/] [hmregexpline/] [hmregexplinetype.php] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 xrow
<?php
2
 
3
/*
4
    Regular Expression datatype for eZ publish 3.x
5
    Copyright (C) 2005  Hans Melis
6
 
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 2 of the License, or
10
    (at your option) any later version.
11
 
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16
*/
17
 
18
/*!
19
  \class   hmregexplinetype hmregexplinetype.php
20
  \ingroup eZDatatype
21
  \brief   Handles the datatype regexpline
22
  \version 1.0
23
  \date    Thursday 17 March 2005 2:22:55 pm
24
  \author  Hans Melis
25
 
26
  By using regexpline you can ... do stuff :)
27
 
28
*/
29
 
30
include_once( 'kernel/classes/ezdatatype.php' );
31
 
32
include_once( 'kernel/common/i18n.php' );
33
 
34
define( 'EZ_DATATYPESTRING_REGEXPLINE', 'hmregexpline' );
35
 
36
class hmregexplinetype extends eZDataType
37
{
38
    /*!
39
      Constructor
40
    */
41
    function hmregexplinetype()
42
    {
43
        $this->eZDataType( EZ_DATATYPESTRING_REGEXPLINE, ezi18n( 'extension/regexpline/datatype', 'Regular Expression Text', 'Datatype name' ),
44
                           array( 'serialize_supported' => true ) );
45
    }
46
 
47
    /*!
48
    Validates all variables given on content class level
49
     \return EZ_INPUT_VALIDATOR_STATE_ACCEPTED or EZ_INPUT_VALIDATOR_STATE_INVALID if
50
             the values are accepted or not
51
    */
52
    function validateClassAttributeHTTPInput( &$http, $base, &$classAttribute )
53
    {
54
        $regexpName = $base . "_hmregexpline_regexp_" . $classAttribute->attribute( 'id' );
55
        $presetName = $base . "_hmregexpline_preset_" . $classAttribute->attribute( 'id' );
56
 
57
        $regexp = $preset = '';
58
 
59
        if( $http->hasPostVariable( $regexpName ) )
60
        {
61
            $regexp = $http->postVariable( $regexpName );
62
        }
63
 
64
        if( $http->hasPostVariable( $presetName ) )
65
        {
66
            $preset = $http->postVariable( $presetName );
67
        }
68
 
69
        if( !empty( $preset ) )
70
        {
71
            $ini =& eZINI::instance( 'regexpline.ini' );
72
            $presets = $ini->variable( 'GeneralSettings', 'RegularExpressions' );
73
 
74
            if( isset( $presets[$preset] ) )
75
            {
76
            $regexp = $presets[$preset];
77
            }
78
        }        
79
 
80
        $check = @preg_match( $regexp, 'Dummy string' );
81
 
82
        if( $check === false )
83
        {
84
            return EZ_INPUT_VALIDATOR_STATE_INVALID;
85
        }
86
 
87
        return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
88
    }
89
 
90
    /*!
91
     Fetches all variables inputed on content class level
92
     \return true if fetching of class attributes are successfull, false if not
93
    */
94
    function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
95
    {
96
        $regexpName = $base . "_hmregexpline_regexp_" . $classAttribute->attribute( 'id' );
97
        $helpName = $base . "_hmregexpline_helptext_" . $classAttribute->attribute( 'id' );
98
        $patternName = $base . "_hmregexpline_patternselect_" . $classAttribute->attribute( 'id' );
99
        $presetName = $base . "_hmregexpline_preset_" . $classAttribute->attribute( 'id' );
100
 
101
        $content = $classAttribute->content();
102
 
103
        if( $http->hasPostVariable( $regexpName ) )
104
        {
105
            $content['regexp'] = $http->postVariable( $regexpName );
106
        }
107
 
108
        if( $http->hasPostVariable( $presetName ) )
109
        {
110
            $content['preset'] = $http->postVariable( $presetName );
111
        }
112
 
113
        if( $http->hasPostVariable( $helpName ) )
114
        {
115
            $content['help_text'] = $http->postVariable( $helpName );
116
        }
117
 
118
        if( $http->hasPostVariable( $patternName ) )
119
        {
120
            $content['pattern_selection'] = $http->postVariable( $patternName );
121
        }
122
        else if( $http->hasPostVariable( 'ContentClassHasInput' ) )
123
        {
124
            $content['pattern_selection'] = array();
125
        }
126
 
127
        $regexp = $content['regexp'];
128
 
129
        if( !empty( $content['preset'] ) )
130
        {
131
            $ini =& eZINI::instance( 'regexpline.ini' );
132
            $presets = $ini->variable( 'GeneralSettings', 'RegularExpressions' );
133
 
134
            if( isset( $presets[$content['preset']] ) )
135
            {
136
                $regexp = $presets[$content['preset']];
137
 
138
                // Clear the regular expression in the content
139
                $content['regexp'] = '';
140
            }
141
        }
142
 
143
        $subPatternCount = @preg_match_all( "/\((?!\?\:)/", $regexp, $matches );
144
 
145
        $content['subpattern_count'] = $subPatternCount == false ? 0 : $subPatternCount;
146
 
147
        $classAttribute->setContent( $content );
148
        $classAttribute->store();
149
 
150
        return true;
151
    }
152
 
153
    function storeClassAttribute( &$classAttribute, $version )
154
    {
155
        $content = $classAttribute->content();
156
 
157
        $classAttribute->setAttribute( 'data_text5', serialize( $content ) );
158
    }
159
 
160
    function &classAttributeContent( &$classAttribute )
161
    {
162
        $content = unserialize( $classAttribute->attribute( 'data_text5' ) );
163
 
164
        if( !is_array( $content ) )
165
        {
166
            $content = array( 'regexp' => '',
167
                              'preset' => '',
168
                              'help_text' => '',
169
                              'subpattern_count' => 0,
170
                              'pattern_selection' => array() );
171
        }
172
 
173
        return $content;
174
    }
175
 
176
    /*!
177
     Validates input on content object level
178
     \return EZ_INPUT_VALIDATOR_STATE_ACCEPTED or EZ_INPUT_VALIDATOR_STATE_INVALID if
179
             the values are accepted or not
180
    */
181
    function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
182
    {
183
        $status = $this->validateAttributeHTTPInput( $http, $base, $contentObjectAttribute, false );
184
 
185
        return $status;
186
    }
187
 
188
    /*!
189
     Fetches all variables from the object
190
     \return true if fetching of class attributes are successfull, false if not
191
    */
192
    function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
193
    {
194
        $textName = $base . "_hmregexpline_data_text_" . $contentObjectAttribute->attribute( 'id' );
195
 
196
        if( $http->hasPostVariable( $textName ) )
197
        {
198
            $text = $http->postVariable( $textName );
199
            $contentObjectAttribute->setContent( $text );
200
            $contentObjectAttribute->storeData();
201
            return true;
202
        }
203
        return false;
204
    }
205
 
206
    function storeObjectAttribute( &$contentObjectAttribute )
207
    {
208
        $text = $contentObjectAttribute->content();
209
 
210
        $contentObjectAttribute->setAttribute( 'data_text', $text );
211
    }
212
 
213
    /*!
214
     Returns the content.
215
    */
216
    function &objectAttributeContent( &$contentObjectAttribute )
217
    {
218
        $text = $contentObjectAttribute->attribute( 'data_text' );
219
 
220
        return $text;
221
    }
222
 
223
    function validateAttributeHTTPInput( &$http, $base, &$objectAttribute, $isInformationCollector = false )
224
    {
225
        $textName = $base . "_hmregexpline_data_text_" . $objectAttribute->attribute( 'id' );
226
        $classAttribute =& $objectAttribute->contentClassAttribute();
227
 
228
        $required = false;
229
        $must_validate = ( $isInformationCollector == $classAttribute->attribute( 'is_information_collector' ) );
230
 
231
        if( method_exists( $objectAttribute, 'validateIsRequired' ) )
232
        {
233
            $required = $objectAttribute->validateIsRequired();
234
        }
235
        else
236
        {
237
            $required = ( $classAttribute->attribute( 'is_required' ) == 1 );
238
        }
239
 
240
        if( $http->hasPostVariable( $textName ) )
241
        {
242
            $text = $http->postVariable( $textName );
243
            $classContent = $classAttribute->content();
244
 
245
            if( empty( $text ) and ( $required === true && $must_validate === true ) )
246
            {
247
                $objectAttribute->setValidationError( ezi18n( 'extension/regexpline/datatype', 'This is a required field which means you can\'t leave it empty' ) );
248
                return EZ_INPUT_VALIDATOR_STATE_INVALID;
249
            }
250
 
251
        if( !empty( $text ) and @preg_match( $this->getRegularExpression( $classContent ), $text ) === 0 )
252
            {
253
                // No match
254
                $objectAttribute->setValidationError( ezi18n( 'extension/regexpline/datatype', 'Your input did not meet the requirements.' ) );
255
                return EZ_INPUT_VALIDATOR_STATE_INVALID;
256
            }
257
        }
258
        else
259
        {
260
            if( $required === true && $must_validate === true )
261
            {
262
                $objectAttribute->setValidationError( ezi18n( 'extension/regexpline/datatype', 'This is a required field which means you can\'t leave it empty' ) );
263
                return EZ_INPUT_VALIDATOR_STATE_INVALID;
264
            }
265
        }
266
 
267
        return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
268
    }
269
 
270
    function validateCollectionAttributeHTTPInput( &$http, $base, &$objectAttribute )
271
    {
272
        $status = $this->validateAttributeHTTPInput( $http, $base, $objectAttribute, true );
273
 
274
        return $status;
275
    }
276
 
277
    function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
278
    {
279
        $textName = $base . "_hmregexpline_data_text_" . $contentObjectAttribute->attribute( 'id' );
280
 
281
        if( $http->hasPostVariable( $textName ) )
282
        {
283
            $text = $http->postVariable( $textName );
284
            $collectionAttribute->setAttribute( 'data_text', $text );
285
            return true;
286
        }
287
        return false;
288
    }
289
 
290
    /*!
291
     Returns the meta data used for storing search indeces.
292
    */
293
    function metaData( $contentObjectAttribute )
294
    {
295
        return $contentObjectAttribute->attribute( 'data_text' );
296
    }
297
 
298
    /*!
299
     Returns the value as it will be shown if this attribute is used in the object name pattern.
300
    */
301
    function title( &$contentObjectAttribute )
302
    {
303
        $classAttribute =& $contentObjectAttribute->contentClassAttribute();
304
        $classContent = $classAttribute->content();
305
        $content = $contentObjectAttribute->content();
306
        $index = "";
307
 
308
        if( is_array( $classContent['pattern_selection'] ) and count( $classContent['pattern_selection'] ) > 0 )
309
        {
310
            $res = @preg_match( $this->getRegularExpression( $classContent ), $content, $matches );
311
 
312
            if( $res !== false )
313
            {
314
                foreach( $classContent['pattern_selection'] as $patternIndex )
315
                {
316
                    if( isset( $matches[$patternIndex] ) )
317
                    {
318
                        $index .= $matches[$patternIndex];
319
                    }
320
                }
321
            }
322
        }
323
        else
324
        {
325
            $index = $content;
326
        }
327
 
328
        return $index;
329
    }
330
 
331
    /*!
332
     \return true if the datatype can be indexed
333
    */
334
    function isIndexable()
335
    {
336
        return true;
337
    }
338
 
339
    function isInformationCollector()
340
    {
341
        return true;
342
    }
343
 
344
    function &sortKey( &$contentObjectAttribute )
345
    {
346
        $text = $contentObjectAttribute->content();
347
 
348
        return $text;
349
    }
350
 
351
    function &sortKeyType()
352
    {
353
        return 'string';
354
    }
355
 
356
    function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
357
    {
358
        $serializedContent = $classAttribute->attribute( 'data_text5' );
359
 
360
        $attributeParametersNode->appendChild( eZDOMDocument::createElementCDATANode( 'content', $serializedContent ) );
361
    }
362
 
363
    function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
364
    {
365
        $serializedContent = $attributeParametersNode->elementTextContentByName( 'content' );
366
 
367
        $classAttribute->setAttribute( 'data_text5', $serializedContent );
368
    }
369
 
370
 
371
    function getRegularExpression( &$classContent )
372
    {
373
        $regexp = $classContent['regexp'];
374
 
375
        if( !empty( $classContent['preset'] ) )
376
        {
377
            $ini =& eZINI::instance( 'regexpline.ini' );
378
            $presets = $ini->variable( 'GeneralSettings', 'RegularExpressions' );
379
 
380
            if( isset( $presets[$classContent['preset']] ) )
381
            {
382
                $regexp = $presets[$classContent['preset']];
383
            }
384
 
385
        }
386
 
387
        return $regexp;
388
    }
389
}
390
 
391
eZDataType::register( EZ_DATATYPESTRING_REGEXPLINE, "hmregexplinetype" );
392
 
393
?>