public function byUrlAction()
 {
     $request = $this->getRequest();
     if (!($request->has('url') && ($url = $request->get('url')))) {
         return;
     }
     $helper = Mage::helper('webtoprint');
     $credentials = $helper->get_zetaprints_credentials();
     $params = array('ID' => $credentials['id'], 'Hash' => zetaprints_generate_user_password_hash($credentials['password']), 'URL' => $url);
     $url = Mage::getStoreConfig('webtoprint/settings/url');
     $key = Mage::getStoreConfig('webtoprint/settings/key');
     $image = zetaprints_download_customer_image($url, $key, $params);
     if (is_array($image) && count($image) == 1) {
         $image = $image[0];
     } else {
         echo 'Error';
         return;
     }
     if ($image['mime'] === 'image/jpeg' || $image['mime'] === 'image/jpg') {
         $image['thumbnail_url'] = $helper->get_photo_thumbnail_url($image['thumbnail'], 0, 100);
     } else {
         $image['thumbnail_url'] = $helper->get_photo_thumbnail_url($image['thumbnail']);
     }
     echo json_encode($image);
 }
 public function updateAction()
 {
     $params = array();
     //Preparing params for image generating request to zetaprints
     foreach ($this->getRequest()->getParams() as $key => $value) {
         if (strpos($key, 'zetaprints-') !== false) {
             $_key = substr($key, 11);
             $_key = substr($_key, 0, 1) . str_replace('_', ' ', substr($_key, 1));
             $params[$_key] = str_replace("\n", "\r\n", $value);
         }
     }
     if (count($params) == 0) {
         return;
     }
     $user_credentials = Mage::helper('webtoprint')->get_zetaprints_credentials();
     $params['ID'] = $user_credentials['id'];
     $params['Hash'] = zetaprints_generate_user_password_hash($user_credentials['password']);
     $url = Mage::getStoreConfig('webtoprint/settings/url');
     $key = Mage::getStoreConfig('webtoprint/settings/key');
     echo zetaprints_get_edited_image_url($url, $key, $params);
 }
 public function create_zetaprints_order($observer)
 {
     $update_mode = $observer->getEvent()->hasQuoteItem() ? false : true;
     $quote_item = $update_mode ? $observer->getEvent()->getItem() : $observer->getEvent()->getQuoteItem();
     if ($quote_item->getParentItem()) {
         $quote_item = $quote_item->getParentItem();
     }
     $option_model = $quote_item->getOptionByCode('info_buyRequest');
     $options = unserialize($option_model->getValue());
     //_zetaprints_debug(array('orig options' => $options));
     //Check if quote item is w2p enabled
     if (!isset($options['zetaprints-TemplateID'])) {
         return;
     }
     if (!isset($options['zetaprints-previews']) || !$options['zetaprints-previews']) {
         Mage::getSingleton('checkout/session')->addNotice(Mage::helper('webtoprint')->__('The product was added in fallback mode. We will update it manually with your input data.'));
         return;
     }
     //Use saved order information from the item for M. re-order...
     if (isset($options['zetaprints-order-id'])) {
         //... and mark the item as re-ordered
         $options['zetaprints-reordered'] = true;
         $option_model->setValue(serialize($options));
         return;
     }
     $dynamicImaging = false;
     if (!($dynamicImaging = $quote_item->getProduct()->getDynamicImaging())) {
         foreach ($quote_item->getProduct()->getCategoryIds() as $categoryId) {
             $category = Mage::getModel('catalog/category')->load($categoryId);
             if ($category->getId() && $category->getDynamicImaging()) {
                 $dynamicImaging = true;
                 break;
             }
         }
     }
     $options['zetaprints-dynamic-imaging'] = $dynamicImaging;
     if (!$dynamicImaging) {
         $params = array();
         $params['TemplateID'] = $options['zetaprints-TemplateID'];
         $params['Previews'] = $options['zetaprints-previews'];
         $user_credentials = Mage::helper('webtoprint')->get_zetaprints_credentials();
         $params['ID'] = $user_credentials['id'];
         $params['Hash'] = zetaprints_generate_user_password_hash($user_credentials['password']);
         $url = Mage::getStoreConfig('webtoprint/settings/url');
         $key = Mage::getStoreConfig('webtoprint/settings/key');
         $order_details = zetaprints_create_order($url, $key, $params);
         if (!$order_details) {
             Mage::throwException('ZetaPrints error');
         }
         //We have to show all previews (for dynamic and static pages) on
         //shopping card and order details, so save preview file names for all pages.
         $previews = '';
         foreach ($order_details['template-details']['pages'] as $page) {
             if (isset($page['updated-preview-image'])) {
                 $previews .= ',' . substr($page['updated-preview-image'], 8);
             } else {
                 if ($order_details['template-details']['missed_pages']) {
                     $previews .= ',' . substr($page['preview-image'], 8);
                 }
             }
         }
         $options['zetaprints-previews'] = substr($previews, 1);
         //Save order GUID in the item options
         $options['zetaprints-order-id'] = $order_details['guid'];
         //If order details contain link to low resolution PDF...
         if ($order_details['pdf'] != '') {
             //... save it in the item options
             $options['zetaprints-order-lowres-pdf'] = $order_details['pdf'];
         }
         //_zetaprints_debug(array('new options' => $options));
     }
     $option_model->setValue(serialize($options));
     if ($update_mode) {
         $option_model->save();
     }
     Mage::getSingleton('core/session')->unsetData('zetaprints-previews');
 }
    public function get_order_webtoprint_links($context, $item = null)
    {
        $isAdmin = false;
        if (!$item) {
            $item = $context->getItem();
            $isAdmin = true;
        }
        $options = $item->getProductOptionByCode('info_buyRequest');
        //Check for ZetaPrints Template ID in item options
        //If it doesn't exist or product doesn't have web-to-print features then...
        if (!isset($options['zetaprints-TemplateID'])) {
            //... just return from the function.
            return;
        }
        $isOrderComplete = isset($options['zetaprints-order-completed']) && $options['zetaprints-order-completed'];
        if ($isAdmin && !$isOrderComplete) {
            $url = Mage::helper('adminhtml')->getUrl('web-to-print-admin/order/complete', array('item' => $item->getId()));
            $title = $this->__('Complete order on ZetaPrints');
            echo '<br />' . "<a id=\"zp-complete-order-link\" href=\"{$url}\">{$title}</a>";
        }
        if ($isAdmin && isset($options['zetaprints-previews']) && !$options['zetaprints-previews']) {
            $input = array();
            foreach ($options as $key => $value) {
                //Ignore key if it doesn't start with 'zetaprints-' prefix
                if (strpos($key, 'zetaprints-') !== 0) {
                    continue;
                }
                //Remove prefix from the key
                $_key = substr($key, 11);
                if (!(strpos($_key, '_') === 0 || strpos($_key, '#') === 0 || strpos($_key, '*') === 0)) {
                    continue;
                }
                if (strpos($_key, '#') === 0) {
                    if (!($details = $context->getTemplateDetails())) {
                        $details = $this->getTemplateDetailsByGUID($options['zetaprints-TemplateID']);
                        if ($details) {
                            $context->setTemplateDetails($details);
                        }
                    }
                    if ($details) {
                        if (!($stockImages = $context->getStockImages())) {
                            $stockImages = array();
                            foreach ($details['pages'] as $page) {
                                foreach ($page['images'] as $imageField) {
                                    if (isset($imageField['stock-images'])) {
                                        foreach ($imageField['stock-images'] as $image) {
                                            if (!isset($stockImages[$image['guid']])) {
                                                $tokens = explode('.', $image['thumb']);
                                                $stockImages[$image['guid']] = array('thumb' => $image['thumb'], 'small-thumb' => $tokens[0] . '_0x100.' . $tokens[1]);
                                            }
                                        }
                                    }
                                }
                            }
                            $context->setStockImages($stockImages);
                        }
                        if (isset($stockImages[$value])) {
                            $url = Mage::getStoreConfig('webtoprint/settings/url') . 'photothumbs/' . $stockImages[$value]['thumb'];
                            $small_url = Mage::getStoreConfig('webtoprint/settings/url') . 'photothumbs/' . $stockImages[$value]['small-thumb'];
                            $value = "<a href=\"{$url}\" target=\"_blank\">" . "<image src=\"{$small_url}\" />" . "</a>";
                        } else {
                            if (!($userImages = $context->getUserImages())) {
                                $customer = Mage::getModel('customer/customer')->load($item->getOrder()->getCustomerId());
                                $userImages = array();
                                if ($customer->getId()) {
                                    $url = Mage::getStoreConfig('webtoprint/settings/url');
                                    $key = Mage::getStoreConfig('webtoprint/settings/key');
                                    $data = array('ID' => $customer->getZetaprintsUser(), 'Hash' => zetaprints_generate_user_password_hash($customer->getZetaprintsPassword()));
                                    $userImages = zetaprints_get_user_images($url, $key, $data);
                                    $context->setUserImages($userImages);
                                }
                            }
                            if (isset($userImages[$value])) {
                                $url = Mage::getStoreConfig('webtoprint/settings/url') . 'photothumbs/' . $userImages[$value]['thumbnail'];
                                $tokens = explode('.', $userImages[$value]['thumbnail']);
                                $small_url = Mage::getStoreConfig('webtoprint/settings/url') . 'photothumbs/' . $tokens[0] . '_0x100.' . $tokens[1];
                                $value = "<a href=\"{$url}\" target=\"_blank\">" . "<image src=\"{$small_url}\" />" . "</a>";
                            }
                        }
                    }
                    if ($value === '#') {
                        $value = $this->__('Default');
                    }
                    if ($value === '') {
                        $value = $this->__('Blank');
                    }
                } else {
                    $value = "<pre>{$value}</pre>";
                }
                //Determine length of field prefix
                $prefix_length = 0;
                if (strpos($_key, '*') === 0) {
                    $prefix_length = 1;
                }
                //Process field name (key), restore original symbols
                $_key = substr($_key, 0, $prefix_length) . str_replace(array('_', "\n"), array(' ', '.'), substr($_key, $prefix_length + 1));
                //Add token to the array
                $input[$_key] = $value;
            }
            if (count($input)) {
                $product = Mage::getModel('catalog/product')->load($options['product']);
                if ($product->getId()) {
                    $productUrl = $product->getProductUrl();
                    $productName = $product->getName();
                }
                ?>
        <div style="display: none;">
          <div id ="zp-user-input-table">

            <?php 
                if (isset($productUrl)) {
                    ?>
              <a href="<?php 
                    echo $productUrl;
                    ?>
">
                <?php 
                    echo $productName;
                    ?>
              </a>
            <?php 
                }
                ?>

            <table id ="zp-user-input-table">
              <thead>
                <tr>
                  <th><?php 
                echo $this->__('Name');
                ?>
</th>
                  <th><?php 
                echo $this->__('Value');
                ?>
</th>
                </tr>
              </thead>
              <tbody>
                <?php 
                foreach ($input as $name => $value) {
                    ?>
                <tr>
                  <td><?php 
                    echo $name;
                    ?>
</td>
                  <td><?php 
                    echo $value;
                    ?>
</td>
                </tr>
                <?php 
                }
                ?>
              </tbody>
            </table>
          </div>
        </div>

        <br />

        <a id ="zp-user-input-link" href="#zp-user-input-table">
          <?php 
                echo $this->__('Show customer\'s input data');
                ?>
        </a>
<?php 
            }
        }
        //Get value of custom option which allows users download files
        //regardless of ZP template setting
        $is_user_allowed_download = Mage::helper('webtoprint')->getCustomOptions('file-download/users@allow=1');
        //Check that downloading generated files is allowed for users
        if (!$isAdmin && !$is_user_allowed_download) {
            $template = Mage::getModel('webtoprint/template')->load($options['zetaprints-TemplateID']);
            if (!$template->getId()) {
                return;
            }
            try {
                $xml = new SimpleXMLElement($template->getXml());
            } catch (Exception $e) {
                Mage::log("Exception: {$e->getMessage()}");
                return;
            }
            if (!$xml) {
                return;
            }
            $template_details = zetaprints_parse_template_details($xml);
            if (!$template_details['download']) {
                return;
            }
        }
        $webtoprint_links = '<br />';
        $types = array('pdf', 'gif', 'png', 'jpeg');
        //If function called from admin template
        if ($isAdmin) {
            //then add CDR file type to list of available types
            array_push($types, 'cdr');
        }
        foreach ($types as $type) {
            if (isset($options['zetaprints-file-' . $type])) {
                $title = strtoupper($type);
                $webtoprint_links .= "<a class=\"zetaprints-order-file-link {$type}\" href=\"{$options['zetaprints-file-' . $type]}\" target=\"_blank\">{$title}</a>&nbsp;";
            }
        }
        //Check if the item is not null (it means the function was called from admin
        //interface) and ZetaPrints Order ID option is in the item then...
        if ($isAdmin && isset($options['zetaprints-order-id'])) {
            //... create URL to order details on web-to-print site
            $zp_order_url = Mage::getStoreConfig('webtoprint/settings/url') . '?page=order-details;OrderID=' . $options['zetaprints-order-id'];
            //Display it on the page
            $webtoprint_links .= " <a target=\"_blank\" href=\"{$zp_order_url}\">ZP order</a>";
        }
        return $webtoprint_links;
    }
