Esempio n. 1
0
 /**
  * @param string $file
  * @return string
  */
 protected function getData()
 {
     $this->_class->setMetadata($this->metadata);
     $this->_class->setParams($this->params);
     $this->_class->setRows($this->rows);
     $this->_class->setTitle($this->title);
     return $this->_class->export(false);
 }
 /**
  * Get data from the application in a certain mime type format.
  * @param integer $id id of data to get from the application
  * @param string $type specifies the mime type of the returned data
  * @param string $version specifies the mime type version of the returned data
  * @return mixed data from application, the datatype depends on the passed mime type, false if no data exists for the passed id
  */
 function getData($id, $type, $version = '')
 {
     if (!is_integer($id) || $id <= 0) {
         return false;
     }
     switch ($type) {
         case 'text/x-ical':
         case 'text/calendar':
             return $this->bo_iCal->export(array('l_event_id' => $id));
             break;
         case 'text/xml':
             return false;
             break;
         case 'x-phpgroupware/search-index-data-item':
             $event = $this->bo->read_entry($id);
             if (!$event) {
                 return false;
             }
             $event_array = $this->bo->event2array($event);
             $fields = array();
             while (list($key, $value) = each($event_array)) {
                 if (isset($value['data'])) {
                     if (is_array($value['data'])) {
                         $fields[$key] = implode(',', $value['data']);
                     } else {
                         $fields[$key] = $value['data'];
                     }
                 }
             }
             $fields['id'] = $id;
             $fields['owner_id'] = $event['owner'];
             $fields['priority'] = $event['priority'];
             $fields['category_id'] = $event['category'];
             $fields['last_mod'] = $this->bo->maketime($event['modtime']);
             return $this->_export_index_data_item($fields);
             break;
         default:
             return false;
     }
 }
Esempio n. 3
0
 /**
  * Exports source file coverage
  *
  * @param  object $collector The collector instance.
  * @return array
  */
 protected static function _sourceFiles($collector)
 {
     $result = [];
     foreach ($collector->export() as $file => $data) {
         $content = file_get_contents($file);
         $nbLines = substr_count($content, "\n");
         $lines = [];
         for ($i = 0; $i <= $nbLines; $i++) {
             $lines[] = isset($data[$i]) ? $data[$i] : null;
         }
         $result[] = ['name' => $file, 'coverage' => json_encode($lines), 'blob_id' => sha1('blob ' . strlen($content) . "" . $content)];
     }
     return $result;
 }
Esempio n. 4
0
 /**
  * Helper method used by `export()` to extract the data either from a bound entity, or from
  * passed configuration, and filter it through a configured whitelist, if present.
  *
  * @return array
  */
 protected function _exportData()
 {
     $data = $this->_entity ? $this->_entity->export() : $this->_data;
     if (!($list = $this->_config['whitelist'])) {
         return $data;
     }
     $list = array_combine($list, $list);
     if (!$this->_entity) {
         return array_intersect_key($data, $list);
     }
     foreach ($data as $type => $values) {
         if (!is_array($values)) {
             continue;
         }
         $data[$type] = array_intersect_key($values, $list);
     }
     return $data;
 }
Esempio n. 5
0
 /**
  * Helper method for `Database::read()` to export query while handling additional joins
  * when using relationships and limited result sets. Filters conditions on subsequent
  * queries to just the ones applying to the relation.
  *
  * @see lithium\data\source\Database::read()
  * @param object $query The query object.
  * @return array The exported query returned by reference.
  */
 protected function &_queryExport($query)
 {
     $data = $query->export($this);
     if (!$query->limit() || !($model = $query->model())) {
         return $data;
     }
     foreach ($query->relationships() as $relation) {
         if ($relation['type'] !== 'hasMany') {
             continue;
         }
         $pk = $this->name($model::meta('name') . '.' . $model::key());
         if ($query->order()) {
             list($fields, $orders) = $this->_distinctExport($query);
             array_unshift($fields, "DISTINCT ON({$pk}) {$pk} AS _ID_");
             $command = $this->renderCommand('read', array('limit' => null, 'order' => null, 'fields' => implode(', ', $fields)) + $data);
             $command = rtrim($command, ';');
             $command = $this->renderCommand('read', array('source' => "( {$command} ) AS _TEMP_", 'fields' => '_ID_', 'order' => "ORDER BY " . implode(', ', $orders), 'limit' => $data['limit']));
         } else {
             $command = $this->renderCommand('read', array('fields' => "DISTINCT({$pk}) AS _ID_") + $data);
         }
         $result = $this->_execute($command);
         $ids = array();
         foreach ($result as $row) {
             $ids[] = $row[0];
         }
         if (!$ids) {
             $data = null;
             break;
         }
         $conditions = array();
         $relations = array_keys($query->relationships());
         $pattern = '/^(' . implode('|', $relations) . ')\\./';
         foreach ($query->conditions() as $key => $value) {
             if (preg_match($pattern, $key)) {
                 $conditions[$key] = $value;
             }
         }
         $data['conditions'] = $this->conditions(array($pk => $ids) + $conditions, $query);
         $data['limit'] = '';
         break;
     }
     return $data;
 }
