Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
NativeXmlPublicationFormatFilter.inc.php
1 <?php
2 
16 import('lib.pkp.plugins.importexport.native.filter.NativeXmlRepresentationFilter');
17 
23  function NativeXmlPublicationFormatFilter($filterGroup) {
24  parent::NativeXmlRepresentationFilter($filterGroup);
25  }
26 
27  //
28  // Implement template methods from NativeImportFilter
29  //
34  function getPluralElementName() {
35  return 'publication_formats'; // defined if needed in the future.
36  }
37 
43  return 'publication_format';
44  }
45 
46  //
47  // Implement template methods from PersistableFilter
48  //
52  function getClassName() {
53  return 'plugins.importexport.native.filter.NativeXmlPublicationFormatFilter';
54  }
55 
56 
62  function handleElement($node) {
63  $deployment = $this->getDeployment();
64  $context = $deployment->getContext();
65  $submission = $deployment->getSubmission();
66  assert(is_a($submission, 'Submission'));
67 
68  $representation = parent::handleElement($node);
69 
70  if ($node->getAttribute('approved') == 'true') $representation->setIsApproved(true);
71  if ($node->getAttribute('physical_format') == 'true') $representation->setPhysicalFormat(true);
72 
73  $representationDao = Application::getRepresentationDAO();
74  $representationDao->insertObject($representation);
75 
76  // Handle metadata in subelements. Do this after the insertObject() call because it
77  // creates other DataObjects which depend on a representation id.
78  for ($n = $node->firstChild; $n !== null; $n=$n->nextSibling) if (is_a($n, 'DOMElement')) switch($n->tagName) {
79  case 'Product': $this->_processProductNode($n, $this->getDeployment(), $representation); break;
80  default:
81  }
82 
83  // Update the object.
84  $representationDao->updateObject($representation);
85 
86  return $representation;
87  }
88 
94  function _processProductNode($node, $deployment, &$representation) {
95 
96  import('plugins.importexport.native.Onix30ExportDeployment');
98 
99  $representation->setProductCompositionCode($this->_extractTextFromNode($node, $onixDeployment, 'ProductComposition'));
100  $representation->setEntryKey($this->_extractTextFromNode($node, $onixDeployment, 'ProductForm'));
101  $representation->setProductFormDetailCode($this->_extractTextFromNode($node, $onixDeployment, 'ProductFormDetail'));
102  $representation->setImprint($this->_extractTextFromNode($node, $onixDeployment, 'ImprintName'));
103  $representation->setTechnicalProtectionCode($this->_extractTextFromNode($node, $onixDeployment, 'EpubTechnicalProtection'));
104  $representation->setCountryManufactureCode($this->_extractTextFromNode($node, $onixDeployment, 'CountryOfManufacture'));
105  $this->_extractMeasureContent($node, $onixDeployment, $representation);
106  $this->_extractExtentContent($node, $onixDeployment, $representation);
107 
108  // if this is a published monograph, extract the Audience elements and store them.
109  $publishedMonographDao = DAORegistry::getDAO('PublishedMonographDAO');
110  $publishedMonograph = $publishedMonographDao->getById($representation->getSubmissionId());
111  if ($publishedMonograph) {
112  $publishedMonograph->setAudience($this->_extractTextFromNode($node, $onixDeployment, 'AudienceCodeType'));
113  $publishedMonograph->setAudienceRangeQualifier($this->_extractTextFromNode($node, $onixDeployment, 'AudienceRangeQualifier'));
114  $this->_extractAudienceRangeContent($node, $onixDeployment, $representation);
115  $publishedMonographDao->updateObject($publishedMonograph);
116  }
117 
118  // Things below here require a publication format id since they are dependent on the PublicationFormat.
119 
120  // Extract ProductIdentifier elements.
121  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'ProductIdentifier');
122 
123  if ($nodeList->length > 0) {
124  $identificationCodeDao = DAORegistry::getDAO('IdentificationCodeDAO');
125  for ($i = 0 ; $i < $nodeList->length ; $i++) {
126  $n = $nodeList->item($i);
127  $identificationCode = $identificationCodeDao->newDataObject();
128  $identificationCode->setPublicationFormatId($representation->getId());
129  for ($o = $n->firstChild; $o !== null; $o=$o->nextSibling) if (is_a($o, 'DOMElement')) switch($o->tagName) {
130  case 'onix:ProductIDType': $identificationCode->setCode($o->textContent); break;
131  case 'onix:IDValue': $identificationCode->setValue($o->textContent); break;
132  }
133  // if this is a DOI, use the DOI-plugin structure instead.
134  if ($identificationCode->getCode() == '06') { // DOI code
135  $representation->setStoredPubId('doi', $identificationCode->getValue());
136  } else {
137  $identificationCodeDao->insertObject($identificationCode);
138  }
139 
140  unset($identificationCode);
141  }
142  }
143 
144  // Extract PublishingDate elements.
145  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'PublishingDate');
146 
147  if ($nodeList->length > 0) {
148  $publicationDateDao = DAORegistry::getDAO('PublicationDateDAO');
149  for ($i = 0 ; $i < $nodeList->length ; $i++) {
150  $n = $nodeList->item($i);
151  $date = $publicationDateDao->newDataObject();
152  $date->setPublicationFormatId($representation->getId());
153  for ($o = $n->firstChild; $o !== null; $o=$o->nextSibling) if (is_a($o, 'DOMElement')) switch($o->tagName) {
154  case 'onix:PublishingDateRole': $date->setRole($o->textContent); break;
155  case 'onix:Date':
156  $date->setDate($o->textContent);
157  $date->setDateFormat($o->getAttribute('dateformat'));
158  break;
159  }
160 
161  $publicationDateDao->insertObject($date);
162  unset($date);
163  }
164  }
165 
166  // Extract SalesRights elements.
167  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'SalesRights');
168  if ($nodeList->length > 0) {
169  $salesRightsDao = DAORegistry::getDAO('SalesRightsDAO');
170  for ($i = 0 ; $i < $nodeList->length ; $i ++) {
171  $salesRights = $salesRightsDao->newDataObject();
172  $salesRights->setPublicationFormatId($representation->getId());
173  $salesRightsNode = $nodeList->item($i);
174  $salesRightsROW = $this->_extractTextFromNode($salesRightsNode, $onixDeployment, 'ROWSalesRightsType');
175  if ($salesRightsROW) {
176  $salesRights->setROWSetting(true);
177  $salesRights->setType($salesRightsROW);
178  } else {
179  // Not a 'rest of world' sales rights entry. Parse the Territory elements as well.
180  $salesRights->setType($this->_extractTextFromNode($salesRightsNode, $onixDeployment, 'SalesRightsType'));
181  $salesRights->setROWSetting(false);
182  $territoryNodeList = $salesRightsNode->getElementsByTagNameNS($onixDeployment->getNamespace(), 'Territory');
183  assert($territoryNodeList->length == 1);
184  $territoryNode = $territoryNodeList->item(0);
185  for ($o = $territoryNode->firstChild; $o !== null; $o=$o->nextSibling) if (is_a($o, 'DOMElement')) switch($o->tagName) {
186  case 'onix:RegionsIncluded': $salesRights->setRegionsIncluded(preg_split('/\s+/', $o->textContent)); break;
187  case 'onix:CountriesIncluded': $salesRights->setCountriesIncluded(preg_split('/\s+/', $o->textContent)); break;
188  case 'onix:RegionsExcluded': $salesRights->setRegionsExcluded(preg_split('/\s+/', $o->textContent)); break;
189  case 'onix:CountriesExcluded': $salesRights->setCountriesExcluded(preg_split('/\s+/', $o->textContent)); break;
190  }
191  }
192  }
193 
194  $salesRightsDao->insertObject($salesRights);
195  unset($salesRights);
196  }
197 
198  // Extract ProductSupply elements. Contains Markets, Pricing, Suppliers, and Sales Agents.
199  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'ProductSupply');
200  if ($nodeList->length > 0) {
201  $marketDao = DAORegistry::getDAO('MarketDAO');
202  $representativeDao = DAORegistry::getDAO('RepresentativeDAO');
203 
204  for ($i = 0 ; $i < $nodeList->length ; $i ++) {
205  $productSupplyNode = $nodeList->item($i);
206  $market = $marketDao->newDataObject();
207  $market->setPublicationFormatId($representation->getId());
208  // parse out the Territory for this market.
209  $territoryNodeList = $productSupplyNode->getElementsByTagNameNS($onixDeployment->getNamespace(), 'Territory');
210  assert($territoryNodeList->length == 1);
211  $territoryNode = $territoryNodeList->item(0);
212  for ($o = $territoryNode->firstChild; $o !== null; $o=$o->nextSibling) if (is_a($o, 'DOMElement')) switch($o->tagName) {
213  case 'onix:RegionsIncluded': $market->setRegionsIncluded(preg_split('/\s+/', $o->textContent)); break;
214  case 'onix:CountriesIncluded': $market->setCountriesIncluded(preg_split('/\s+/', $o->textContent)); break;
215  case 'onix:RegionsExcluded': $market->setRegionsExcluded(preg_split('/\s+/', $o->textContent)); break;
216  case 'onix:CountriesExcluded': $market->setCountriesExcluded(preg_split('/\s+/', $o->textContent)); break;
217  }
218 
219  // Market date information.
220  $market->setDate($this->_extractTextFromNode($productSupplyNode, $onixDeployment, 'Date'));
221  $market->setDateRole($this->_extractTextFromNode($productSupplyNode, $onixDeployment, 'MarketDateRole'));
222  $market->setDateFormat($this->_extractTextFromNode($productSupplyNode, $onixDeployment, 'DateFormat'));
223 
224  // A product supply may have an Agent. Look for the PublisherRepresentative element and parse if found.
225  $publisherRepNodeList = $productSupplyNode->getElementsByTagNameNS($onixDeployment->getNamespace(), 'PublisherRepresentative');
226  if ($publisherRepNodeList->length == 1) {
227  $publisherRepNode = $publisherRepNodeList->item(0);
228  $representative = $representativeDao->newDataObject();
229  $representative->setMonographId($deployment->getSubmission()->getId());
230  $representative->setRole($this->_extractTextFromNode($publisherRepNode, $onixDeployment, 'AgentRole'));
231  $representative->setName($this->_extractTextFromNode($publisherRepNode, $onixDeployment, 'AgentName'));
232  $representative->setUrl($this->_extractTextFromNode($publisherRepNode, $onixDeployment, 'WebsiteLink'));
233 
234  // to prevent duplicate Agent creation, check to see if this agent already exists. If it does, use it instead of creating a new one.
235  $existingAgents = $representativeDao->getAgentsByMonographId($deployment->getSubmission()->getId());
236  $foundAgent = false;
237  while ($agent = $existingAgents->next()) {
238  if ($agent->getRole() == $representative->getRole() && $agent->getName() == $representative->getName() && $agent->getUrl() == $representative->getUrl()) {
239  $market->setAgentId($agent->getId());
240  $foundAgent = true;
241  break;
242  }
243  }
244  if (!$foundAgent) {
245  $market->setAgentId($representativeDao->insertObject($representative));
246  }
247  }
248 
249  // Now look for a SupplyDetail element, for the Supplier information.
250  $supplierNodeList = $productSupplyNode->getElementsByTagNameNS($onixDeployment->getNamespace(), 'Supplier');
251  if ($supplierNodeList->length == 1) {
252  $supplierNode = $supplierNodeList->item(0);
253  $representative = $representativeDao->newDataObject();
254  $representative->setMonographId($deployment->getSubmission()->getId());
255  $representative->setRole($this->_extractTextFromNode($supplierNode, $onixDeployment, 'SupplierRole'));
256  $representative->setName($this->_extractTextFromNode($supplierNode, $onixDeployment, 'SupplierName'));
257  $representative->setPhone($this->_extractTextFromNode($supplierNode, $onixDeployment, 'TelephoneNumber'));
258  $representative->setFax($this->_extractTextFromNode($supplierNode, $onixDeployment, 'FaxNumber'));
259  $representative->setEmail($this->_extractTextFromNode($supplierNode, $onixDeployment, 'EmailAddress'));
260  $representative->setUrl($this->_extractTextFromNode($supplierNode, $onixDeployment, 'WebsiteLink'));
261  $representative->setIsSupplier(true);
262 
263  // Again, to prevent duplicate Supplier creation, check to see if this rep already exists. If it does, use it instead of creating a new one.
264  $existingSuppliers = $representativeDao->getSuppliersByMonographId($deployment->getSubmission()->getId());
265  $foundSupplier = false;
266  while ($supplier = $existingSuppliers->next()) {
267  if ($supplier->getRole() == $representative->getRole() && $supplier->getName() == $representative->getName() &&
268  $supplier->getUrl() == $representative->getUrl() && $supplier->getFax() == $representative->getFax() &&
269  $supplier->getPhone() == $representative-> getPhone() && $supplier->getEmail() == $representative->getEmail()) {
270  $market->setSupplierId($supplier->getId());
271  $foundSupplier = true;
272  break;
273  }
274  }
275  if (!$foundSupplier) {
276  $market->setSupplierId($representativeDao->insertObject($representative));
277  }
278 
279  $priceNodeList = $productSupplyNode->getElementsByTagNameNS($onixDeployment->getNamespace(), 'Price');
280  if ($priceNodeList->length == 1) {
281  $priceNode = $priceNodeList->item(0);
282  $market->setPriceTypeCode($this->_extractTextFromNode($priceNode, $onixDeployment, 'PriceType'));
283  $market->setDiscount($this->_extractTextFromNode($priceNode, $onixDeployment, 'DiscountPercent'));
284  $market->setPrice($this->_extractTextFromNode($priceNode, $onixDeployment, 'PriceAmount'));
285  $market->setTaxTypeCode($this->_extractTextFromNode($priceNode, $onixDeployment, 'TaxType'));
286  $market->setTaxRateCode($this->_extractTextFromNode($priceNode, $onixDeployment, 'TaxRateCode'));
287  $market->setCurrencyCode($this->_extractTextFromNode($priceNode, $onixDeployment, 'CurrencyCode'));
288  }
289  }
290 
291  // Extract Pricing information for this format.
292  $representation->setReturnableIndicatorCode($this->_extractTextFromNode($supplierNode, $onixDeployment, 'ReturnsCode'));
293  $representation->getProductAvailabilityCode($this->_extractTextFromNode($supplierNode, $onixDeployment, 'ProductAvailability'));
294 
295  $marketDao->insertObject($market);
296  }
297  }
298  }
299 
307  function _extractTextFromNode($node, $onixDeployment, $nodeName) {
308  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), $nodeName);
309  if ($nodeList->length == 1) {
310  $n = $nodeList->item(0);
311  return $n->textContent;
312  } else
313  return null;
314  }
315 
322  function _extractExtentContent($node, $onixDeployment, &$representation) {
323  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'Extent');
324 
325  for ($i = 0 ; $i < $nodeList->length ; $i++) {
326  $n = $nodeList->item($i);
327  $extentType = $this->_extractTextFromNode($node, $onixDeployment, 'ExtentType');
328  $extentValue = $this->_extractTextFromNode($node, $onixDeployment, 'ExtentValue');
329 
330  switch ($extentType) {
331  case '08': // Digital
332  $representation->setFileSize($extentValue);
333  break;
334  case '00': // Physical, front matter.
335  $representation->setFrontMatter($extentValue);
336  break;
337  case '04': // Physical, back matter.
338  $representation->setBackMatter($extentValue);
339  break;
340  }
341  }
342  }
343 
350  function _extractMeasureContent($node, $onixDeployment, &$representation) {
351  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'Measure');
352  for ($i = 0 ; $i < $nodeList->length ; $i++) {
353  $n = $nodeList->item($i);
354  $measureType = $this->_extractTextFromNode($node, $onixDeployment, 'MeasureType');
355  $measurement = $this->_extractTextFromNode($node, $onixDeployment, 'Measurement');
356  $measureUnitCode = $this->_extractTextFromNode($node, $onixDeployment, 'MeasureUnitCode');
357 
358  // '01' => 'Height', '02' => 'Width', '03' => 'Thickness', '08' => 'Weight'
359  switch ($measureType) {
360  case '01':
361  $representation->setHeight($measurement);
362  $representation->setHeightUnitCode($measureUnitCode);
363  break;
364  case '02':
365  $representation->setWidth($measurement);
366  $representation->setWidthUnitCode($measureUnitCode);
367  break;
368  case '03':
369  $representation->setThickness($measurement);
370  $representation->setThicknessUnitCode($measureUnitCode);
371  break;
372  case '08':
373  $representation->setWeight($measurement);
374  $representation->setWeightUnitCode($measureUnitCode);
375  break;
376  }
377  }
378  }
379 
387  function _extractAudienceRangeContent($node, $onixDeployment, &$representation) {
388  $nodeList = $node->getElementsByTagNameNS($onixDeployment->getNamespace(), 'AudienceRange');
389  for ($i = 0 ; $i < $nodeList->length ; $i++) {
390  $n = $nodeList->item($i);
391  $audienceRangePrecision = 0;
392  for ($o = $n->firstChild; $o !== null; $o=$o->nextSibling) if (is_a($o, 'DOMElement')) switch($o->tagName) {
393  case 'AudienceRangePrecision': $audienceRangePrevision = $o->textContent; break;
394  case 'AudienceRangeValue':
395  switch ($audienceRangePrecision) {
396  case '01':
397  $representation->setAudienceRangeExact($o->textContent);
398  break;
399  case '03':
400  $representation->setAudienceRangeTo($o->textContent);
401  break;
402  case '04':
403  $representation->setAudienceRangeFrom($o->textContent);
404  break;
405  }
406  break;
407  }
408  }
409  }
410 }
411 
412 ?>
static & getDAO($name, $dbconn=null)
static getRepresentationDAO()
_extractAudienceRangeContent($node, $onixDeployment, &$representation)
_extractMeasureContent($node, $onixDeployment, &$representation)
_extractTextFromNode($node, $onixDeployment, $nodeName)
Base class that converts a Native XML document to a set of authors.
_extractExtentContent($node, $onixDeployment, &$representation)
Class that converts a Native XML document to a set of publication formats.
_processProductNode($node, $deployment, &$representation)
Class configuring the native import/export process to this application&#39;s specifics.