예제 #1
0
파일: Gbase.php 프로젝트: natureday1/Life
/**
 * Inserts a new recipe by performing an HTTP POST to the
 * items feed.
 * @param boolean $dryRun (optional) True if this should be a dry run insert
 * @return Zend_Gdata_Gbase_ItemFeed The newly created entry
 */
function postItem($dryRun = false)
{
    $client = Zend_Gdata_AuthSub::getHttpClient($_POST['token']);
    $gdata = new Zend_Gdata_Gbase($client);
    $newEntry = $gdata->newItemEntry();
    // Add title
    $newEntry->title = $gdata->newTitle(trim($_POST['recipe_title']));
    // Add some content
    $newEntry->content = $gdata->newContent($_POST['recipe_text']);
    $newEntry->content->type = 'text';
    // Define item type
    $newEntry->itemType = 'testrecipes';
    $newEntry->itemType->type = 'text';
    // Add item-specific attributes
    $newEntry->addGbaseAttribute('cuisine', $_POST['cuisine'], 'text');
    $newEntry->addGbaseAttribute('cooking_time', $_POST['time_val'] . ' ' . $_POST['time_units'], 'intUnit');
    $newEntry->addGbaseAttribute('main_ingredient', $_POST['main_ingredient'], 'text');
    $newEntry->addGbaseAttribute('serving_count', $_POST['serves'], 'number');
    // Post the item
    $createdEntry = $gdata->insertGbaseItem($newEntry, $dryRun);
    return $createdEntry;
}
예제 #2
0
/** 
 * Inserts a Base item into the Customer Items feed
 *
 * @param Zend_Http_Client $client The authenticated client object
 * @return string The URL of the newly created entry
 */
function insertItem($client, $dryRun = false)
{
    echo "<h2>Insert an item</h2>\n";
    $service = new Zend_Gdata_Gbase($client);
    $newEntry = $service->newItemEntry();
    // Add title
    $title = "PHP Developer Handbook";
    $newEntry->title = $service->newTitle(trim($title));
    // Add some content
    $content = "Essential handbook for PHP developers. This is a test item.";
    $newEntry->content = $service->newContent($content);
    $newEntry->content->type = 'text';
    // Define product type
    $itemType = "Products";
    $newEntry->itemType = $itemType;
    $newEntry->itemType->type = 'text';
    // Add item-specific attributes
    $newEntry->addGbaseAttribute('product_type', 'book', 'text');
    $newEntry->addGbaseAttribute('price', '12.99 USD', 'floatUnit');
    $newEntry->addGbaseAttribute('quantity', '10', 'int');
    $newEntry->addGbaseAttribute('weight', '2.2 lbs', 'numberUnit');
    $newEntry->addGbaseAttribute('condition', 'New', 'text');
    $newEntry->addGbaseAttribute('UPC', 'UPC12345', 'text');
    $newEntry->addGbaseAttribute('ISBN', 'ISBN12345', 'text');
    $newEntry->addGbaseAttribute('author', 'John Doe', 'text');
    $newEntry->addGbaseAttribute('edition', 'First Edition', 'text');
    $newEntry->addGbaseAttribute('pages', '253', 'number');
    $newEntry->addGbaseAttribute('publisher', 'My Press', 'text');
    $newEntry->addGbaseAttribute('year', '2007', 'number');
    $newEntry->addGbaseAttribute('label', 'PHP 4', 'text');
    $newEntry->addGbaseAttribute('label', 'development', 'text');
    /**
     * If you'd like to use Google Base-hosted item pages and accept Google Checkout, 
     * simply uncomment the following statement to make Google Checkout as a payment option.
     * Don't include the link attribute. If you do, your listing will direct 
     * buyers to your website instead of a Google Base-hosted page that displays the 
     * Google Checkout Buy button.
     *   Note: Google Checkout can be used for products only:
     *         <g:item_type>Products</g:item_type>
     *         Also, make sure that 'price' and 'quantity' attributes are defined.
     */
    $newEntry->addGbaseAttribute('payment_accepted', 'Google Checkout', 'text');
    /**
     * If you're already using Google Checkout on your website, include 
     * 'Google Checkout' as a payment option by including payment_notes attribute.
     * In this case, you should include the link attribute, as users will be 
     * directed to your Google Checkout-integrated website. 
     */
    //$newEntry->addGbaseAttribute('payment_notes', 'Google Checkout', 'text');
    //$link = 'http://www.mysite.com/products/item123.php';
    //$newEntry->link = $service->newLink($link);
    //$newEntry->link->type = 'text/html';
    // Post the item
    $createdEntry = $service->insertGbaseItem($newEntry, $dryRun);
    $itemUrl = $createdEntry->id->text;
    echo "\t<ol><li><b>" . $createdEntry->title->text . "</b><br />\n";
    echo "\t<span>" . $createdEntry->id->text . "</span><br />\n";
    echo "\t<p>" . $createdEntry->content->text . "</p></li></ol>\n";
    echo "\tSuccessfully inserted an item at " . $itemUrl . "<br /><br />\n";
    return $itemUrl;
}
예제 #3
0
 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());
         }
     }
 }