Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezisbn10to13converter.php
Go to the documentation of this file.
1 <?php
11 class eZISBN10To13Converter
12 {
13 public $Force;
21 public function __construct( $script, $cli, $params )
22 {
23 $this->Script = $script;
24 $this->Cli = $cli;
25 $this->AttributeArray = array();
26 if ( isset( $params['force'] ) )
27 {
28 $this->Force = $params['force'];
29 }
30 else
31 {
32 $this->Force = false;
33 }
34 }
35
36
43 function addAllClasses()
44 {
45 $db = eZDB::instance();
46 $this->Cli->output( $this->Cli->stylize( 'strong', 'Fetch All' ) . ' classes:' );
47 $sql = "SELECT id, data_int1 FROM ezcontentclass_attribute WHERE " .
48 "data_type_string='ezisbn' and version='0'";
49
50 $classAttributeList = $db->arrayQuery( $sql );
51 $status = false;
52 if ( count( $classAttributeList ) > 0 )
53 {
54 foreach ( $classAttributeList as $classAttributeItem )
55 {
56 $classAttributeID = $classAttributeItem['id'];
57 $isIsbn13 = $classAttributeItem['data_int1'];
58 $classAttribute = eZContentClassAttribute::fetch( $classAttributeID );
59
60 if ( $this->Force === true or $isIsbn13 == 1 )
61 {
62 $this->AttributeArray[$classAttributeID] = $classAttribute;
63 $status = true;
64 }
65 else
66 {
67 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The Class id ' .
68 $this->Cli->stylize( 'strong', $classAttribute->attribute( 'contentclass_id' ) ) . ' attribute id ' .
69 $this->Cli->stylize( 'strong', $classAttributeID ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' );
70 }
71
72 }
73 }
74
75 return $status;
76 }
77
84 function addClass( $classID )
85 {
86 $status = false;
87 if ( is_numeric( $classID ) )
88 {
89 $class = eZContentClass::fetch( $classID );
90 if ( $class instanceof eZContentClass )
91 {
92 $classFilter = array( 'data_type_string' => 'ezisbn' );
93 $classAttributes = $class->fetchAttributes();
94 $attributeFound = false;
95 if ( count( $classAttributes ) > 0 )
96 {
97 foreach ( $classAttributes as $attribute )
98 {
99 if ( $attribute->attribute( 'data_type_string' ) == 'ezisbn' )
100 {
101 if ( $attribute->attribute( 'data_int1' ) == 1 or $this->Force === true )
102 {
103 $attributeFound = true;
104 $this->AttributeArray[$attribute->attribute( 'id' )] = $attribute;
105 }
106 else
107 {
108 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
109 $this->Cli->stylize( 'strong', $attribute->attribute( 'id' ) ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' );
110 }
111 }
112 }
113 }
114
115 if ( $attributeFound === false )
116 {
117 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' Did not find any ISBN attributes in contentclass: ' .
118 $this->Cli->stylize( 'strong', $classID ) . '.' );
119 }
120 }
121 else
122 {
123 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' the class id ' .
124 $this->Cli->stylize( 'strong', $classID ) . ' does not exist.' );
125 }
126 }
127 else if ( $classID !== null )
128 {
129 $status = true;
130 $this->Cli->output( $this->Cli->stylize( 'error', 'Error:' ) . ' the class id need to be numeric.' );
131 }
132
133 return $status;
134 }
135
142 function addAttribute( $attributeID )
143 {
144 $status = false;
145 if ( is_numeric( $attributeID ) )
146 {
147 $classAttribute = eZContentClassAttribute::fetch( $attributeID );
148 if ( $classAttribute instanceof eZContentClassAttribute )
149 {
150 if ( $classAttribute->attribute( 'data_type_string' ) == 'ezisbn' )
151 {
152 if ( $classAttribute->attribute( 'data_int1' ) == 1 or $this->Force === true )
153 {
154 $this->AttributeArray[$classAttribute->attribute( 'id' )] = $classAttribute;
155 }
156 else
157 {
158 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
159 $this->Cli->stylize( 'strong', $attributeID ) . ' is not set to ISBN-13. Use --force to set the ISBN-13 flag' );
160 }
161 }
162 else
163 {
164 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
165 $this->Cli->stylize( 'strong', $attributeID ) . ' is not an ISBN datatype but of type ' .
166 $this->Cli->stylize( 'strong', $classAttribute->attribute( 'data_type_string' ) ) . '.' );
167 }
168 }
169 else
170 {
171 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' The attribute id ' .
172 $this->Cli->stylize( 'strong', $attributeID ) . ' does not exist.' );
173 }
174 }
175 else if ( $attributeID !== null )
176 {
177 $this->Cli->output( $this->Cli->stylize( 'error', 'Error:' ) . ' the attribute id need to be numeric.' );
178 $status = true;
179 }
180
181 return $status;
182 }
183
184
188 function attributeCount()
189 {
190 return count( $this->AttributeArray );
191 }
192
196 function execute()
197 {
198 foreach ( $this->AttributeArray as $classAttribute )
199 {
200 $contentClass = eZContentClass::fetch( $classAttribute->attribute( 'contentclass_id' ) );
201 $this->Cli->output( "Process class: " . $this->Cli->stylize( 'strong', $classAttribute->attribute( 'contentclass_id' ) ) .
202 " (" . $contentClass->attribute( 'name' ) . "), attribute id: " .
203 $this->Cli->stylize( 'strong', $classAttribute->attribute( 'id' ) ) .
204 " (" . $classAttribute->attribute( 'name' ) . "):" );
205
206 $this->updateContentFromClassAttribute( $classAttribute->attribute( 'id' ) );
207 $this->updateClassAttributeToISBN13( $classAttribute->attribute( 'id' ) );
208
209 $this->Cli->output( " Finished." );
210 }
211 }
212
217 function updateContentFromClassAttribute( $classAttributeID )
218 {
219 $asObject = true;
220
221 $i = 0;
222 $offset = 0;
223 $countList = 0;
224 $limit = 100;
225 $conditions = array( "contentclassattribute_id" => $classAttributeID );
226 $limitArray = array( 'offset' => $offset,
227 'limit' => $limit );
228
229 $sortArray = array( 'id' => 'asc' );
230
231 // Only fetch some objects each time to avoid memory problems.
232 while ( true )
233 {
234 $contentObjectAttributeList = eZPersistentObject::fetchObjectList( eZContentObjectAttribute::definition(),
235 null,
236 $conditions,
237 $sortArray,
238 $limitArray,
239 $asObject );
240 if ( count( $contentObjectAttributeList ) == 0 )
241 {
242 break;
243 }
244 foreach ( $contentObjectAttributeList as $contentObjectAttribute )
245 {
246 $this->updateContentObjectAttribute( $contentObjectAttribute );
247 }
248 $this->Cli->output( ".", false );
249 $i++;
250 if ( ( $i % 70 ) == 0 )
251 {
252 $this->Cli->output( ' ' . $this->Cli->stylize( 'strong', $i * $limit ) );
253 }
254 $countList = count( $contentObjectAttributeList );
255 unset( $contentObjectList );
256 $offset += $limit;
257 $limitArray = array( 'offset' => $offset,
258 'limit' => $limit );
259 }
260 $repeatLength = 70 - ( $i % 70 );
261 $count = ( ( $i - 1 ) * $limit ) + $countList;
262 $this->Cli->output( str_repeat( ' ', $repeatLength ) . ' ' . $this->Cli->stylize( 'strong', $count ), false );
263 }
264
270 function updateContentObjectAttribute( $contentObjectAttribute )
271 {
272 $isbnNumber = $contentObjectAttribute->attribute( 'data_text' );
273 $isbnValue = trim( $isbnNumber );
274 $error = false;
275
276 // If the number only consists of hyphen, it should be emty.
277 if ( preg_match( "/^\-+$/", $isbnValue ) )
278 {
279 $emtyValue = '';
280 $this->updateContentISBNNumber( $contentObjectAttribute, $emtyValue );
281 return true;
282 }
283 // Validate the ISBN number.
284 $digits = preg_replace( "/\-/", "", $isbnValue );
285
286 if ( trim( $digits ) != "" )
287 {
288 // If the length of the number is 10, it is an ISBN-10 number and need
289 // to be converted to ISBN-13.
290 if ( strlen( $digits ) == 10 )
291 {
292 $ean = eZISBNType::convertISBN10toISBN13( $digits );
293 }
294 else if ( strlen( $digits ) == 13 )
295 {
296 $ean = $digits;
297 }
298 else
299 {
300 $error = true;
301 }
302
303 if ( $error === false )
304 {
305 $isbn13 = new eZISBN13();
306 $formatedISBN13Value = $isbn13->formatedISBNValue( $ean, $error );
307 }
308
309 if ( $error === false )
310 {
311 $this->updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value );
312 }
313 else
314 {
315 $this->Cli->output( $this->Cli->stylize( 'warning', 'Warning:' ) . ' ISBN: ' .
316 $this->Cli->stylize( 'strong', $isbnNumber ) . ' is not valid. You need to update contentobject: ' .
317 $this->Cli->stylize( 'strong', $contentObjectAttribute->attribute( 'contentobject_id' ) ) . ' version: ' .
318 $this->Cli->stylize( 'strong', $contentObjectAttribute->attribute( 'version' ) ) . ' manually.' );
319 }
320 }
321 }
322
328 function updateClassAttributeToISBN13( $classAttributeID )
329 {
330 $db = eZDB::instance();
331 $sql = "UPDATE ezcontentclass_attribute SET data_int1='1' WHERE id='" . $classAttributeID . "'";
332 $db->query( $sql );
333 }
334
341 function updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value )
342 {
343 $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
344 $version = $contentObjectAttribute->attribute( 'version' );
345 $db = eZDB::instance();
346 $sql = "UPDATE ezcontentobject_attribute SET data_text='" . $db->escapeString( $formatedISBN13Value ) .
347 "' WHERE id='" . $contentObjectAttributeID . "' AND version='" . $version . "'" ;
348 $db->query( $sql );
349 }
350
351 public $Cli;
352 public $Script;
353 public $AttributeArray;
354 }
355
356 ?>
eZContentClassAttribute
Encapsulates data for a class attribute.
Definition: ezcontentclassattribute.php:19
eZContentClassAttribute\fetch
static fetch( $id, $asObject=true, $version=eZContentClass::VERSION_STATUS_DEFINED, $field_filters=null)
Definition: ezcontentclassattribute.php:380
eZContentClass
Handles eZ Publish content classes.
Definition: ezcontentclass.php:20
eZContentClass\fetch
static fetch( $id, $asObject=true, $version=eZContentClass::VERSION_STATUS_DEFINED, $user_id=false, $parent_id=null)
Definition: ezcontentclass.php:1116
eZContentObjectAttribute\definition
static definition()
Definition: ezcontentobjectattribute.php:38
eZDB\instance
static instance( $databaseImplementation=false, $databaseParameters=false, $forceNewInstance=false)
Definition: ezdb.php:117
eZISBN10To13Converter
Definition: ezisbn10to13converter.php:12
eZISBN10To13Converter\updateClassAttributeToISBN13
updateClassAttributeToISBN13( $classAttributeID)
Definition: ezisbn10to13converter.php:328
eZISBN10To13Converter\__construct
__construct( $script, $cli, $params)
Definition: ezisbn10to13converter.php:21
eZISBN10To13Converter\$Script
$Script
Definition: ezisbn10to13converter.php:352
eZISBN10To13Converter\attributeCount
attributeCount()
Definition: ezisbn10to13converter.php:188
eZISBN10To13Converter\$AttributeArray
$AttributeArray
Definition: ezisbn10to13converter.php:353
eZISBN10To13Converter\$Force
$Force
Definition: ezisbn10to13converter.php:13
eZISBN10To13Converter\updateContentFromClassAttribute
updateContentFromClassAttribute( $classAttributeID)
Definition: ezisbn10to13converter.php:217
eZISBN10To13Converter\addAllClasses
addAllClasses()
Definition: ezisbn10to13converter.php:43
eZISBN10To13Converter\updateContentISBNNumber
updateContentISBNNumber( $contentObjectAttribute, $formatedISBN13Value)
Definition: ezisbn10to13converter.php:341
eZISBN10To13Converter\$Cli
$Cli
Definition: ezisbn10to13converter.php:351
eZISBN10To13Converter\execute
execute()
Definition: ezisbn10to13converter.php:196
eZISBN10To13Converter\addClass
addClass( $classID)
Definition: ezisbn10to13converter.php:84
eZISBN10To13Converter\addAttribute
addAttribute( $attributeID)
Definition: ezisbn10to13converter.php:142
eZISBN10To13Converter\updateContentObjectAttribute
updateContentObjectAttribute( $contentObjectAttribute)
Definition: ezisbn10to13converter.php:270
eZISBN13
The class eZISBN13 handles ISBN-13 numbers.
Definition: ezisbn13.php:21
eZISBNType\convertISBN10toISBN13
static convertISBN10toISBN13( $isbnNr)
Definition: ezisbntype.php:189
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:806
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.