Subversion Repositories regexpline_backup

[/] [stable/] [1.1/] [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-2006  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.1
23
  \date    Thursday 26 February 2004 12:58: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,
44
                           ezi18n( 'extension/regexpline/datatype', 'Regular Expression Text', 'Datatype name' ),
45
                           array( 'serialize_supported' => true ) );
46
    }
47
 
48
    /*!
49
    Validates all variables given on content class level
50
     \return EZ_INPUT_VALIDATOR_STATE_ACCEPTED or EZ_INPUT_VALIDATOR_STATE_INVALID if
51
             the values are accepted or not
52
    */
53
    function validateClassAttributeHTTPInput( &$http, $base, &$classAttribute )
54
    {
55
        $regexpName = $base . "_hmregexpline_regexp_" . $classAttribute->attribute( 'id' );
56
        $presetName = $base . "_hmregexpline_preset_" . $classAttribute->attribute( 'id' );
57
 
58
        $regexp = $preset = '';
59
 
60
        if( $http->hasPostVariable( $regexpName ) )
61
        {
62
            $regexp = $http->postVariable( $regexpName );
63
        }
64
 
65
        if( $http->hasPostVariable( $presetName ) )
66
        {
67
            $preset = $http->postVariable( $presetName );
68
        }
69
 
70
        $content = array( 'regexp' => $regexp,
71
                          'preset' => $preset );
72
        $regexp = $this->getRegularExpression( $content );
73
 
74
        $check = @preg_match( $regexp, 'Dummy string' );
75
 
76
        if( $check === false )
77
        {
78
            return EZ_INPUT_VALIDATOR_STATE_INVALID;
79
        }
80
 
81
        return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
82
    }
83
 
84
    /*!
85
     Fetches all variables inputed on content class level
86
     \return true if fetching of class attributes are successfull, false if not
87
    */
88
    function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
89
    {
90
        $regexpName = $base . "_hmregexpline_regexp_" . $classAttribute->attribute( 'id' );
91
        $helpName = $base . "_hmregexpline_helptext_" . $classAttribute->attribute( 'id' );
92
        $patternName = $base . "_hmregexpline_namepattern_" . $classAttribute->attribute( 'id' );
93
        $presetName = $base . "_hmregexpline_preset_" . $classAttribute->attribute( 'id' );
94
 
95
        $content = $classAttribute->content();
96
 
97
        if( $http->hasPostVariable( $regexpName ) )
98
        {
99
            $content['regexp'] = $http->postVariable( $regexpName );
100
        }
101
 
102
        if( $http->hasPostVariable( $presetName ) )
103
        {
104
            $content['preset'] = $http->postVariable( $presetName );
105
        }
106
 
107
        if( $http->hasPostVariable( $helpName ) )
108
        {
109
            $content['help_text'] = $http->postVariable( $helpName );
110
        }
111
 
112
        if( $http->hasPostVariable( $patternName ) )
113
        {
114
            $content['naming_pattern'] = $http->postVariable( $patternName );
115
        }
116
        else if( $http->hasPostVariable( 'ContentClassHasInput' ) )
117
        {
118
            $content['naming_pattern'] = '';
119
        }
120
 
121
        $regexp = $this->getRegularExpression( $content );
122
 
123
        $subPatternCount = @preg_match_all( "/\((?!\?\:)(.*)\)/U", $regexp, $matches, PREG_PATTERN_ORDER );
124
 
125
        $content['subpattern_count'] = $subPatternCount == false ? 0 : $subPatternCount;
126
        $content['subpatterns'] = $matches[1];
127
 
128
        $classAttribute->setContent( $content );
129
        $classAttribute->store();
130
 
131
        return true;
132
    }
133
 
134
    function storeClassAttribute( &$classAttribute, $version )
135
    {
136
        $content = $classAttribute->content();
137
 
138
        $classAttribute->setAttribute( 'data_text5', serialize( $content ) );
139
    }
140
 
141
    function &classAttributeContent( &$classAttribute )
