Ejemplo n.º 1
0
 /**
  * Format a recordset
  *
  * @param Garp_Model $model
  * @param array $rowset
  * @return string
  */
 public function format(Garp_Model $model, array $rowset)
 {
     $phpexcel = new PHPExcel();
     PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_AdvancedValueBinder());
     // set metadata
     $props = $phpexcel->getProperties();
     if (Garp_Auth::getInstance()->isLoggedIn()) {
         $userData = Garp_Auth::getInstance()->getUserData();
         $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
         if ($bootstrap) {
             $view = $bootstrap->getResource('view');
             $userName = $view->fullName($userData);
             $props->setCreator($userName)->setLastModifiedBy($userName);
         }
     }
     $props->setTitle('Garp content export – ' . $model->getName());
     if (count($rowset)) {
         $this->_addContent($phpexcel, $model, $rowset);
     }
     /**
      * Hm, PHPExcel seems to only be able to write to a file (instead of returning
      * an XLS binary string). Therefore, we save a temporary file, read its contents
      * and return those, after which we unlink the temp file.
      */
     $tmpFileName = APPLICATION_PATH . '/data/logs/tmp.xls';
     $writer = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
     $writer->save($tmpFileName);
     $contents = file_get_contents($tmpFileName);
     unlink($tmpFileName);
     return $contents;
 }
