/**
  * create contact pdf
  *
  * @param    Addressbook_Model_Contact $_contact contact data
  *
  * @return    string    the contact pdf
  */
 public function generate(Addressbook_Model_Contact $_contact)
 {
     $locale = Tinebase_Core::get('locale');
     $translate = Tinebase_Translation::getTranslation('Addressbook');
     // set user timezone
     $_contact->setTimezone(Tinebase_Core::getUserTimezone());
     $contactFields = array(array('label' => $translate->_('Business Contact Data'), 'type' => 'separator'), array('label' => $translate->_('Organisation / Unit'), 'type' => 'singleRow', 'value' => array(array('org_name', 'org_unit')), 'glue' => ' / '), array('label' => $translate->_('Business Address'), 'type' => 'multiRow', 'value' => array('adr_one_street', 'adr_one_street2', array('adr_one_postalcode', 'adr_one_locality'), array('adr_one_region', 'adr_one_countryname'))), array('label' => $translate->_('Email'), 'value' => array('email')), array('label' => $translate->_('Telephone Work'), 'value' => array('tel_work')), array('label' => $translate->_('Telephone Cellphone'), 'value' => array('tel_cell')), array('label' => $translate->_('Telephone Car'), 'value' => array('tel_car')), array('label' => $translate->_('Telephone Fax'), 'value' => array('tel_fax')), array('label' => $translate->_('Telephone Page'), 'value' => array('tel_pager')), array('label' => $translate->_('URL'), 'value' => array('url')), array('label' => $translate->_('Role'), 'value' => array('role')), array('label' => $translate->_('Room'), 'value' => array('room')), array('label' => $translate->_('Assistant'), 'value' => array('assistent')), array('label' => $translate->_('Assistant Telephone'), 'value' => array('tel_assistent')), array('label' => $translate->_('Private Contact Data'), 'type' => 'separator'), array('label' => $translate->_('Private Address'), 'type' => 'multiRow', 'value' => array('adr_two_street', 'adr_two_street2', array('adr_two_postalcode', 'adr_two_locality'), array('adr_two_region', 'adr_two_countryname'))), array('label' => $translate->_('Email Home'), 'value' => array('email_home')), array('label' => $translate->_('Telephone Home'), 'value' => array('tel_home')), array('label' => $translate->_('Telephone Cellphone Private'), 'value' => array('tel_cell_private')), array('label' => $translate->_('Telephone Fax Home'), 'value' => array('tel_fax_home')), array('label' => $translate->_('URL Home'), 'value' => array('url_home')), array('label' => $translate->_('Other Data'), 'type' => 'separator'), array('label' => $translate->_('Birthday'), 'value' => array('bday')), array('label' => $translate->_('Job Title'), 'value' => array('title')));
     try {
         $tineImage = Addressbook_Controller::getInstance()->getImage($_contact->getId());
         Tinebase_ImageHelper::resize($tineImage, 160, 240, Tinebase_ImageHelper::RATIOMODE_PRESERVANDCROP);
         $tmpPath = tempnam(Tinebase_Core::getTempDir(), 'tine20_tmp_gd');
         $tmpPath .= $tineImage->getImageExtension();
         file_put_contents($tmpPath, $tineImage->blob);
         $contactPhoto = Zend_Pdf_Image::imageWithPath($tmpPath);
         unlink($tmpPath);
     } catch (Exception $e) {
         // gif images are not supported yet by zf (or some other error)
         if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) {
             Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . $e->__toString());
         }
         $contactPhoto = NULL;
     }
     // build title (name) + subtitle + icon
     $nameFields = array('n_prefix', 'n_given', 'n_middle', 'n_family', 'n_suffix');
     $titleArray = array();
     foreach ($nameFields as $nameField) {
         if (!empty($_contact[$nameField])) {
             $titleArray[] = $_contact[$nameField];
         }
     }
     $title = implode(' ', $titleArray);
     $subtitle = $_contact->org_name;
     $titleIcon = "/images/oxygen/32x32/apps/system-users.png";
     // add data to array
     $record = array();
     foreach ($contactFields as $fieldArray) {
         if (!isset($fieldArray['type']) || $fieldArray['type'] !== 'separator') {
             $values = array();
             foreach ($fieldArray['value'] as $valueFields) {
                 $content = array();
                 if (is_array($valueFields)) {
                     $keys = $valueFields;
                 } else {
                     $keys = array($valueFields);
                 }
                 foreach ($keys as $key) {
                     if ($_contact->{$key} instanceof Tinebase_DateTime) {
                         $content[] = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_contact->{$key}, NULL, NULL, 'date');
                     } elseif (!empty($_contact->{$key})) {
                         if (preg_match("/countryname/", $key)) {
                             $content[] = Zend_Locale::getTranslation($_contact->{$key}, 'country', $locale);
                         } else {
                             $content[] = $_contact->{$key};
                         }
                     }
                 }
                 if (!empty($content)) {
                     $glue = isset($fieldArray['glue']) ? $fieldArray['glue'] : " ";
                     $values[] = implode($glue, $content);
                 }
             }
             if (!empty($values)) {
                 $record[] = array('label' => $fieldArray['label'], 'type' => isset($fieldArray['type']) ? $fieldArray['type'] : 'singleRow', 'value' => sizeof($values) === 1 ? $values[0] : $values);
             }
         } elseif (isset($fieldArray['type']) && $fieldArray['type'] === 'separator') {
             $record[] = $fieldArray;
         }
     }
     // add notes
     $record = $this->_addActivities($record, $_contact->notes);
     //print_r($record);
     // tags
     $tags = isset($_contact['tags']) ? $_contact['tags'] : array();
     // generate pdf
     $this->generatePdf($record, $title, $subtitle, $tags, $_contact->note, $titleIcon, $contactPhoto, array(), FALSE);
 }
 /**
  * downloads an image/thumbnail at a given size
  *
  * @param unknown_type $application
  * @param string $id
  * @param string $location
  * @param int $width
  * @param int $height
  * @param int $ratiomode
  */
 public function getImage($application, $id, $location, $width, $height, $ratiomode)
 {
     $this->checkAuth();
     // close session to allow other requests
     Tinebase_Session::writeClose(true);
     $clientETag = null;
     $ifModifiedSince = null;
     if (isset($_SERVER['If_None_Match'])) {
         $clientETag = trim($_SERVER['If_None_Match'], '"');
         $ifModifiedSince = trim($_SERVER['If_Modified_Since'], '"');
     } elseif (isset($_SERVER['HTTP_IF_NONE_MATCH']) && isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         $clientETag = trim($_SERVER['HTTP_IF_NONE_MATCH'], '"');
         $ifModifiedSince = trim($_SERVER['HTTP_IF_MODIFIED_SINCE'], '"');
     }
     if ($application == 'Tinebase' && $location == 'tempFile') {
         $tempFile = Tinebase_TempFile::getInstance()->getTempFile($id);
         $imgInfo = Tinebase_ImageHelper::getImageInfoFromBlob(file_get_contents($tempFile->path));
         $image = new Tinebase_Model_Image($imgInfo + array('application' => $application, 'id' => $id, 'location' => $location));
     } else {
         $image = Tinebase_Controller::getInstance()->getImage($application, $id, $location);
     }
     $serverETag = sha1($image->blob . $width . $height . $ratiomode);
     // cache for 3600 seconds
     $maxAge = 3600;
     header('Cache-Control: private, max-age=' . $maxAge);
     header("Expires: " . gmdate('D, d M Y H:i:s', Tinebase_DateTime::now()->addSecond($maxAge)->getTimestamp()) . " GMT");
     // overwrite Pragma header from session
     header("Pragma: cache");
     // if the cache id is still valid
     if ($clientETag == $serverETag) {
         header("Last-Modified: " . $ifModifiedSince);
         header("HTTP/1.0 304 Not Modified");
         header('Content-Length: 0');
     } else {
         #$cache = Tinebase_Core::getCache();
         #if ($cache->test($serverETag) === true) {
         #    $image = $cache->load($serverETag);
         #} else {
         if ($width != -1 && $height != -1) {
             Tinebase_ImageHelper::resize($image, $width, $height, $ratiomode);
         }
         #    $cache->save($image, $serverETag);
         #}
         header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
         header('Content-Type: ' . $image->mime);
         header('Etag: "' . $serverETag . '"');
         flush();
         die($image->blob);
     }
 }
 /**
  * test preserve no fill fit height
  */
 public function testResizeRatioModePreservNoFillHeight()
 {
     $testImageRatio = $this->_testImageData['width'] / $this->_testImageData['height'];
     $dstHeight = 100;
     Tinebase_ImageHelper::resize($this->_testImage, $dstHeight * 100, $dstHeight, Tinebase_ImageHelper::RATIOMODE_PRESERVNOFILL);
     $this->assertEquals($dstHeight, $this->_testImage->height);
     $this->assertEquals(floor($dstHeight * $testImageRatio), $this->_testImage->width);
 }