142
    {
143
        $content = unserialize( $classAttribute->attribute( 'data_text5' ) );
144
 
145
        if( !is_array( $content ) )
146
        {
147
            $content = array( 'regexp' => '',
148
                              'preset' => '',
149
                              'help_text' => '',
150
                              'subpattern_count' => 0,
151
                              'subpatterns' => array(),
152
                              'naming_pattern' => '' );
153
        }
154
        else if( isset( $content['pattern_selection'] ) )
155
        {
156
            $this->migratePatternSelection( $content );
157
        }
158
 
159
        return $content;
160
    }
161
 
162
    /*!
163
     Validates input on content object level
164
     \return EZ_INPUT_VALIDATOR_STATE_ACCEPTED or EZ_INPUT_VALIDATOR_STATE_INVALID if
165
             the values are accepted or not
166
    */
167
    function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
168
    {
169
        $status = $this->validateAttributeHTTPInput( $http, $base, $contentObjectAttribute, false );
170
 
171
        return $status;
172
    }
173
 
174
    /*!
175
     Fetches all variables from the object
176
     \return true if fetching of class attributes are successfull, false if not
177
    */
178
    function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
179
    {
180
        $textName = $base . "_hmregexpline_data_text_" . $contentObjectAttribute->attribute( 'id' );
181
 
182
        if( $http->hasPostVariable( $textName ) )
183
        {
184
            $text = $http->postVariable( $textName );
185
            $contentObjectAttribute->setContent( $text );
186
            $contentObjectAttribute->storeData();
187
            return true;
188
        }
189
        return false;
190
    }
191
 
192
    function storeObjectAttribute( &$contentObjectAttribute )
193
    {
194
        $text = $contentObjectAttribute->content();
195
 
196
        $contentObjectAttribute->setAttribute( 'data_text', $text );
197
    }
198
 
199
    /*!
200
     Returns the content.
201
    */
202
    function &objectAttributeContent( &$contentObjectAttribute )
203
    {
204
        $text = $contentObjectAttribute->attribute( 'data_text' );
205
 
206
        return $text;
207
    }
208
 
209
    function validateAttributeHTTPInput( &$http, $base, &$objectAttribute, $isInformationCollector = false )
210
    {
211
        $textName = $base . "_hmregexpline_data_text_" . $objectAttribute->attribute( 'id' );
212
        $classAttribute =& $objectAttribute->contentClassAttribute();
213
 
214
        $required = false;
215
        $must_validate = ( $isInformationCollector == $classAttribute->attribute( 'is_information_collector' ) );
216
 
217
        if( method_exists( $objectAttribute, 'validateIsRequired' ) )
218
        {
219
            $required = $objectAttribute->validateIsRequired();
220
        }
221
        else
222
        {
223
            $required = ( $classAttribute->attribute( 'is_required' ) == 1 );
224
        }
225
 
226
        if( $http->hasPostVariable( $textName ) )
227
        {
228
            $text = $http->postVariable( $textName );
229
            $classContent = $classAttribute->content();
230
 
231
            if( empty( $text ) and ( $required === true && $must_validate === true ) )
232
            {
233
                $objectAttribute->setValidationError( ezi18n( 'extension/regexpline/datatype', 'This is a required field which means you can\'t leave it empty' ) );
234
                return EZ_INPUT_VALIDATOR_STATE_INVALID;
235
            }
236
 
237
            if( !empty( $text ) and @preg_match( $this->getRegularExpression( $classContent ), $text ) === 0 )
238
            {
239
                // No match
240
                $objectAttribute->setValidationError( ezi18n( 'extension/regexpline/datatype', 'Your input did not meet the requirements.' ) );
241
                return EZ_INPUT_VALIDATOR_STATE_INVALID;
242
            }
243
        }
244
        else
245
        {
246
            if( $required === true && $must_validate === true )
247
            {
248
                $objectAttribute->setValidationError( ezi18n( 'extension/regexpline/datatype', 'This is a required field which means you can\'t leave it empty' ) );
249
                return EZ_INPUT_VALIDATOR_STATE_INVALID;
250
            }
251
        }
252
 
253
        return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
254
    }
255
 
256
    function validateCollectionAttributeHTTPInput( &$http, $base, &$objectAttribute )
