/**
  * Prepare to new PDF book
  * clear folder and indexes table
  */
 public function newbookAction()
 {
     if (!Zend_Auth::getInstance()->hasIdentity()) {
         throw new Zend_Exception("Page not found", 404);
     }
     $this->_helper->layout->disableLayout();
     // clear indexes
     $categoryIndex = new Model_DbTable_CategoryIndex();
     $productIndex = new Model_DbTable_ProductIndex();
     $categoryIndex->truncate();
     $productIndex->truncate();
     // clear folder
     $files = scandir(APPLICATION_ROOT . $this::PDFBOOK_DIR);
     foreach ($files as $file) {
         $file = APPLICATION_ROOT . $this::PDFBOOK_DIR . '/' . $file;
         if (is_file($file)) {
             unlink($file);
         }
     }
 }
 /**
  * Adds index pages
  * 
  * @param int $page Start page
  * 
  * @return Zend_Pdf Generated book
  */
 public function IndexPages($page = 1)
 {
     $this->category = "Предметный указатель";
     if ($this->format == 'A4' && $this->print) {
         $this->margins = array('top' => 34, 'right' => 64, 'bottom' => 34, 'left' => 44);
     } else {
         $this->margins = array('top' => 20, 'right' => 50, 'bottom' => 20, 'left' => 30);
     }
     if ($page % 2 == 0) {
         $this->margins = $this->swapMargins($this->margins);
     }
     $productIndexModel = new Model_DbTable_ProductIndex();
     $indexes = $productIndexModel->getAdapter()->fetchAll("SELECT p.sku AS sku, pi.page AS page, pi.product_id as product_id FROM products AS p INNER JOIN productIndex AS pi ON pi.product_id = p.id WHERE p.parent_id IS NULL ORDER BY p.sku");
     $page = $this->createBook($page, false);
     $colWidth = $page->getWidth() / $this::INDEXPAGE_COLUMNS - 20;
     $pointWidth = $page->widthForStringUsingFontsize('.', Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 7.0 : 4.5);
     for ($p = 0; $p < intval(count($indexes) / ($this::INDEXPAGE_COLUMNS * $this::INDEXPAGE_ROWS) + 1); $p++) {
         $page->setFont(Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 7.0 : 4.5);
         for ($column = 0; $column < $this::INDEXPAGE_COLUMNS; $column++) {
             for ($row = 0; $row < $this::INDEXPAGE_ROWS; $row++) {
                 $productIndex = (object) $indexes[$p * $this::INDEXPAGE_COLUMNS * $this::INDEXPAGE_ROWS + $this::INDEXPAGE_ROWS * $column + $row];
                 /* write */
                 if (!trim($productIndex->sku)) {
                     continue;
                 }
                 $curColWidth = $colWidth - $page->widthForStringUsingFontsize($productIndex->page, Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 7.0 : 4.5);
                 // append `.` to page
                 $textWidth = $page->widthForStringUsingFontsize($productIndex->sku, Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 7.0 : 4.5);
                 $productIndex->sku .= str_repeat('.', intval(($curColWidth - $textWidth) / ($pointWidth / 2)));
                 $page->drawTextBlock($productIndex->sku, ($colWidth + 20) * $column, $page->getHeight() - $row * 6);
                 //                    $page->drawTextBlock(intval($page->getWidth()).'--'.$colWidth.'--'.(($colWidth+20)*$column + $curColWidth).'--'.intval($curColWidth-$textWidth), ($colWidth+20)*$column, $page->getHeight()-$row*6);
                 $page->drawTextBlock($productIndex->page, ($colWidth + 20) * $column + $curColWidth, $page->getHeight() - $row * 6);
             }
         }
         $page->drawCategory($this->category);
         $page->drawSideIcons("A");
         $page = $this->addPage(false);
     }
     return $this->book;
 }