Beispiel #5
0
 public function getTemplateXmlForCurrentProduct()
 {
     if (!($xml = Mage::registry('webtoprint-template-xml'))) {
         if (!($product = Mage::registry('product'))) {
             return;
         }
         if (!$product->getId()) {
             return;
         }
         if (!($templateGuid = $this->getTemplateGuidFromProduct($product))) {
             return;
         }
         //This flag shows a status of web-to-print user registration
         $user_was_registered = true;
         //Check a status of web-to-print user registration on ZetaPrints
         //and if it's not then set user_was_registered flag to false
         if (!($user_credentials = $this->get_zetaprints_credentials())) {
             $template = Mage::getModel('webtoprint/template')->load($templateGuid);
             if ($template->getId()) {
                 $user_was_registered = false;
             }
         }
         //Remember a status of web-to-print user registrarion for subsequent
         //function calls
         Mage::register('webtoprint-user-was-registered', $user_was_registered);
         if ($user_was_registered) {
             $url = Mage::getStoreConfig('webtoprint/settings/url');
             $key = Mage::getStoreConfig('webtoprint/settings/key');
             $data = array('ID' => $user_credentials['id'], 'Hash' => zetaprints_generate_user_password_hash($user_credentials['password']));
             if ($product->getConfigureMode() && ($orderId = Mage::registry('webtoprint-order-id'))) {
                 $data['OrderID'] = $orderId;
             }
             $template_xml = zetaprints_get_template_details_as_xml($url, $key, $templateGuid, $data);
             //!!! Load XML for the template from DB
             //    if loading from ZP was unsuccessful
         } else {
             $template_xml = $template->getXml();
         }
         try {
             $xml = new SimpleXMLElement($template_xml);
         } catch (Exception $e) {
             Mage::log("Exception: {$e->getMessage()}");
             return false;
         }
         $session = Mage::getSingleton('core/session');
         if ($session->hasData('zetaprints-previews')) {
             $previews = unserialize($session->getData('zetaprints-previews'));
             if (is_array($previews) && !$this->replacePreviewImages($xml, $previews)) {
                 $session->setData('zetaprints-previews', '');
             }
         }
         $this->generateImageUrls($xml);
         $request = $this->_getRequest();
         //If product page was requested with reorder parameter...
         if ($request->has('reorder') && strlen($request->getParam('reorder')) == 36) {
             //...then replace field values from order details
             $this->replace_user_input_from_order_details($xml, $this->_getRequest()->getParam('reorder'));
         } else {
             if ($request->has('for-item')) {
                 //...then replace various template values from item's options
                 $this->replace_template_values_from_cart_item($xml, $request->getParam('for-item'));
             } else {
                 if ($product->getConfigureMode()) {
                     if ($item = Mage::registry('wishlist_item')) {
                         $xml = $this->updateTemplate($xml, $this->extractUserInput($item->getBuyRequest()));
                     }
                 }
             }
         }
         Mage::register('webtoprint-template-xml', $xml);
     }
     return $xml;
 }
 public function indexAction()
 {
     $params = array();
     //Preparing params for image generating request to zetaprints
     foreach ($this->getRequest()->getParams() as $key => $value) {
         //Ignore key if it doesn't start with 'zetaprints-' prefix
         if (strpos($key, 'zetaprints-') !== 0) {
             continue;
         }
         //Remove prefix from the key
         $_key = substr($key, 11);
         //Text and image template fields distinguish by prefix in its name
         //Prefix for text fields is '_' sign, for image fields is '#' sign.
         //Metadata fields for text and image fields prepends prefix
         //with '*' sign, i.e. '*_' for text fields and '*#' for image fields.
         //So POST fields have 1- or 2-letter prefixes.
         //Determine length of field prefix
         $prefix_length = 1;
         if (strpos($_key, '*') === 0) {
             $prefix_length = 2;
         }
         //Process field name (key), restore original symbols
         $_key = substr($_key, 0, $prefix_length) . str_replace(array('_', "\n"), array(' ', '.'), substr($_key, $prefix_length));
         //Add token to the array
         $params[$_key] = $value;
     }
     if (count($params) == 0) {
         return;
     }
     //$session = Mage::getSingleton('customer/session');
     //$text_cache = $session->getTextFieldsCache();
     //if (!$text_cache)
     //  $text_cache = array();
     //$image_cache = $session->getImageFieldsCache();
     //if (!$image_cache)
     //  $image_cache = array();
     //foreach ($params as $key => $value)
     //  if (strpos($key, '_') !== false) {
     //    $_key = substr($key, 1);
     //    if (array_key_exists($_key, $text_cache))
     //      unset($text_cache[$_key]);
     //    if ($value)
     //      $text_cache[$_key] = $value;
     //    if ($length = count($text_cache) > 150)
     //      $text_cache = array_slice($text_cache, $length - 150);
     //  } elseif (strpos($key, '#') !== false) {
     //    $_key = substr($key, 1);
     //    if (array_key_exists($_key, $image_cache))
     //      unset($image_cache[$_key]);
     //    if ($value)
     //      $image_cache[$_key] = $value;
     //    if ($length = count($image_cache) > 50)
     //      $image_cache = array_slice($image_cache, $length - 50);
     //  }
     //$session->setTextFieldsCache($text_cache);
     //$session->setImageFieldsCache($image_cache);
     //reset($params);
     $user_credentials = Mage::helper('webtoprint')->get_zetaprints_credentials();
     $params['ID'] = $user_credentials['id'];
     $params['Hash'] = zetaprints_generate_user_password_hash($user_credentials['password']);
     $url = Mage::getStoreConfig('webtoprint/settings/url');
     $key = Mage::getStoreConfig('webtoprint/settings/key');
     $templates_details = zetaprints_update_preview($url, $key, $params);
     if (!$templates_details) {
         return;
     }
     $helper = Mage::helper('webtoprint');
     //Generate URLs for preview and thumbnail images
     foreach ($templates_details['pages'] as &$page) {
         if (isset($page['updated-preview-image'])) {
             $page['updated-preview-url'] = $helper->get_preview_url(substr($page['updated-preview-image'], 8));
         }
         if (isset($page['updated-thumb-image'])) {
             $page['updated-thumb-url'] = $helper->get_thumbnail_url(substr($page['updated-thumb-image'], 6), 100, 100);
         }
     }
     echo json_encode($templates_details);
 }