00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_GoogleBase_Model_Service_Item extends Mage_GoogleBase_Model_Service
00035 {
00036 const DEFAULT_ITEM_TYPE = 'products';
00037 const DEFAULT_ATTRIBUTE_TYPE = 'text';
00038
00039
00040
00041
00042
00043
00044 protected $_object = null;
00045
00046
00047
00048
00049
00050
00051 protected $_item = null;
00052
00053
00054
00055
00056
00057
00058
00059 public function setObject($object)
00060 {
00061 $this->_object = $object;
00062 return $this;
00063 }
00064
00065
00066
00067
00068
00069
00070 public function getObject()
00071 {
00072 return $this->_object;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 public function setItem($item)
00082 {
00083 $this->_item = $item;
00084 return $this;
00085 }
00086
00087
00088
00089
00090
00091
00092 public function getItem()
00093 {
00094 return $this->_item;
00095 }
00096
00097
00098
00099
00100
00101
00102 public function getService($storeId = null)
00103 {
00104 if ($storeId === null) {
00105 $storeId = $this->getStoreId();
00106 }
00107 return parent::getService($storeId);
00108 }
00109
00110
00111
00112
00113
00114
00115 public function insert()
00116 {
00117 $this->_checkItem();
00118 $service = $this->getService();
00119 $entry = $service->newItemEntry();
00120 $this->setEntry($entry);
00121 $this->_prepareEnrtyForSave();
00122 $this->getEntry()->setItemType($this->_getItemType());
00123 $entry = $service->insertGbaseItem($this->getEntry());
00124 $this->setEntry($entry);
00125 $entryId = $this->getEntry()->getId();
00126 $published = $this->gBaseDate2DateTime($this->getEntry()->getPublished()->getText());
00127 $this->getItem()
00128 ->setGbaseItemId($entryId)
00129 ->setPublished($published);
00130
00131 if ($expires = $this->_getAttributeValue('expiration_date')) {
00132 $expires = $this->gBaseDate2DateTime($expires);
00133 $this->getItem()->setExpires($expires);
00134 }
00135 }
00136
00137
00138
00139
00140
00141
00142 public function update()
00143 {
00144 $this->_checkItem();
00145 $service = $this->getService();
00146 $entry = $service->getGbaseItemEntry( $this->getItem()->getGbaseItemId() );
00147 $this->setEntry($entry);
00148 $this->_prepareEnrtyForSave();
00149 $entry = $service->updateGbaseItem($this->getEntry());
00150
00151 }
00152
00153
00154
00155
00156
00157
00158 public function delete()
00159 {
00160 $this->_checkItem();
00161
00162 $service = $this->getService();
00163 $entry = $service->getGbaseItemEntry( $this->getItem()->getGbaseItemId() );
00164 return $service->deleteGbaseItem($entry, $this->getDryRun());
00165 }
00166
00167
00168
00169
00170
00171
00172 public function hide()
00173 {
00174 $this->_saveDraft(true);
00175 return $this;
00176 }
00177
00178
00179
00180
00181
00182
00183 public function activate()
00184 {
00185 $this->_saveDraft(false);
00186 return $this;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195 protected function _saveDraft($yes = true)
00196 {
00197 $this->_checkItem();
00198
00199 $service = $this->getService();
00200 $entry = $service->getGbaseItemEntry( $this->getItem()->getGbaseItemId() );
00201
00202 $draftText = $yes ? 'yes' : 'no';
00203 $draft = $service->newDraft($draftText);
00204 $control = $service->newControl($draft);
00205
00206 $entry->setControl($control);
00207 $entry->save();
00208 return $this;
00209 }
00210
00211
00212
00213
00214
00215
00216 protected function _prepareEnrtyForSave()
00217 {
00218 $object = $this->getObject();
00219 if (!($object instanceof Varien_Object)) {
00220 Mage::throwException(Mage::helper('googlebase')->__('Object model is not specified to save Google Base entry'));
00221 }
00222
00223 $this->_setUniversalData();
00224
00225 $attributes = $this->getAttributeValues();
00226 if (is_array($attributes) && count($attributes)) {
00227 foreach ($attributes as $name => $data) {
00228
00229 $name = $this->_normalizeString($name);
00230 $value = isset($data['value']) ? $data['value'] : '';
00231 $type = isset($data['type']) && $data['type'] ? $data['type'] : self::DEFAULT_ATTRIBUTE_TYPE;
00232
00233 $customSetter = '_setAttribute' . ucfirst($name);
00234 if (method_exists($this, $customSetter)) {
00235 $this->$customSetter($name, $value, $type);
00236 } else {
00237 $this->_setAttribute($name, $value, $type);
00238 }
00239 }
00240 }
00241 return $this;
00242 }
00243
00244
00245
00246
00247
00248
00249 protected function _setUniversalData()
00250 {
00251 $service = $this->getService();
00252 $object = $this->getObject();
00253 $entry = $this->getEntry();
00254
00255 $this->_setAttribute('id', $object->getId() . '_' . $this->getStoreId(), 'text');
00256
00257 if ($object->getName()) {
00258 $title = $service->newTitle()->setText( $object->getName() );
00259 $entry->setTitle($title);
00260 }
00261
00262 if ($object->getUrl()) {
00263 $links = $entry->getLink();
00264 if (!is_array($links)) {
00265 $links = array();
00266 }
00267 $link = $service->newLink();
00268 $link->setHref($object->getUrl());
00269 $link->setRel('alternate');
00270 $link->setType('text/html');
00271 if ($object->getName()) {
00272 $link->setTitle($object->getName());
00273 }
00274 $links[0] = $link;
00275 $entry->setLink($links);
00276 }
00277
00278 if ($object->getDescription()) {
00279 $content = $service->newContent()->setText( $object->getDescription() );
00280 $entry->setContent($content);
00281 }
00282
00283 if ($object->getQuantity()) {
00284 $quantity = $object->getQuantity() ? max(1, (int)$object->getQuantity()) : 1;
00285 $this->_setAttribute('quantity', $quantity, 'int');
00286 }
00287
00288 $targetCountry = $this->getConfig()->getTargetCountry($this->getStoreId());
00289
00290 if ($object->getData('image_url')) {
00291 $this->_setAttribute('image_link', $object->getData('image_url'), 'url');
00292 }
00293
00294 $this->_setAttribute('target_country', $targetCountry, 'text');
00295 $this->_setAttribute('item_language', $this->getConfig()->getCountryInfo($targetCountry, 'language'), 'text');
00296
00297 return $this;
00298 }
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 protected function _setAttribute($attribute, $value, $type = 'text')
00310 {
00311 $entry = $this->getEntry();
00312 $gBaseAttribute = $entry->getGbaseAttribute($attribute);
00313 if (isset($gBaseAttribute[0]) && is_object($gBaseAttribute[0])) {
00314 $gBaseAttribute[0]->text = $value;
00315 } else {
00316 $entry->addGbaseAttribute($attribute, $value, $type);
00317 }
00318 return $this;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 protected function _setAttributePrice($attribute, $value, $type = 'text')
00331 {
00332 $targetCountry = $this->getConfig()->getTargetCountry($this->getStoreId());
00333 $this->_setAttribute(
00334 $this->getConfig()->getCountryInfo($targetCountry, 'price_attribute_name', $this->getStoreId()),
00335 sprintf('%.2f', $value),
00336 'floatUnit'
00337 );
00338 }
00339
00340
00341
00342
00343
00344
00345
00346 protected function _getAttributeValue($attribute)
00347 {
00348 $entry = $this->getEntry();
00349 $attributeArr = $entry->getGbaseAttribute($attribute);
00350 if (is_array($attributeArr) && is_object($attributeArr[0])) {
00351 return $attributeArr[0]->getText();
00352 }
00353 return null;
00354 }
00355
00356
00357
00358
00359
00360
00361 protected function _getItemType()
00362 {
00363 return $this->getItemType() ? $this->getItemType() : $this->getConfig()->getDefaultItemType($this->getStoreId());
00364 }
00365
00366
00367
00368
00369
00370
00371 protected function _checkItem()
00372 {
00373 if (!($this->getItem() instanceof Mage_GoogleBase_Model_Item)) {
00374 Mage::throwException(Mage::helper('googlebase')->__('Item model is not specified to delete Google Base entry'));
00375 }
00376 }
00377
00378
00379
00380
00381
00382
00383
00384 protected function _normalizeString($string)
00385 {
00386 return preg_replace('/\s+/', '_', $string);
00387
00388
00389
00390
00391 }
00392
00393
00394
00395
00396
00397
00398
00399
00400 public function gBaseDate2DateTime($gBaseDate)
00401 {
00402 return Mage::getSingleton('core/date')->date(null, $gBaseDate);
00403 }
00404 }