public function parse()
 {
     $url = Mage::getStoreConfig('webtoprint/settings/url');
     $key = Mage::getStoreConfig('webtoprint/settings/key');
     //Always print debug information. Issue #80
     $this->debug = true;
     if ($url) {
         $this->notice("ZetaPrints URL: {$url}");
     } else {
         $this->error("ZetaPrints URL is empty");
     }
     if ($key) {
         $this->notice('ZetaPrints API Key: ' . substr($key, 0, 6) . '…');
     } else {
         $this->error("ZetaPrints API Key is empty");
     }
     $refresh_templates = (bool) Mage::getStoreConfig('webtoprint/settings/refresh-templates');
     if ($refresh_templates) {
         $this->warning('Refresh all templates');
     }
     $catalogs = zetaprints_get_list_of_catalogs($url, $key);
     if ($catalogs === null) {
         $this->error('Error in parsing catalogs detailes xml');
         return;
     } else {
         if (is_string($catalogs)) {
             $this->error("Error in receiving catalogs: {$catalogs}");
             return;
         }
     }
     if (!count($catalogs)) {
         $this->warning('No catalogs');
         return;
     }
     $total_number_of_catalogs = 0;
     $number_of_ignored_catalogs = 0;
     $total_number_of_templates = 0;
     $number_of_added_templates = 0;
     $number_of_uptodate_templates = 0;
     $number_of_updated_templates = 0;
     $number_of_removed_templates = 0;
     $number_of_ignored_templates = 0;
     $line = 0;
     $all_template_guids = array();
     foreach ($catalogs as $catalog) {
         $total_number_of_catalogs++;
         $templates = zetaprints_get_templates_from_catalog($url, $key, $catalog['guid']);
         if ($templates === null) {
             $this->error("Error in parsing templates detailes xml from catalog {$catalog['title']}");
             $number_of_ignored_catalogs++;
             continue;
         } else {
             if (is_string($templates)) {
                 $this->error("Error in receiving list of templates for catalog {$catalog['title']}: {$templates}");
                 $number_of_ignored_catalogs++;
                 continue;
             }
         }
         if (!count($templates)) {
             $this->warning("No templates in catalog {$catalog['title']}");
             $number_of_ignored_catalogs++;
             continue;
         }
         $total_number_of_templates += count($templates);
         foreach ($templates as $template) {
             $line++;
             $all_template_guids[$template['guid']] = $template['guid'];
             $template_array = zetaprints_get_template_detailes($url, $key, $template['guid']);
             if (!$template_array) {
                 $this->error("{$line}. Error in receiving or parsing detailes of template {$template['guid']}. Leaving the template unmodified.");
                 continue;
             }
             $has_fields = false;
             foreach ($template_array['pages'] as $page) {
                 if (isset($page['images']) || isset($page['fields'])) {
                     $has_fields = true;
                     break;
                 }
             }
             $template['public'] = $catalog['public'];
             $templates_collection = Mage::getModel('webtoprint/template')->getCollection()->get_by_guid($template['guid'])->load();
             if ($templates_collection->getSize() == 1) {
                 foreach ($templates_collection as $template_model) {
                     if (!$has_fields) {
                         $template_model->setExist(false)->save();
                         $number_of_removed_templates++;
                         $this->warning("{$line}. Template {$template['guid']} has no variable fields. Template will be deleted.");
                         continue;
                     }
                     if (strtotime($template['date']) > strtotime($template_model->getDate()) || $refresh_templates || (int) $template_model->getPublic() != $template['public']) {
                         $this->debug("{$line}. Template {$template['guid']} is outdated");
                         $template['xml'] = zetaprints_get_template_details_as_xml($url, $key, $template['guid']);
                         if (!$template['xml']) {
                             $this->error("{$line}. Error in receiving detailes for template {$template['guid']}. Leaving the template unmodified.");
                             continue;
                         }
                         $template_model->addData($template)->save();
                         $number_of_updated_templates++;
                     } else {
                         $number_of_uptodate_templates++;
                         $this->debug("{$line}. Template {$template['guid']} is up to date");
                     }
                 }
             } else {
                 if (!$has_fields) {
                     $this->warning("{$line}. Template {$template['guid']} has no variable fields. Ignored.");
                     $number_of_ignored_templates++;
                     continue;
                 }
                 $template['xml'] = zetaprints_get_template_details_as_xml($url, $key, $template['guid']);
                 if (!$template['xml']) {
                     $this->error("{$line}. Error in receiving detailes for template {$template['guid']}. Passing the template.");
                     $number_of_ignored_templates++;
                     continue;
                 }
                 $template_model = Mage::getModel('webtoprint/template');
                 $template_model->addData($template)->save();
                 if ($template_model->getId()) {
                     $number_of_added_templates++;
                     $this->notice("{$line}. Template {$template['guid']} is succesfully added.");
                 }
             }
         }
     }
     $templates_collection = Mage::getModel('webtoprint/template')->getCollection()->load();
     foreach ($templates_collection as $template) {
         if (!isset($all_template_guids[$template->getGuid()])) {
             $number_of_removed_templates += 1;
             $template->setExist(false)->save();
         }
     }
     $this->notice("Total number of catalogs: {$total_number_of_catalogs}");
     $this->notice("Number of ignored catalogs: {$number_of_ignored_catalogs}");
     $this->notice("Total number of templates: {$total_number_of_templates}");
     $this->notice("Number of added templates: {$number_of_added_templates}");
     $this->notice("Number of up to date templates: {$number_of_uptodate_templates}");
     $this->notice("Number of updated templates: {$number_of_updated_templates}");
     $this->notice("Number of ignored templates: {$number_of_ignored_templates}");
     $this->notice("Number of removed templates: {$number_of_removed_templates}");
     if ($number_of_added_templates > 0) {
         $this->notice('Run ZetaPrints product creation profile to create products for new templates automatically. Read <a href="http://www.zetaprints.com/magentohelp/">www.zetaprints.com/magentohelp/</a> for more info on product creation.');
     }
 }
 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;
 }