/kernel/classes/datatypes/ezisbn/ezisbnregistrantrange.php

https://bitbucket.org/ericsagnes/ezpublish-multisite · PHP · 205 lines · 109 code · 12 blank · 84 comment · 4 complexity · d2b85226570cd03afec0593fe709a727 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the eZISBNRegistrantRange class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version 2012.8
  8. * @package kernel
  9. */
  10. /*!
  11. \class eZISBNRegistrantRange ezisbnregistrantrange.php
  12. \brief The class eZISBNRegistrantRange handles Registrant ranges.
  13. Has information about how the different ranges the registrant element
  14. could be in. Example: From 00 to 19 and continues from 200-699.
  15. This means that the length of the registrant can differ from
  16. range to range.
  17. The registrant element is the third element in the ISBN-13 number, after
  18. the Prefix and Registration group number.
  19. Example: 978-0-11-000222-4 where 11 is the registrant number.
  20. The different Registrant ranges are described in more detail at
  21. http://www.isbn-international.org
  22. */
  23. class eZISBNRegistrantRange extends eZPersistentObject
  24. {
  25. /*!
  26. Constructor
  27. */
  28. function eZISBNRegistrantRange( $row )
  29. {
  30. $this->eZPersistentObject( $row );
  31. }
  32. /*!
  33. Definition of the ranges for ISBN Registrant.
  34. */
  35. static function definition()
  36. {
  37. return array( 'fields' => array( 'id' => array( 'name' => 'ID',
  38. 'datatype' => 'integer',
  39. 'default' => 0,
  40. 'required' => true ),
  41. 'from_number' => array( 'name' => 'FromNumber',
  42. 'datatype' => 'integer',
  43. 'default' => 0,
  44. 'required' => true ),
  45. 'to_number' => array( 'name' => 'ToNumber',
  46. 'datatype' => 'integer',
  47. 'default' => 0,
  48. 'required' => true ),
  49. 'registrant_from' => array( 'name' => 'RegistrantFrom',
  50. 'datatype' => 'string',
  51. 'default' => '',
  52. 'required' => true ),
  53. 'registrant_to' => array( 'name' => 'RegistrantTo',
  54. 'datatype' => 'string',
  55. 'default' => '',
  56. 'required' => true ),
  57. 'registrant_length' => array( 'name' => 'RegistrantLength',
  58. 'datatype' => 'integer',
  59. 'default' => 0,
  60. 'required' => true ),
  61. 'isbn_group_id' => array( 'name' => 'ISBNGroupID',
  62. 'datatype' => 'integer',
  63. 'default' => 0,
  64. 'required' => true,
  65. 'foreign_class' => 'eZISBNGroup',
  66. 'foreign_attribute' => 'id',
  67. 'multiplicity' => '1..*' ),
  68. ),
  69. 'keys' => array( 'id' ),
  70. 'increment_key' => 'id',
  71. 'class_name' => 'eZISBNRegistrantRange',
  72. 'name' => 'ezisbn_registrant_range' );
  73. }
  74. /*!
  75. \static
  76. \param $ISBNGroupID The id that point to the ISBN Group object
  77. (Which contain info about the area and the unique group number).
  78. \param $fromNumber Group is starting from test number, which is based on. Example: 20000
  79. the 5 numbers after the Prefix number and Registration Group number.
  80. \param $toNumber Group is ending on test number, which is based on. Example: 69999
  81. the 5 numbers after the Prefix number and Registration Group number.
  82. \param $registrantFrom Registrant number is starting on, based on the length set
  83. in the registrant range. Is a string to support 0 in front. Example: 200
  84. \param $registrantTo Registrant number ending on, based on the length set
  85. in the registrant range. Is a string to support 0 in front. Example: 699
  86. \param $length How many characters $registrantFrom and $registrantTo should have.
  87. Create a new registrant range for an ISBN group / area.
  88. \return A new eZISBNRegistrantRange object.
  89. */
  90. static function create( $ISBNGroupID, $fromNumber, $toNumber, $registrantFrom, $registrantTo, $length )
  91. {
  92. $row = array(
  93. 'id' => null,
  94. 'from_number' => $fromNumber,
  95. 'to_number' => $toNumber,
  96. 'registrant_from' => $registrantFrom,
  97. 'registrant_to' => $registrantTo,
  98. 'registrant_length' => $length,
  99. 'isbn_group_id' => $ISBNGroupID );
  100. return new eZISBNRegistrantRange( $row );
  101. }
  102. /*!
  103. \static
  104. Removes the registrant area based on ID \a $id.
  105. */
  106. static function removeByID( $id )
  107. {
  108. eZPersistentObject::removeObject( eZISBNRegistrantRange::definition(),
  109. array( 'id' => $id ) );
  110. }
  111. /*!
  112. \static
  113. Fetch the registrant group for a unique registration group area.
  114. \param $groupID The id that point to the ISBN Group object
  115. (Which contain info about the area and the unique group number).
  116. \param $asObject If the result should be returned as object or an array.
  117. \return the registrant list for an ISBN registration group id.
  118. */
  119. static function fetchListByGroupID( $groupID, $asObject = true )
  120. {
  121. $conditions = array( 'isbn_group_id' => $groupID );
  122. $sortArray = array( array( 'from_number' => 'asc' ) );
  123. return eZPersistentObject::fetchObjectList( eZISBNRegistrantRange::definition(),
  124. null, $conditions, $sortArray, null,
  125. $asObject );
  126. }
  127. /*!
  128. \static
  129. Will extract the registrant number based on the different ranges
  130. which is based on the 5 first digits after the Prefix field and the registration group number.
  131. \param $isbnNr Should be a stripped down ISBN number with just the digits (ean number).
  132. \param $group is an object of eZISBNGroup, which needs to be known before this function is called.
  133. Contains information about the group itself.
  134. \param $groupRange is an object of eZISBNGroupRange, which needs to be known before this function is called.
  135. Contains information about the valid ranges for the ISBN group.
  136. \param $registrantLength is the length of the Registrant in the range that was found.
  137. Is sent back in the reference variable.
  138. \return the registrant range object if found and false if not found.
  139. */
  140. static function extractRegistrant( $isbnNr, $group, $groupRange, &$registrantLength )
  141. {
  142. $registrant = false;
  143. if ( $group instanceof eZISBNGroup and
  144. $groupRange instanceof eZISBNGroupRange )
  145. {
  146. $groupLength = $groupRange->attribute( 'group_length' );
  147. $groupID = $group->attribute( 'id' );
  148. $registrantOffset = 3 + $groupLength;
  149. $testSegment = substr( $isbnNr, $registrantOffset, 5 );
  150. if ( is_numeric( $testSegment ) )
  151. {
  152. $conditions = array( 'from_number' => array( '<=', $testSegment ),
  153. 'to_number' => array( '>=', $testSegment ),
  154. 'isbn_group_id' => $groupID );
  155. $groupRangeArray = eZPersistentObject::fetchObjectList( eZISBNRegistrantRange::definition(),
  156. null, $conditions, null, null,
  157. true );
  158. if ( count( $groupRangeArray ) == 1 )
  159. {
  160. $length = $groupRangeArray[0]->attribute( 'registrant_length' );
  161. // Copy the length to send it back as a reference.
  162. $registrantLength = $length;
  163. $registrant = $groupRangeArray[0];
  164. }
  165. }
  166. }
  167. return $registrant;
  168. }
  169. /*!
  170. \static
  171. Removes all ISBN group ranges from the database.
  172. */
  173. static function cleanAll()
  174. {
  175. $db = eZDB::instance();
  176. $definition = eZISBNRegistrantRange::definition();
  177. $table = $definition['name'];
  178. $sql = "TRUNCATE TABLE " . $table;
  179. $db->query( $sql );
  180. }
  181. }
  182. ?>