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
 /**
  * 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.º 3
0
 /**
  * Insert a new row
  * @param Garp_Model $model
  * @param Array $data Collection of data
  * @param Array $mapping Collection of column names
  * @return Mixed primary key
  */
 protected function _insert(Garp_Model $model, array $data, array $mapping)
 {
     $newData = array();
     // Remove unused mappings
     $mapping = array_filter($mapping);
     foreach ($mapping as $key => $val) {
         $newData[$val] = $data[$key];
     }
     return $model->insert($newData);
 }
Ejemplo n.º 4
0
 /**
  * Rollback all inserts when the import throws an error halfway
  * @param Garp_Model $model
  * @param Array $primaryKeys Collection of primary keys
  * @return Void
  */
 public function rollback(Garp_Model $model, array $primaryKeys)
 {
     if (empty($primaryKeys)) {
         return;
     }
     $primaryCols = (array) $model->info(Zend_Db_Table::PRIMARY);
     $where = array();
     foreach ($primaryKeys as $pk) {
         $recordWhere = array();
         foreach ((array) $pk as $i => $key) {
             $recordWhere[] = $model->getAdapter()->quoteIdentifier(current($primaryCols)) . ' = ' . $model->getAdapter()->quote($key);
         }
         $recordWhere = implode(' AND ', $recordWhere);
         $recordWhere = '(' . $recordWhere . ')';
         $where[] = $recordWhere;
         reset($primaryCols);
     }
     $where = implode(' OR ', $where);
     if (empty($where)) {
         return;
     }
     $model->delete($where);
 }
Ejemplo n.º 5
0
 /**
  * Check a record belongs to the currently logged in user.
  * This check is based on the author_id column.
  *
  * @param array $data The record data. Primary key must be present here.
  * @param string $where A WHERE clause to find the record
  * @return bool
  */
 protected function _itemBelongsToUser($data, $where = false)
 {
     $userData = Garp_Auth::getInstance()->getUserData();
     $userId = $userData['id'];
     if (!array_key_exists('author_id', $data)) {
         if (!$where) {
             return false;
         }
         // fetch the record based on the given WHERE clause
         $row = $this->_model->fetchRow($where);
         if (!$row || !$row->author_id) {
             return false;
         }
         $data = $row->toArray();
     }
     return $userId == $data['author_id'];
 }
Ejemplo n.º 6
0
 /**
  * Find a related recordset.
  * @param Garp_Model $model The model that spawned this data
  * @param Garp_Db_Row $row The row object
  * @param Garp_Util_Configuration $options Various relation options
  * @return String The name of the method.
  */
 protected function _getRelatedRowset(Garp_Model $model, Garp_Db_Table_Row $row, Garp_Util_Configuration $options)
 {
     /**
      * An optional passed SELECT object will be passed by reference after every query.
      * This results in an error when 'clone' is not used, because correlation names will be
      * used twice (since they were set during the first iteration). Using 'clone' makes sure
      * a brand new SELECT object is used every time that hasn't been soiled by a possible
      * previous query.
      */
     $conditions = is_null($options['conditions']) ? null : clone $options['conditions'];
     $otherModel = $options['modelClass'];
     if (!$otherModel instanceof Zend_Db_Table_Abstract) {
         $otherModel = new $otherModel();
     }
     /**
      * Do not cache related queries. The "outside" query should be the only
      * query that's cached.
      */
     $originalCacheQueriesFlag = $otherModel->getCacheQueries();
     $otherModel->setCacheQueries(false);
     $modelName = get_class($otherModel);
     $relatedRowset = null;
     // many to many
     if (!empty($options['bindingModel'])) {
         $relatedRowset = $row->findManyToManyRowset($otherModel, $options['bindingModel'], $options['rule2'], $options['rule'], $conditions);
     } else {
         /**
          * 'mode' is used to clear ambiguity with homophilic relationships. For example,
          * a Model_Doc can have have child Docs and one parent Doc. The conditionals below can never tell
          * which method to call (findParentRow or findDependentRowset) from the referenceMap.
          * Therefore, we can help the decision-making by passing "mode". This can either be
          * "parent" or "dependent", which will then force a call to findParentRow and findDependentRowset,
          * respectively.
          */
         if (is_null($options['mode'])) {
             // belongs to
             try {
                 $model->getReference($modelName, $options['rule']);
                 $relatedRowset = $row->findParentRow($otherModel, $options['rule'], $conditions);
             } catch (Exception $e) {
                 if (!Garp_Content_Relation_Manager::isInvalidReferenceException($e)) {
                     throw $e;
                 }
                 try {
                     // one to many - one to one
                     // The following line triggers an exception if no reference is available
                     $otherModel->getReference(get_class($model), $options['rule']);
                     $relatedRowset = $row->findDependentRowset($otherModel, $options['rule'], $conditions);
                 } catch (Exception $e) {
                     if (!Garp_Content_Relation_Manager::isInvalidReferenceException($e)) {
                         throw $e;
                     }
                     $bindingModel = $model->getBindingModel($modelName);
                     $relatedRowset = $row->findManyToManyRowset($otherModel, $bindingModel, $options['rule2'], $options['rule'], $conditions);
                 }
             }
         } else {
             switch ($options['mode']) {
                 case 'parent':
                     $relatedRowset = $row->findParentRow($otherModel, $options['rule'], $conditions);
                     break;
                 case 'dependent':
                     $relatedRowset = $row->findDependentRowset($otherModel, $options['rule'], $conditions);
                     break;
                 default:
                     throw new Garp_Model_Exception('Invalid value for "mode" given. Must be either "parent" or ' . '"dependent", but "' . $options['mode'] . '" was given.');
                     break;
             }
         }
     }
     // Reset the cacheQueries value. It's a static property,
     // so leaving it FALSE will affect all future fetch() calls to this
     // model. Not good.
     $otherModel->setCacheQueries($originalCacheQueriesFlag);
     return $relatedRowset;
 }
Ejemplo n.º 7
0
 /**
  * Insert a new row
  * @param Garp_Model $model
  * @param Array $cellData Collection of data
  * @param Array $mapping Collection of column names
  * @return Mixed primary key
  */
 protected function _insert(Garp_Model $model, array $cellData, array $mapping)
 {
     if (count($cellData) !== count($mapping)) {
         throw new Exception("Cannot create rowdata from these keys and values.\nKeys:" . implode(', ', $mapping) . "\n" . "Values:" . implode(', ', $cellData));
     }
     $data = array_combine($mapping, $cellData);
     // ignored columns have an empty key
     unset($data['']);
     return $model->insert($data);
 }
Ejemplo n.º 8
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;
 }