public function ajaxAction()
 {
     $product_id = $this->getRequest()->getParam('id');
     $productsModel = new Model_DbTable_Products();
     $product = $productsModel->find($product_id)->current();
     $subproductsModel = new Model_DbTable_Subproducts();
     $select = $subproductsModel->select()->order('order ASC');
     $subProducts = $product->findDependentRowset("Model_DbTable_Subproducts", 'SubproductsRel', $select);
     if (!empty($subProducts)) {
         $this->view->modifications = $subProducts;
         $subProductParams = $product->getSubParams();
         if (!empty($subProductParams)) {
             $headTable = array();
             $headTable[] = 'Название';
             foreach ($subProductParams as $subProductParam) {
                 $headTable[] = $subProductParam->name;
             }
             $this->view->tableHead = $headTable;
         }
         $tableBody = array();
         foreach ($subProducts as $modification) {
             $tableBody[] = $modification->sku;
             $paramsVavues = $modification->getParamsValues();
             foreach ($paramsVavues as $values) {
                 $tableBody['params'][] = $values->value;
             }
         }
     }
     $this->view->product = $product;
 }
 public function byProduct($product, $for_catalog = false)
 {
     $category = $product->findManyToManyRowset("Model_DbTable_Categories", "Model_DbTable_CategoryXref")->current();
     if ($category) {
         $this->category = $category->getBcnName();
     } else {
         $this->category = "Неизвестно";
     }
     $productParams = $product->getParams();
     if (!$for_catalog) {
         $this->margins = array('top' => 35, 'right' => 20, 'bottom' => 35, 'left' => 20);
     }
     if (is_null($this->book)) {
         $page = $this->createBook(1, $for_catalog);
     } else {
         $page = $this->lastPage();
     }
     $offset = $for_catalog == false ? 0 : $page->getHeight() - $page->getCurrentPosition() - 1;
     if (!$for_catalog) {
         $page->setMargins(35, 20, 35, 20);
     }
     $noteStyle = new Zend_Pdf_Style();
     $noteStyle->setFont(Model_Static_Fonts::get("Arial Narrow"), $this->format == 'A4' ? 6.5 : 4.5);
     $noteStyle->setLineWidth($page->getWidth() - 20);
     $style = new Zend_Pdf_Style();
     $style->setFont(Model_Static_Fonts::get("Arial Narrow"), $this->format == 'A4' ? 8.0 : 6.5);
     $style->setLineWidth($page->getWidth());
     $paramsLinesCount = 0;
     foreach ($productParams as $param) {
         $paramsLinesCount += count(explode("\n", $param->value));
     }
     $paramsLinesCount++;
     $subproductsModel = new Model_DbTable_Subproducts();
     $select = $subproductsModel->select()->order('order ASC');
     $subProducts = $product->findDependentRowset("Model_DbTable_Subproducts", 'SubproductsRel', $select);
     // расчет высоты всего продукта, проверка влезает ли он на страницу, если не влезает смотрим сколько именно не влезает и есть ли субпродукты,
     // которые мы може отрисовать на этой странице и перенести отстаток на другую страницу
     $productHeightWithoutTable = 10 + max(array(80, $this->format == 'A4' ? $paramsLinesCount * 12 : $paramsLinesCount * 8)) + ($product->description ? $page->getTextBlockHeight(trim($product->description), $style, 3) : 0);
     $productHeight = 20 + max(array(80, $this->format == 'A4' ? $paramsLinesCount * 12 : $paramsLinesCount * 8)) + ($product->description ? $page->getTextBlockHeight(trim($product->description), $style, 3) : 0) + (count($subProducts) <= 30 ? $this->format == 'A4' ? count($subProducts) * 12 + 10 : count($subProducts) * 8 + 12 + 15 : 0) + ($product->note ? $page->getTextBlockHeight($product->note, $noteStyle) + 10 : 0);
     // note*/
     if ($offset) {
         if ($page->getHeight() - $offset - 10 < (count($subProducts) <= 30 ? $productHeight : intval($productHeight) + 40)) {
             if ($page->getPageNumber() > 1) {
                 $page->drawCategory(isset($this->old_category) ? $this->old_category : $this->category);
                 $this->old_category = NULL;
                 $page = $this->addPage();
                 $offset = 0;
             }
         } else {
             if ($page->getCurrentPosition() < 700 && $for_catalog) {
                 $offset -= 5;
             }
             $page->init($offset);
             $this->old_category = $this->category;
         }
     } else {
         $this->old_category = $this->category;
     }
     $this->product_page = $page->getPageNumber();
     // DEBUG auto-height calculate
     if (isset($_REQUEST['DEBUG'])) {
         $page->drawHorizontalLine(-20, 575, $page->getHeight() - $offset, 1, new Zend_Pdf_Color_Html('green'));
         $page->drawHorizontalLine(-20, 575, $page->getHeight() - $offset - $productHeight, 2, new Zend_Pdf_Color_Html('red'));
         $page->drawTextBlock(count($subProducts), -20, $page->getHeight() - $offset - $productHeight);
     }
     // --- block / information
     $page->setFont(Model_Static_Fonts::get("Franklin Gothic Demi Cond"), 14);
     $page->drawTextBlock($product->sku, 5, $page->getHeight() - $offset);
     $page->setFont(Model_Static_Fonts::get("Franklin Gothic Demi Cond"), 10);
     $page->drawTextBlock($product->name, 5, $page->getHeight() - 10 - $offset);
     // --- block / images
     $images = array($product->image);
     if ($product->a_images) {
         $images[] = $product->a_images[0];
     }
     $x = 0;
     if ($page->getPageNumber() % 2 == 0) {
         // если картинки справа (иконки слева)
         $images = array_reverse($images);
         // здесь нужно посчитать правильные ширины отступов начала изображений
         $x = $page->getWidth() - 5;
         foreach ($images as $image) {
             $sizes = $page->picSize($this->getProductImageFullpath($image), $this::IMAGE_SIZE, $this::IMAGE_SIZE, 2);
             //$x = $x - $this::IMAGE_SIZE * $sizes[0];
             $x = $x - $this::IMAGE_SIZE;
             //echo $image." ";
         }
         $x = $x - 5 * (count($images) - 1);
     } else {
         // картинки слева - просто задаем базовый отступ по x
         $x += 5;
     }
     //echo $x." ";
     $count = 0;
     // count of images (x75)
     //в зависимости от задачи, выбираем папку с картинками, за это отвечает параметр $print
     foreach ($images as $image) {
         if ($this->print) {
             //$c = $page -> drawPic(APPLICATION_ROOT . '/files/images/product_tiff/' . substr($image, 0, strripos($image, ".")).'.tif', $x, $page -> getHeight() - 20 - $offset, $this::IMAGE_SIZE, $this::IMAGE_SIZE, isset($images[1]) ? 1 : 2,1);
             $c = $page->drawPic($this->getProductImageFullpath($image), $x, $page->getHeight() - 20 - $offset, $this::IMAGE_SIZE, $this::IMAGE_SIZE, 2, 1);
         } else {
             $c = $page->drawPic($this->getProductImageFullpath($image), $x, $page->getHeight() - 20 - $offset, $this::IMAGE_SIZE, $this::IMAGE_SIZE, isset($images[1]) ? 1 : 2, 1);
         }
         if ($page->getPageNumber() % 2 == 0) {
             // если картинки справа, то не считаем полную ширину
             $x += $this::IMAGE_SIZE + 5;
         } else {
             $x += $this::IMAGE_SIZE * $c + 5;
         }
         $count += $c;
     }
     // --- block / params
     $offsetX = $page->getPageNumber() % 2 ? $this::IMAGE_SIZE * $count + 15 : 10;
     $tableWidth = $page->getWidth() - $this::IMAGE_SIZE * $count - 25;
     if (count($productParams) > 0) {
         $params = array();
         foreach ($productParams as $productParam) {
             $params[] = array($productParam->name, str_replace(array(' ' . chr('0x0D') . chr('0x0A'), chr('0x0D') . chr('0x0A')), ', ', $productParam->value));
         }
         $page->drawTable($params, $offsetX, $page->getHeight() - 30 - $offset, $tableWidth);
     }
     // --- block / description
     if ($product->description) {
         $style = new Zend_Pdf_Style();
         $style->setFont(Model_Static_Fonts::get("Arial Narrow"), $this->format == 'A4' ? 8.0 : 6.5);
         $style->setLineWidth($page->getWidth());
         if ($for_catalog) {
             $page->drawTextBlock(str_replace(array('      ', '       ', '        '), '. ', trim($product->description)), 5, $page->getCurrentPosition() + 25, $style, 3);
             //chr('0x0D').chr('0x0A')
             $page->setCurrentPosition($page->getCurrentPosition() + 10);
         } else {
             $page->drawTextBlock(str_replace(array('      ', '       ', '        '), chr('0x0D') . chr('0x0A'), trim($product->description)), 5, $page->getCurrentPosition() + 15, $style, 3);
             $page->setCurrentPosition($page->getCurrentPosition() + 15);
         }
     }
     // --- block / sub products
     $params = array();
     $subproductParams = $product->getSubParams();
     foreach ($subProducts as $subProduct) {
         $productParams = $subProduct->getParamsValues();
         $row = array($subProduct->sku);
         foreach ($productParams as $productParam) {
             $row[] = $productParam->value;
         }
         $params[] = $row;
     }
     if (count($subProducts) > 0) {
         $header = array('Название');
         foreach ($subproductParams as $subproductParam) {
             $header[] = $subproductParam->name;
         }
     }
     if ($product->note) {
         $noteHeight = $page->getTextBlockHeight($product->note, $noteStyle);
     } else {
         $noteHeight = 0;
     }
     //
     //	$noteHeight = 0;
     $curRow = 0;
     if ($params) {
         $page->setCurrentPosition($page->getCurrentPosition() + 20);
         // move table-description
         while ($curRow !== NULL) {
             $curRow = $page->drawTable($params, 0, $page->getCurrentPosition(), $page->getWidth() - 5, $noteHeight, $header, $curRow, true, $product->note);
             if ($curRow !== NULL) {
                 $page->drawCategory($this->category);
             }
             //		else {
             //	                if ($product -> note && $for_catalog) {
             //				$page -> drawTextBlock($product -> note, 5, $page -> getCurrentPosition() + 24, $noteStyle);
             //			}elseif($product -> note){
             //				$page -> drawTextBlock($product -> note, 5, $page -> getCurrentPosition() + 62, $noteStyle);
             //			}
             //		}
             //		$page -> drawTextBlock($curRow, -20, $page -> getHeight() - $offset - $productHeight);
             $page = $this->addPage(false);
             if ($curRow !== NULL) {
                 $curRow++;
             }
         }
     } else {
         $this->AddPage(false);
     }
     return $this->book;
 }