Ejemplo n.º 2
0
 /**
  * Check if there are bindings to fetch related records and
  * start the related rowset fetching.
  * @param Garp_Model $model The model that spawned this data
  * @param Garp_Db_Table_Row|Garp_Db_Table_Rowset $data The fetched root data
  * @return Void
  */
 protected function _combineResultsWithBindings(Garp_Model $model, $data)
 {
     $tableName = $model->getName();
     if ($data) {
         $bindings = $model->getBindings();
         if (!empty($bindings)) {
             // check if it's a rowset or a single row
             if (!$data instanceof Garp_Db_Table_Rowset) {
                 $data = array($data);
             }
             foreach ($bindings as $binding => $bindOptions) {
                 /**
                  * We keep tabs on the outer call to fetch() by checking getRecursion.
                  * If it is 0, this is the first call in the chain. That way we can
                  * clean up the recursion when we're done, since all subsequent fetch() calls
                  * will happen within this one and the if ($cleanup) line ahead will only
                  * fire on the first fetch(). Look at it like this:
                  *
                  * fetch()
                  *   cleanup = 1
                  *     fetch()
                  *       fetch()
                  *     fetch()
                  *       fetch()
                  *     fetch()
                  *       fetch()
                  *   if (cleanup) resetRecursion()
                  *
                  */
                 $cleanup = false;
                 if (Garp_Model_Db_BindingManager::getRecursion(get_class($model), $binding) == 0) {
                     $cleanup = true;
                 }
                 if (Garp_Model_Db_BindingManager::isAllowedFetch(get_class($model), $binding)) {
                     Garp_Model_Db_BindingManager::registerFetch(get_class($model), $binding);
                     foreach ($data as $datum) {
                         // there's no relation possible if the primary key is not among the fetched columns
                         $prim = (array) $model->info(Zend_Db_Table::PRIMARY);
                         foreach ($prim as $key) {
                             try {
                                 $datum->{$key};
                             } catch (Exception $e) {
                                 break 2;
                             }
                         }
                         $relatedRowset = $this->_getRelatedRowset($model, $datum, $bindOptions);
                         $datum->setRelated($binding, $relatedRowset);
                     }
                 }
                 if ($cleanup) {
                     Garp_Model_Db_BindingManager::resetRecursion(get_class($model), $binding);
                 }
             }
             // return the pointer to 0
             if ($data instanceof Garp_Db_Table_Rowset) {
                 $data->rewind();
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Format a recordset
  * @param Garp_Model $model
  * @param Array $rowset
  * @return String
  */
 public function format(Garp_Model $model, array $rowset)
 {
     $view = new Zend_View();
     $view->setScriptPath(GARP_APPLICATION_PATH . '/modules/g/views/scripts/content/export/');
     $view->data = $rowset;
     $view->name = $model->getName();
     $out = $view->render('html.phtml');
     return $out;
 }
Ejemplo n.º 4
0
 /**
  * Create a WHERE clause for use with Zend_Db_Select
  *
  * @param array $query WHERE options
  * @param string $separator AND/OR
  * @param bool $useJointView Wether to use the *_joint view or the table.
  * @return string WHERE clause
  */
 protected function _createWhereClause(array $query, $separator = 'AND', $useJointView = true)
 {
     $where = array();
     $adapter = $this->_model->getAdapter();
     $nativeColumns = $this->_model->info(Zend_Db_Table_Abstract::COLS);
     if ($useJointView) {
         $tableName = $this->_getTableName($this->_model);
         $mockTable = new Zend_Db_Table(array(Zend_Db_Table_Abstract::NAME => $tableName, Zend_Db_Table_Abstract::PRIMARY => $this->_model->info(Zend_Db_Table_Abstract::PRIMARY)));
         $nativeColumns = $mockTable->info(Zend_Db_Table_Abstract::COLS);
     } else {
         $tableName = $this->_model->getName();
     }
     // change native columns to lowercase
     // because when columnName is configured in camelcase in the model config
     // it causes problems when checking if refColumn is a native column
     $nativeColumns = array_map(function ($column) {
         return strtolower($column);
     }, $nativeColumns);
     foreach ($query as $column => $value) {
         if (strtoupper($column) === 'OR' && is_array($value)) {
             $where[] = $this->_createWhereClause($value, 'OR');
         } elseif (is_array($value)) {
             $where[] = $adapter->quoteInto($adapter->quoteIdentifier($tableName) . '.' . $column . ' IN(?)', $value);
         } elseif (is_null($value)) {
             if (substr($column, -2) == '<>') {
                 $column = preg_replace('/<>$/', '', $column);
                 $where[] = $column . ' IS NOT NULL';
             } else {
                 $where[] = $column . ' IS NULL';
             }
         } elseif (is_scalar($value)) {
             // Use $refColumn to see if this column is native to the current
             // model.
             $refColumn = null;
             if (!preg_match('/(>=?|<=?|like|<>)/i', $column, $matches)) {
                 $refColumn = $column;
                 $column = $adapter->quoteIdentifier($column) . ' =';
             } else {
                 // explode column so the actual column name can be quoted
                 $parts = explode(' ', $column);
                 $refColumn = $parts[0];
                 $column = $adapter->quoteIdentifier($parts[0]) . ' ' . $parts[1];
             }
             if (strpos($refColumn, '.') === false && in_array($refColumn, $nativeColumns)) {
                 $column = $adapter->quoteIdentifier($tableName) . '.' . $column;
             }
             $where[] = $adapter->quoteInto($column . ' ?', $value);
         }
     }
     return '(' . implode(" {$separator} ", $where) . ')';
 }
Ejemplo n.º 5
0
 /**
  * Return the current weight of a set of records.
  * Note that only the first record found will be used. Working with multiple records
  * (which is possible using Zend's update() functionality) is not implemented.
  * @param Garp_Model $model
  * @param String $where
  * @param Array $modelRelationConfig 
  * @return Int
  */
 public function findCurrentWeight(Garp_Model $model, $where, $foreignKey, array $modelRelationConfig)
 {
     $foreignKeyColumn = $model->getAdapter()->quoteIdentifier($modelRelationConfig[self::FOREIGN_KEY_COLUMN_KEY]);
     $weightColumn = $modelRelationConfig[self::WEIGHT_COLUMN_KEY];
     $select = $model->select()->from($model->getName(), array('weight' => $weightColumn))->where($foreignKeyColumn . ' = ?', $foreignKey);
     $where = (array) $where;
     foreach ($where as $w) {
         $select->where($w);
     }
     $result = $model->fetchRow($select);
     if ($result && $result->weight) {
         return $result->weight;
     }
     return 0;
 }