Esempio n. 6
0
 /**
  * Helper method for `Database::read()` to export query while handling additional joins
  * when using relationships and limited result sets. Filters conditions on subsequent
  * queries to just the ones applying to the relation.
  *
  * @see lithium\data\source\Database::read()
  * @param object $query The query object.
  * @return array The exported query returned by reference.
  */
 protected function &_queryExport($query)
 {
     $data = $query->export($this);
     if (!$query->limit() || !($model = $query->model())) {
         return $data;
     }
     foreach ($query->relationships() as $relation) {
         if ($relation['type'] !== 'hasMany') {
             continue;
         }
         $pk = $this->name($model::meta('name') . '.' . $model::key());
         $result = $this->_execute($this->renderCommand('read', array('fields' => "DISTINCT({$pk}) AS _ID_") + $data));
         $ids = array();
         foreach ($result as $row) {
             $ids[] = $row[0];
         }
         if (!$ids) {
             $data = null;
             break;
         }
         $conditions = array();
         $relations = array_keys($query->relationships());
         $pattern = '/^(' . implode('|', $relations) . ')\\./';
         foreach ($query->conditions() as $key => $value) {
             if (preg_match($pattern, $key)) {
                 $conditions[$key] = $value;
             }
         }
         $data['conditions'] = $this->conditions(array($pk => $ids) + $conditions, $query);
         $data['limit'] = '';
         break;
     }
     return $data;
 }
Esempio n. 7
0
 /**
  * Helper which export the query export
  *
  * @param object $query The query object
  * @return array The export array
  */
 protected function &_queryExport($query)
 {
     $data = $query->export($this);
     if ($query->limit() && ($model = $query->model())) {
         foreach ($query->relationships() as $relation) {
             if ($relation['type'] === 'hasMany') {
                 $name = $model::meta('name');
                 $key = $model::key();
                 $fields = $data['fields'];
                 $fieldname = $this->name("{$name}.{$key}");
                 $data['fields'] = "DISTINCT({$fieldname}) AS _ID_";
                 $sql = $this->renderCommand('read', $data);
                 $result = $this->_execute($sql);
                 $ids = array();
                 while ($row = $result->next()) {
                     $ids[] = $row[0];
                 }
                 if (!$ids) {
                     $return = null;
                     return $return;
                 }
                 $data['fields'] = $fields;
                 $data['limit'] = '';
                 $data['conditions'] = $this->conditions(array("{$name}.{$key}" => $ids), $query);
                 return $data;
             }
         }
     }
     return $data;
 }
Esempio n. 8
0
 /**
  * This method generates a new version.
  *
  * It creates a duplication of the object, to allow restoring. It marks all prior
  * versions as `outdated` and the new one as `active`.
  *
  * You probably want to create a new version of an entity, whenever save is called. To achieve
  * this, you have to take care, all data is set into the entity and Versions::add with updated
  * entity is called.
  *
  * In the following example you can see, how a meta-field, `versions` is used, to decide if
  * a version needs to be created, or not.
  *
  * {{{
  *	public function save($entity, $data = array(), array $options = array()) {
  *		if (!empty($data)) {
  *			$entity->set($data);
  *		}
  *		if (!isset($options['callbacks']) || $options['callbacks'] !== false) {
  *			$versions = static::meta('versions');
  *			if (($versions === true) || (is_callable($versions) && $versions($entity, $options))) {
  *				$version_id = Versions::add($entity, $options);
  *				if ($version_id) {
  *					$entity->set(compact('version_id'));
  *				}
  *			}
  *		}
  *		return parent::save($entity, null, $options);
  *	}
  * }}}
  *
  * You have to set `versions` to true, in meta like this:
  *
  * {{{
  *  $model::meta('versions', true);
  * // OR
  *  static::meta('versions', function($entity, $options){
  *		return (bool) Environment::is('production');
  *	});
  * }}}
  *
  * @param object $entity the instance, that needs to created a new version for
  * @param array $options additional options
  * @filter
  */
 public static function add($entity, array $options = array())
 {
     $defaults = array('force' => false);
     $options += $defaults;
     $params = compact('entity', 'options');
     return static::_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         $model = $entity->model();
         if ($model == $self || !$entity->exists()) {
             return false;
         }
         $key = $model::meta('key');
         $foreign_id = (string) $entity->{$key};
         $export = $entity->export();
         $updated = Set::diff($self::cleanData($export['update']), $self::cleanData($export['data']));
         if (empty($updated)) {
             if (!$options['force']) {
                 return false;
             }
             $updated = $entity->data();
         }
         $self::update(array('status' => 'outdated'), compact('model', 'foreign_id'));
         $data = array('model' => $model, 'foreign_id' => $foreign_id, 'status' => 'active', 'name' => (string) $entity->title(), 'fields' => $updated, 'data' => json_encode($entity->data()), 'created' => time());
         $version = $self::create($data);
         if (!$version->save()) {
             return false;
         }
         return $version->id();
     });
 }