/**
  * 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);
         }
     }
 }
 public function generateContents($page = 1)
 {
     $INDEXPAGE_ROWS = $this::INDEXPAGE_ROWS - 44;
     $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);
     }
     $categoryIndexModel = new Model_DbTable_CategoryIndex();
     $indexes = $categoryIndexModel->getAdapter()->fetchAll("SELECT c.name AS name, ci.page AS page, ci.category_id as category_id, ci.depth as depth FROM categories AS c INNER JOIN categoryIndex AS ci ON ci.category_id = c.id ORDER BY ci.page, ci.depth");
     $page = $this->createBook($page, false);
     $colWidth = $page->getWidth();
     $pointWidth = $page->widthForStringUsingFontsize('.', Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 9.0 : 4.5);
     for ($p = 0; $p < intval(count($indexes) / $INDEXPAGE_ROWS + 1); $p++) {
         $page->setFont(Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 9.0 : 4.5);
         $page->setFillColor(new Zend_Pdf_Color_Html("#000000"));
         for ($row = 0; $row < $INDEXPAGE_ROWS; $row++) {
             $productIndex = (object) $indexes[$p * $INDEXPAGE_ROWS + $row];
             /* write */
             if (!trim($productIndex->name)) {
                 continue;
             }
             $curColWidth = $colWidth - $page->widthForStringUsingFontsize($productIndex->page, Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 9.0 : 4.5);
             switch ($productIndex->depth) {
                 case 0:
                     $nameFont = 'Arial Narrow Bold';
                     $page->setFillColor(new Zend_Pdf_Color_Html("#ef7900"));
                     break;
                 case 1:
                     $nameFont = 'Arial Narrow Bold';
                     $page->setFillColor(new Zend_Pdf_Color_Html("#000000"));
                     break;
                 case 2:
                 default:
                     $nameFont = 'Arial Narrow';
                     $page->setFillColor(new Zend_Pdf_Color_Html("#000000"));
                     break;
             }
             // append `.` to page
             $page->setFont(Model_Static_Fonts::get($nameFont), $this->format == 'A4' ? 9.0 : 4.5);
             $textWidth = $page->widthForStringUsingFontsize($productIndex->name, Model_Static_Fonts::get($nameFont), $this->format == 'A4' ? 9.0 : 4.5);
             $points = str_repeat(chr('0x2E'), intval(($curColWidth - $textWidth - intval($productIndex->depth * 10)) / 2 - 5));
             $page->drawTextBlock($productIndex->name, intval($productIndex->depth * 10), $page->getHeight() - $row * 9);
             $page->setFont(Model_Static_Fonts::get('Arial Narrow'), $this->format == 'A4' ? 9.0 : 4.5);
             $page->setFillColor(new Zend_Pdf_Color_Html("#000000"));
             $page->drawTextBlock($points, $textWidth + $productIndex->depth * 10, $page->getHeight() - $row * 9);
             $page->drawTextBlock($productIndex->page, $curColWidth, $page->getHeight() - $row * 9);
         }
         $page->drawCategory($this->category);
         $page->drawSideIcons("A");
         $page = $this->addPage(false);
     }
     return $this->book;
 }