/** * Updates an existing recipe by performing an HTTP PUT * on its feed URI, using the updated values as the data. * @return true */ function updateItem() { $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']); $gdata = new Zend_Gdata_Gbase($client); $itemUrl = $_POST['link']; $updatedEntry = $gdata->getGbaseItemEntry($itemUrl); // Update title $updatedEntry->title = $gdata->newTitle(trim($_POST['recipe_title'])); // Update content $updatedEntry->content = $gdata->newContent($_POST['recipe_text']); $updatedEntry->content->type = 'text'; // Update item-specific attributes $baseAttributeArr = $updatedEntry->getGbaseAttribute('cuisine'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['cuisine']; } $baseAttributeArr = $updatedEntry->getGbaseAttribute('cooking_time'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['time_val'] . ' ' . $_POST['time_units']; } $baseAttributeArr = $updatedEntry->getGbaseAttribute('main_ingredient'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['main_ingredient']; } $baseAttributeArr = $updatedEntry->getGbaseAttribute('serving_count'); if (is_object($baseAttributeArr[0])) { $baseAttributeArr[0]->text = $_POST['serves']; } $dryRun = false; $gdata->updateGbaseItem($updatedEntry, $dryRun); // Alternatively, you can call the save() method directly on the entry // $updatedEntry->save(); return true; }
/** * Updates a Base item entry. It demonstrates how to access and * update/remove Base attributes * * @param Zend_Http_Client $client The authenticated client object * @return void */ function updateItem($client, $itemUrl, $dryRun = false) { echo "<h2>Update my item</h2>\n"; $service = new Zend_Gdata_Gbase($client); if ($entry = $service->getGbaseItemEntry($itemUrl)) { echo "\t<ol>\n"; // Update the title $oldTitle = $entry->title->text; $newTitle = 'PHP Developer Handbook Second Edition'; $entry->title = $service->newTitle($newTitle); // Find <g:price> attribute and update the price $baseAttributeArr = $entry->getGbaseAttribute('price'); if (is_object($baseAttributeArr[0])) { $oldPrice = $baseAttributeArr[0]->text; $newPrice = '16.99 USD'; $baseAttributeArr[0]->text = $newPrice; } // Find <g:edition> attribute and update the edition $baseAttributeArr = $entry->getGbaseAttribute('edition'); if (is_object($baseAttributeArr[0])) { $oldEdition = $baseAttributeArr[0]->text; $newEdition = 'Second Edition'; $baseAttributeArr[0]->text = $newEdition; } // Find <g:pages> attribute and update the number of pages $baseAttributeArr = $entry->getGbaseAttribute('pages'); if (is_object($baseAttributeArr[0])) { $oldPages = $baseAttributeArr[0]->text; $newPages = '278'; $baseAttributeArr[0]->text = $newPages; // Update the attribute type from 'number' to 'int' if ($baseAttributeArr[0]->type == 'number') { $newType = 'int'; $baseAttributeArr[0]->type = $newType; } } // Remove <g:label> attributes and add new attributes $baseAttributeArr = $entry->getGbaseAttribute('label'); foreach ($baseAttributeArr as $note) { $entry->removeGbaseAttribute($note); } $entry->addGbaseAttribute('note', 'PHP 5', 'text'); $entry->addGbaseAttribute('note', 'Web Programming', 'text'); try { $entry->save($dryRun); echo "\t<li><b>" . $entry->title->text . "</b><br />\n"; echo "\t<span>" . $entry->id->text . "</span><br />\n"; echo "\t<p>" . $entry->content->text . "</p></li>\n"; echo "\tSuccessfully updated entry at " . $entry->id->text . "</li><br />\n"; } catch (Zend_Gdata_App_Exception $e) { echo "<div class='error'>ERROR:</div><br />\n"; var_dump($e); return null; } echo "\t</ol>\n"; } else { echo "\tNo item exists at " . $itemUrl . "<br />\n"; return null; } }
private function _insert($productId, &$params) { $dryRun = Axis::config()->gbase->main->dryRun; $tableLanguage = Axis::single('locale/language'); $currentLanguage = $tableLanguage->find($params['language'])->current(); /* Get local product data */ $tableProduct = Axis::single('catalog/product'); $tableManufacture = Axis::single('catalog/product_manufacturer'); $currencyModel = Axis::single('locale/currency'); $zendCurrency = $currencyModel->getCurrency($params['currency']); $rate = $currencyModel->getData($params['currency'], 'rate'); $currencyOptions = array('position' => Zend_Currency::RIGHT, 'display' => Zend_Currency::USE_SHORTNAME); $product = $tableProduct->find($productId)->current(); $productUrl = $product->getHumanUrl(); $productDescription = $product->getDescription($params['language']); if ($product->manufacturer_id != '0') { $productManufacture = $tableManufacture->find($product->manufacturer_id)->current(); } $productCategories = $product->getCategories($params['language']); $entry = $this->_service->newItemEntry(); /* Atom namespace */ $entry->title = $this->_service->newTitle(trim($productDescription['name'])); $content = $productDescription['description'] == '' ? Axis::translate('catalog')->__('No description available') : trim($productDescription['description']); $entry->content = $this->_service->newContent($content); $entry->content->type = 'text'; $entry->itemType = Axis::config()->gbase->main->itemType; if (Axis::config()->gbase->main->link == 'Website' && ($siteUrl = $this->_getSiteUrl($params['site']))) { $link = new Zend_Gdata_App_Extension_Link(); $link->setHref(preg_replace('/\\s+/', '%20', trim($siteUrl . '/' . $this->view->catalogUrl . '/' . $productUrl))); $link->setRel('alternate'); $link->setType('text/html'); $link->setTitle(trim($productDescription['name'])); $linkArray[] = $link; $entry->setLink($linkArray); } else { $entry->addGbaseAttribute('payment_accepted', 'Google Checkout', 'text'); } /* G namespace */ //$price = $zendCurrency->toCurrency($product->price * $rate, $currencyOptions); $price = $product->price * $rate; $entry->addGbaseAttribute('price', $price, 'floatUnit'); $entry->addGbaseAttribute('condition', 'new', 'text'); $entry->addGbaseAttribute('id', $product->sku . '_' . $params['country'], 'text'); $entry->addGbaseAttribute('quantity', (int) $product->quantity, 'int'); $entry->addGbaseAttribute('weight', "{$product->weight} lbs", 'numberUnit'); //@TODO get weight unit if ($productManufacture) { $entry->addGbaseAttribute('brand', $productManufacture->name, 'text'); } $entry->addGbaseAttribute('target_country', $params['country'], 'text'); $entry->addGbaseAttribute('item_language', $currentLanguage->code, 'text'); if ($siteUrl) { $entry->addGbaseAttribute('image_link', preg_replace('/\\s+/', '%20', trim($siteUrl . '/media' . '/product' . $product->image_base)), 'url'); } foreach ($productCategories as $category) { $entry->addGbaseAttribute('product_type', $category['name'], 'text'); } foreach (Axis::config()->gbase->main->payment as $payment) { if ($payment != '') { $entry->addGbaseAttribute('payment', $payment, 'text'); } } if (Axis::config()->gbase->main->notes != '') { $entry->addGbaseAttribute('payment_notes', Axis::config()->gbase->main->notes, 'text'); } if (Axis::config()->gbase->main->application != '') { $entry->addGbaseAttribute('application', Axis::config()->gbase->main->application, 'text'); } $attributes = $this->_getProductAttributes($product, $params['language']); foreach ($attributes as $attr_id => $attribute) { $attrName = $attribute['optionName']; $attrValue = isset($attribute['valueName']) ? $attribute['valueName'] : $attribute['values']; $entry->addGbaseAttribute(preg_replace('/\\s+/', '_', $attrName), $attrValue, 'text'); } /* Private attributes */ $entry->addGbaseAttribute('site', $params['site'], 'int'); $array = $entry->getExtensionElements(); $privateAttr = array(); $privateAttr[0]['name'] = 'access'; $privateAttr[0]['value'] = 'private'; $privateAttr[1]['name'] = 'type'; $privateAttr[1]['value'] = 'int'; $localId = new Zend_Gdata_App_Extension_Element('local_id', 'g', 'http://base.google.com/ns/1.0', $product->id); $localId->setExtensionAttributes($privateAttr); $array[count($array)] = $localId; $entry->setExtensionElements($array); Axis::session('gbase')->last_exported = $productId; Axis::session('gbase')->processed_count++; try { $this->_service->insertGbaseItem($entry, $dryRun); Axis::session('gbase')->imported_count++; } catch (Exception $e) { Axis::message()->addError($this->view->escape($e->getMessage())); try { $log = new Zend_Log(new Zend_Log_Writer_Stream(Axis::config()->system->path . '/var/logs/gbase.log')); $log->err($e->getMessage()); } catch (Exception $e) { Axis::message()->addError($e->getMessage()); } } }