257
    {
258
        $status = $this->validateAttributeHTTPInput( $http, $base, $objectAttribute, true );
259
 
260
        return $status;
261
    }
262
 
263
    function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
264
    {
265
        $textName = $base . "_hmregexpline_data_text_" . $contentObjectAttribute->attribute( 'id' );
266
 
267
        if( $http->hasPostVariable( $textName ) )
268
        {
269
            $text = $http->postVariable( $textName );
270
            $collectionAttribute->setAttribute( 'data_text', $text );
271
            return true;
272
        }
273
        return false;
274
    }
275
 
276
    /*!
277
     Returns the meta data used for storing search indeces.
278
    */
279
    function metaData( $contentObjectAttribute )
280
    {
281
        return $contentObjectAttribute->attribute( 'data_text' );
282
    }
283
 
284
    /*!
285
     Returns the value as it will be shown if this attribute is used in the object name pattern.
286
    */
287
    function title( &$contentObjectAttribute )
288
    {
289
        $classAttribute =& $contentObjectAttribute->contentClassAttribute();
290
        $classContent = $classAttribute->content();
291
        $content = $contentObjectAttribute->content();
292
        $index = "";
293
 
294
        // Exit if the input is empty
295
        if( $content == '' )
296
        {
297
            return $content;
298
        }
299
 
300
        if( isset( $classContent['pattern_selection'] ) )
301
        {
302
            $this->migratePatternSelection( $classContent );
303
        }
304
 
305
        $res = @preg_match( $this->getRegularExpression( $classContent ), $content, $matches );
306
 
307
        // Only replace if there's at least a match
308
        if( $res > 0 && $classContent['naming_pattern'] != '' )
309
        {
310
            $index = preg_replace( "/<([0-9]+)>/e", "\$matches[\\1]", $classContent['naming_pattern'] );
311
        }
312
 
313
        return $index;
314
    }
315
 
316
    /*!
317
     \return true if the datatype can be indexed
318
    */
319
    function isIndexable()
320
    {
321
        return true;
322
    }
323
 
324
    function isInformationCollector()
325
    {
326
        return true;
327
    }
328
 
329
    function &sortKey( &$contentObjectAttribute )
330
    {
331
        $text = $contentObjectAttribute->content();
332
 
333
        return $text;
334
    }
335
 
336
    function &sortKeyType()
337
    {
338
        return 'string';
339
    }
340
 
341
    function serializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
342
    {
343
        $serializedContent = $classAttribute->attribute( 'data_text5' );
344
 
345
        $attributeParametersNode->appendChild( eZDOMDocument::createElementCDATANode( 'content', $serializedContent ) );
346
    }
347
 
348
    function unserializeContentClassAttribute( &$classAttribute, &$attributeNode, &$attributeParametersNode )
349
    {
350
        $serializedContent = $attributeParametersNode->elementTextContentByName( 'content' );
351
 
352
        $classAttribute->setAttribute( 'data_text5', $serializedContent );
353
    }
354
 
355
 
356
    function getRegularExpression( &$classContent )
357
    {
358
        $regexp = $classContent['regexp'];
359
 
360
        if( !empty( $classContent['preset'] ) )
361
        {
362
            $ini =& eZINI::instance( 'regexpline.ini' );
363
            $presets = $ini->variable( 'GeneralSettings', 'RegularExpressions' );
364
 
365
            if( isset( $presets[$classContent['preset']] ) )
366
            {
367
                $regexp = $presets[$classContent['preset']];
368
            }
369
 
370
        }
371
 
372
        return $regexp;
373
    }
374
 
375
    function migratePatternSelection( &$classContent )
376
    {
377
        // Migrate the old pattern_selection to the newer naming_pattern
378
        $content['naming_pattern'] = '';
379
 
380
        foreach( $content['pattern_selection'] as $pattern )
381
        {
382
            $content['naming_pattern'] .= "<$pattern>";
383
        }
384
 
385
        unset( $content['pattern_selection'] );  
386
    }
387
}
388
 
389
eZDataType::register( EZ_DATATYPESTRING_REGEXPLINE, "hmregexplinetype" );
390
 
391
?>