Ejemplo n.º 1
0
 function save($data)
 {
     if (empty($data['Origin']) && !Set::numeric(array_keys($data))) {
         $data = array('Origin' => $data);
     }
     $key = null;
     if (!empty($data['Origin']['link'])) {
         $key = md5($data['Origin']['link']);
         if (!empty($this->__cache[$key])) {
             return $this->__cache[$key];
         }
     }
     if (empty($data['Origin']['id']) && !empty($data['Origin']['provider_key'])) {
         $found = parent::find('first', array('conditions' => array('Origin.provider_key' => $data['Origin']['provider_key'])));
         if ($found) {
             unset($found['Origin']['modified']);
             $this->set($found);
         }
     }
     $this->set($data);
     $result = parent::save();
     $result['Origin']['id'] = $this->id;
     if ($key) {
         $this->__cache[$key] = $result;
     }
     return $result;
 }
Ejemplo n.º 2
0
 function addLoadedItems($items)
 {
     $this->load();
     foreach ($items as $modelName => $m_items) {
         if (!Set::numeric(array_keys(SetMulti::excludeKeys($m_items, array('internal'))))) {
             $m_items = array($m_items);
         }
         foreach ($m_items as $item) {
             if (!empty($item['id'])) {
                 $model = ClassRegistry::init($modelName);
                 if ($model->Behaviors->attached('Node')) {
                     if (!empty($item['internal']['Node'][0]['id'])) {
                         $nodeId = $item['internal']['Node'][0]['id'];
                     } else {
                         $node = $model->node($model->myNodeRef($item['id']), false, true);
                         $nodeId = $node['Node']['id'];
                     }
                     if (!empty($nodeId)) {
                         if (isset($this->loadedItems[$nodeId])) {
                             unset($this->loadedItems[$nodeId]);
                             //Make sure everything id in DESC Order
                         }
                         $this->loadedItems[$nodeId] = $this->startTime;
                         if (!empty($item['items'])) {
                             $this->addLoadedItems($item['items']);
                         }
                     }
                 }
             }
         }
     }
     //debug($this->loadedItems);
 }
Ejemplo n.º 3
0
 function afterFind($data)
 {
     if ($data && is_array($data) && !Set::numeric(array_keys($data))) {
         $data = array($data);
     }
     if (is_array($data) && Set::numeric(array_keys($data))) {
         foreach ($data as $i => $row) {
             if (empty($row['user']) && !empty($row['sender'])) {
                 $row['user'] = $row['sender'];
                 $row['message'] = true;
             } else {
                 $row['message'] = false;
             }
             $data[$i] = array('Item' => array('provider_key' => $row['id'], 'message' => $row['message'], 'text' => $row['text'], 'client' => $this->parse($row, 'source'), 'link' => 'http://twitter.com/' . $row['user']['screen_name'] . '/status/' . $row['id'], 'reply_user_provider_key' => $this->parse($row, 'in_reply_to_user_id'), 'reply_status_provider_key' => $this->parse($row, 'in_reply_to_status_id'), 'posted' => date('Y-m-d H:i:s', strtotime($row['created_at']))), 'Origin' => array('provider_key' => $row['user']['id'], 'name' => $row['user']['screen_name'], 'profile' => $row['user']['description'], 'origin_link' => $row['user']['url'], 'follower_count' => $row['user']['followers_count'], 'following_count' => $row['user']['friends_count'], 'update_count' => $row['user']['statuses_count'], 'avatar' => $row['user']['profile_image_url'], 'link' => 'http://twitter.com/' . $row['user']['screen_name']));
             if ($this->name == 'TwitterAccount') {
                 $data[$i]['Origin']['following'] = true;
             }
             if (!empty($row['retweeted_status'])) {
                 $data[$i]['Item']['text'] = $row['retweeted_status']['text'];
                 $data[$i]['ForwardOrigin'] = array('provider_key' => $row['retweeted_status']['user']['id'], 'name' => $row['retweeted_status']['user']['screen_name'], 'profile' => $row['retweeted_status']['user']['description'], 'origin_link' => $row['retweeted_status']['user']['url'], 'follower_count' => $row['retweeted_status']['user']['followers_count'], 'following_count' => $row['retweeted_status']['user']['friends_count'], 'update_count' => $row['retweeted_status']['user']['statuses_count'], 'avatar' => $row['retweeted_status']['user']['profile_image_url'], 'link' => 'http://twitter.com/' . $row['retweeted_status']['user']['screen_name']);
             }
         }
     } else {
         if (is_string($data)) {
             $dataArray = explode('&', $data);
             $data = array();
             foreach ($dataArray as $value) {
                 list($key, $value) = explode('=', $value);
                 $data[$key] = $value;
             }
         }
     }
     return $data;
 }
Ejemplo n.º 4
0
 function afterFind($results, $primary)
 {
     $results = parent::afterFind($results, $primary);
     if (!empty($results)) {
         if (!Set::numeric(array_keys($results))) {
             $tmp = array(&$results);
             $myResults =& $tmp;
         } else {
             $myResults =& $results;
         }
         foreach ($myResults as &$resRoot) {
             ////// get updated Data //////
             if (isset($resRoot[$this->alias])) {
                 $res =& $resRoot[$this->alias];
             } else {
                 $res =& $resRoot;
             }
             $res['TemplateConfig'] = $this->getConfig($res);
             if (!empty($res['TemplateConfig'])) {
                 $result = $res['TemplateConfig']->afterFind($this, $res);
                 if (!empty($result)) {
                     $res = $result;
                 }
             }
             if (!empty($resRoot['NewsletterAssoc'])) {
                 foreach ($resRoot['NewsletterAssoc'] as $assoc) {
                     $res['associated'][$assoc['type']] = $assoc['newsletter_id'];
                 }
             }
         }
     }
     return $results;
 }
Ejemplo n.º 5
0
 function afterFind($data)
 {
     $data = $data['results'];
     if ($data && Set::numeric(array_keys($data))) {
         foreach ($data as $i => $row) {
             $data[$i] = array('Item' => array('provider_key' => $row['id'], 'text' => $row['text'], 'client' => $row['source'], 'link' => 'http://twitter.com/' . $row['from_user'] . '/status/' . $row['id'], 'posted' => date('Y-m-d H:i:s', strtotime($row['created_at']))), 'Origin' => array('provider_key' => $row['from_user_id'], 'name' => $row['from_user'], 'avatar' => $row['profile_image_url'], 'link' => 'http://twitter.com/' . $row['from_user']));
         }
     }
     return $data;
 }
Ejemplo n.º 6
0
 function __prepareAction($data)
 {
     if (isset($data['Action']) && is_array($data['Action']) && count($data['Action']) > 0 && !Set::numeric(array_keys($data['Action']))) {
         $action = $data['Action'];
         $data['Action'] = array();
         $i = 0;
         foreach ($action as $metaUuid => $metaArray) {
             $data['Action'][$i] = $metaArray;
             $i++;
         }
     }
     return $data;
 }
Ejemplo n.º 7
0
 /**
  * Private method for MetaBehavior::prepareData()
  *
  * @param object $model
  * @param array  $data
  * @return array
  */
 private function __prepareMeta($data)
 {
     if (isset($data['Meta']) && is_array($data['Meta']) && count($data['Meta']) > 0 && !Set::numeric(array_keys($data['Meta']))) {
         $meta = $data['Meta'];
         $data['Meta'] = array();
         $i = 0;
         foreach ($meta as $metaUuid => $metaArray) {
             $data['Meta'][$i] = $metaArray;
             $i++;
         }
     }
     return $data;
 }
Ejemplo n.º 8
0
 function beforeSave()
 {
     // Convert all the uuid keys to ordered numbers
     if (isset($this->data['NodeSchemaField']) && is_array($this->data['NodeSchemaField']) && count($this->data['NodeSchemaField']) > 0 && !Set::numeric(array_keys($this->data['NodeSchemaField']))) {
         $nodeSchemaField = $this->data['NodeSchemaField'];
         $this->data['NodeSchemaField'] = array();
         $i = 0;
         foreach ($nodeSchemaField as $nodeSchemaFieldUuid => $nodeSchemaFieldArray) {
             $this->data['NodeSchemaField'][$i] = $nodeSchemaFieldArray;
             unset($this->data['NodeSchemaField'][$nodeSchemaFieldUuid]);
             // don't need this uuid field anymore
             $i++;
         }
     }
     return true;
 }
Ejemplo n.º 9
0
 function checkEvents($events)
 {
     $toTrigger = array();
     if (!Set::numeric(array_keys($events))) {
         $events = array($events);
     }
     foreach ($events as $event) {
         if (empty($event[$this->alias])) {
             $event = array($this->alias => $event);
         }
         if (!empty($event[$this->alias]['time']) && strtotime($event[$this->alias]['time']) <= mktime()) {
             $toTrigger[] = $event;
         }
     }
     if (!empty($toTrigger)) {
         $this->triggerEvents($toTrigger);
     }
 }
 function search($artist)
 {
     $artist = strtolower($artist);
     //check for "The ...." pattern, switch to ..., The"
     if (stripos($artist, 'the ') === 0) {
         $artist = substr($artist, 4) . ', The';
     }
     $url = sprintf('%s/search', $this->url);
     $params = array('type' => 'artists', 'q' => $artist);
     $results = $this->__request($url, $params);
     if (!empty($results['Exactresults'])) {
         if (Set::numeric(array_keys($results['Exactresults']['Result']))) {
             return array_shift($results['Exactresults']['Result']);
         }
         return $results['Exactresults']['Result'];
     }
     return false;
 }
Ejemplo n.º 11
0
 function getResultFormat($model, &$results, $alias = null)
 {
     if (is_null($alias)) {
         $alias = $model->alias;
     }
     $oldFormat = array();
     $oldFormat['multiple'] = is_array($results) && Set::numeric(array_keys($results));
     if (!$oldFormat['multiple']) {
         $val = $results;
     } else {
         $val = reset($results);
     }
     $oldFormat['named'] = !empty($val) && (is_array($val) && isset($val[$alias]) || !empty($model->{key($val)}) && $model->{key($val)} instanceof Model);
     if (!$oldFormat['named']) {
         $val = array($alias => $val);
     }
     $oldFormat['idOnly'] = !empty($val[$alias]) && !is_array($val[$alias]) && is_numeric($val[$alias]);
     return $oldFormat;
 }
Ejemplo n.º 12
0
 function afterFind($data)
 {
     $data = $data['results'];
     $filteredData = array();
     $maxReplyId = 0;
     if ($data && Set::numeric(array_keys($data))) {
         foreach ($data as $i => $row) {
             $maxReplyId = max($maxReplyId, $row['id']);
             $options = array('callbacks' => false, 'action' => 'single', 'urlSuffix' => $row['id']);
             $item = parent::find('all', $options);
             if (!empty($item['in_reply_to_status_id']) && $item['in_reply_to_status_id'] == $this->replyProviderKey) {
                 $filteredData[] = array('Item' => array('provider_key' => $row['id'], 'text' => $row['text'], 'client' => $row['source'], 'link' => 'http://twitter.com/' . $row['from_user'] . '/status/' . $row['id'], 'reply_user_provider_key' => $this->parse($item, 'in_reply_to_user_id'), 'reply_status_provider_key' => $this->parse($item, 'in_reply_to_status_id'), 'posted' => date('Y-m-d H:i:s', strtotime($row['created_at']))), 'Origin' => array('provider_key' => $row['from_user_id'], 'name' => $row['from_user'], 'avatar' => $row['profile_image_url'], 'link' => 'http://twitter.com/' . $row['from_user']));
             }
         }
     }
     if ($maxReplyId > 0) {
         $filteredData[] = array('maxReplyId' => $maxReplyId);
     }
     return $filteredData;
 }
Ejemplo n.º 13
0
 /**
  * Creates all parameters on configuration using defaults, if not set.
  * The configuration is stored back in the Configure class.
  * 
  * @access public
  * @return boolean Return true if succefully nomalized, false, if not.
  */
 static function normalizeConfig()
 {
     $config = Configure::read('ContentStream');
     if (Configure::read() && (!is_array($config['streams']) || Set::numeric(array_keys($config['streams'])))) {
         return !trigger_error('CsConfigurator::normalizeConfig - ContentStream.streams must be a array indexed by type of content stream.');
     }
     $config['streams'] = Set::normalize($config['streams']);
     foreach ($config['streams'] as $type => &$stream) {
         if (!isset($stream['model'])) {
             $stream['model'] = Inflector::camelize($type) . '.' . Inflector::classify($type);
         }
         if (!isset($stream['controller'])) {
             $stream['controller'] = Inflector::pluralize($type);
         }
         if (!isset($stream['title'])) {
             $stream['title'] = Inflector::humanize($type);
         }
     }
     Configure::write('ContentStream', $config);
     return self::$nomalized = true;
 }
Ejemplo n.º 14
0
 function index()
 {
     $panels = array();
     foreach (Configure::read('Status.panels') as $panel => $options) {
         if (is_numeric($panel)) {
             $panel = $options;
             $options = array();
         }
         if (empty($options) || !Set::numeric(array_keys($options))) {
             $options = array($options);
         }
         if (strpos($panel, '.') !== false) {
             list($plugin, $panel) = explode('.', $panel);
         } else {
             $plugin = false;
         }
         foreach ($options as $option) {
             $panels[] = array('plugin' => $plugin, 'element' => $panel, 'options' => $option);
         }
     }
     $this->set('panels', $panels);
 }
Ejemplo n.º 15
0
 function makeItemXml($items)
 {
     $out = '';
     foreach ($items as $modelName => $model) {
         if (!empty($model)) {
             $tagName = Inflector::underscore($modelName);
             if ($tagName != 'data') {
                 $tagName = Inflector::singularize($tagName);
             }
             unset($model['internal']);
             if (!Set::numeric(array_keys($model))) {
                 $model = array($model);
             } else {
             }
             foreach ($model as $item) {
                 $attrib = array();
                 if (is_array($item)) {
                     $subItems = null;
                     unset($item['internal']);
                     if (!empty($item['items'])) {
                         $subItems = $item['items'];
                     }
                     unset($item['items']);
                     extract($this->dataSplitAttrib($item));
                     //debug($attrib );
                     $content = $this->makeItemXml($collections);
                     if (!empty($subItems)) {
                         $content .= $this->Xml->elem('items', null, $this->makeItemXml($subItems));
                     }
                 } else {
                     $content = $item;
                 }
                 $out .= $this->Xml->elem($tagName, $attrib, $content);
             }
         }
     }
     return $out;
 }
Ejemplo n.º 16
0
 function unifiedResult($model, $results, $alias = null, $format = array(), &$oldFormat = null)
 {
     $defFormat = array('multiple' => true, 'named' => true);
     $format = array_merge($defFormat, (array) $format);
     if (is_null($alias)) {
         $alias = $model->alias;
     }
     $oldFormat['multiple'] = Set::numeric(array_keys($results));
     if (!$oldFormat['multiple']) {
         $oldFormat['named'] = isset($results[$alias]);
         if ($oldFormat['named'] == $format['named']) {
             $uResults = $results;
         } elseif ($format['named']) {
             $uResults = array($alias => $results);
         } else {
             $uResults = $results[$alias];
         }
         if ($format['multiple']) {
             $uResults = array($uResults);
         }
     } else {
         $uResults = array();
         foreach ($results as $key => &$val) {
             $oldFormat['named'] = isset($val[$alias]);
             if ($oldFormat['named'] == $format['named']) {
                 $uResults[$key] = $val;
             } elseif ($format['named']) {
                 $uResults[$key][$alias] = $val;
             } else {
                 $uResults[$key] = $val[$alias];
             }
         }
         if (!$format['multiple']) {
             $uResults = $uResults[0];
         }
     }
     return $uResults;
 }
Ejemplo n.º 17
0
 function addItems($items)
 {
     //debug($items);
     if (!empty($items)) {
         foreach ($items as $modelName => $model) {
             App::import('Lib', 'SetMulti');
             if (!Set::numeric(array_keys(SetMulti::excludeKeys($model, array('internal'))))) {
                 $model = array($model);
             }
             if (array_key_exists('internal', $model)) {
                 if (empty($this->items[$modelName]['internal'])) {
                     $this->items[$modelName]['internal'] = array();
                 }
                 $this->items[$modelName]['internal'] = array_merge($this->items[$modelName]['internal'], $model['internal']);
                 unset($model['internal']);
             }
             foreach ($model as $item) {
                 $this->items[$modelName][] = $item;
             }
         }
         $this->controller->Link->addLoadedItems($items);
     }
 }
Ejemplo n.º 18
0
 /**
  * Serialize view vars.
  *
  * @param array $serialize The viewVars that need to be serialized.
  * @return string The serialized data
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
     if (is_array($serialize)) {
         $data = array($rootNode => array());
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             $data[$rootNode][$alias] = $this->viewVars[$key];
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Set::numeric(array_keys($data))) {
             $data = array($rootNode => array($serialize => $data));
         }
     }
     $options = array();
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     return Xml::fromArray($data, $options)->asXML();
 }
Ejemplo n.º 19
0
 function fullRef($ref)
 {
     if (is_array($ref)) {
         if (isset($ref['Node'])) {
             $ref = $ref['Node'];
         }
     } elseif (is_object($ref) && is_a($ref, 'Model')) {
         $ref = array('model' => $ref->name, 'foreign_key' => $ref->id);
     } elseif (!is_numeric($ref)) {
         if (strpos($ref, '/') !== false) {
             $ref = array('path' => $ref);
         } elseif (strpos($ref, ':') !== false) {
             //debug(explode(':', $ref,2));
             $ref = array_combine(array('model', 'foreign_key'), explode(':', $ref, 2));
         } else {
             $ref = array('alias' => $ref);
         }
     } else {
         $ref = array('id' => $ref);
     }
     if (empty($this->_refFields)) {
         $fields = $this->getFields(false);
         $fields[] = 'path';
         $this->_refFields = $fields;
     } else {
         $fields = $this->_refFields;
     }
     if (Set::numeric(array_keys($ref))) {
         //debug($ref);
         $ref = array('path' => $ref);
     } elseif (count($ref) == 1) {
         $key = key($ref);
         if (!in_array($key, $fields) && $key) {
             $ref = array('model' => $key, 'foreign_key' => $ref[$key]);
         }
     }
     if (!empty($ref['path'])) {
         if (!is_array($ref['path'])) {
             $ref['path'] = explode('/', $ref['path']);
         }
         foreach ($ref['path'] as $i => $subRef) {
             $ref['path'][$i] = $this->fullRef($subRef);
         }
     }
     $ref = array_intersect_key($ref, array_flip($fields));
     return $ref;
 }
 /**
  * Invoke the normalization method and populates the $settings property.
  * 
  * After populates the settings, with all used values (with default values when not set),
  * this method create one belongsTo relationship with CsContentStream model for each
  * ContentStream.
  * 
  * ### The options:
  * - 
  * 
  * After this method executed, the settings should be like:
  * {{{
  * 	[ModelAlias] => array(
  *		[streams] => array
  *			[one_foreign_key] => array(
  *				[assocName] => array(
  *				[allowedContents] => array(
  *					[content_type_one] => array(
  *						'model' => 'ContentPlugin.ContentModel',
  *						'title' => 'Content title',
  *					),
  *					[content_type_two] => array( ... ),
  *					.
  *					.
  *					.
  *				)
  *			),
  *			[another_foreign_key] => array(
  *				.
  *				.
  *				.
  *			)
  *		)
  *	)
  * }}}
  * 
  * @param model $Model The model where this behavior is attached
  * @param array $options Configuration options.
  * @access public
  */
 function setup(&$Model, $options)
 {
     if (!isset($options['streams']) || empty($options['streams']) || !is_array($options['streams']) || Set::numeric(array_keys($options['streams']))) {
         return !trigger_error('CsContentStreamHolderBehavior::setup - The `streams` parameter must be set on format: \'foreign_key\' => \'type\'');
     }
     $config = CsConfigurator::getConfig();
     $type = $have = $callbacks = $assocName = null;
     foreach ($options['streams'] as $fk => &$stream) {
         if (!is_array($stream)) {
             $stream = array('type' => $stream);
         }
         if (isset($options['allowedContents'])) {
             $stream['allowedContents'] = $options['allowedContents'];
         } elseif (isset($stream['type']) && is_string($stream['type']) && isset($config['types'][$stream['type']])) {
             $stream['allowedContents'] = $config['types'][$stream['type']];
         } else {
             return !trigger_error('CsContentStreamHolderBehavior - It must be set `allowedContents`(array) or `type`(string). In case of `type` set, it must be configured on content_stream plugin.');
         }
         foreach ($stream['allowedContents'] as $k => $allowedContent) {
             if (!is_string($allowedContent)) {
                 return !trigger_error('CsContentStreamHolderBehavior - Parameter allowed content must be an string.');
             } elseif (!isset($config['streams'][$allowedContent])) {
                 return !trigger_error('CsContentStreamHolderBehavior - Content type `' . $allowedContent . '` not known.');
             }
             $stream['allowedContents'][$allowedContent] = $config['streams'][$allowedContent];
             unset($stream['allowedContents'][$k]);
         }
         $stream['assocName'] = empty($stream['type']) ? Inflector::camelize($fk) : Inflector::camelize('cs_' . $stream['type']);
         $Model->belongsTo[$stream['assocName']] = array('className' => 'ContentStream.CsContentStream', 'foreignKey' => $fk);
     }
     $this->settings[$Model->alias] = $options;
     $Model->__createLinks();
     $this->runtime[$Model->alias] = array();
 }
Ejemplo n.º 21
0
 /**
  * testNumericArrayCheck method
  *
  * @see Hash test cases, as Set::numeric() is just a proxy.
  * @return void
  */
 public function testNumericArrayCheck()
 {
     $data = array('one');
     $this->assertTrue(Set::numeric(array_keys($data)));
 }
Ejemplo n.º 22
0
 /**
  * Generates the fields list of an SQL query.
  *
  * @param Model $model
  * @param string $alias Alias tablename
  * @param mixed $fields
  * @param boolean $quote If false, returns fields array unquoted
  * @return array
  * @access public
  */
 function fields(&$model, $alias = null, $fields = array(), $quote = true)
 {
     if (empty($alias)) {
         $alias = $model->alias;
     }
     $cacheKey = array($model->useDbConfig, $model->table, array_keys($model->schema()), $model->name, $model->getVirtualField(), $alias, $fields, $quote);
     $cacheKey = crc32(serialize($cacheKey));
     if ($return = $this->cacheMethod(__FUNCTION__, $cacheKey)) {
         return $return;
     }
     $allFields = empty($fields);
     if ($allFields) {
         $fields = array_keys($model->schema());
     } elseif (!is_array($fields)) {
         $fields = String::tokenize($fields);
     }
     $fields = array_values(array_filter($fields));
     $allFields = $allFields || in_array('*', $fields) || in_array($model->alias . '.*', $fields);
     $virtual = array();
     $virtualFields = $model->getVirtualField();
     if (!empty($virtualFields)) {
         $virtualKeys = array_keys($virtualFields);
         foreach ($virtualKeys as $field) {
             $virtualKeys[] = $model->alias . '.' . $field;
         }
         $virtual = $allFields ? $virtualKeys : array_intersect($virtualKeys, $fields);
         foreach ($virtual as $i => $field) {
             if (strpos($field, '.') !== false) {
                 $virtual[$i] = str_replace($model->alias . '.', '', $field);
             }
             $fields = array_diff($fields, array($field));
         }
         $fields = array_values($fields);
     }
     if (!$quote) {
         if (!empty($virtual)) {
             $fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
         }
         return $fields;
     }
     $count = count($fields);
     if ($count >= 1 && !in_array($fields[0], array('*', 'COUNT(*)'))) {
         for ($i = 0; $i < $count; $i++) {
             if (is_string($fields[$i]) && in_array($fields[$i], $virtual)) {
                 unset($fields[$i]);
                 continue;
             }
             if (is_object($fields[$i]) && isset($fields[$i]->type) && $fields[$i]->type === 'expression') {
                 $fields[$i] = $fields[$i]->value;
             } elseif (preg_match('/^\\(.*\\)\\s' . $this->alias . '.*/i', $fields[$i])) {
                 continue;
             } elseif (!preg_match('/^.+\\(.*\\)/', $fields[$i])) {
                 $prepend = '';
                 if (strpos($fields[$i], 'DISTINCT') !== false) {
                     $prepend = 'DISTINCT ';
                     $fields[$i] = trim(str_replace('DISTINCT', '', $fields[$i]));
                 }
                 $dot = strpos($fields[$i], '.');
                 if ($dot === false) {
                     $prefix = !(strpos($fields[$i], ' ') !== false || strpos($fields[$i], '(') !== false);
                     $fields[$i] = $this->name(($prefix ? $alias . '.' : '') . $fields[$i]);
                 } else {
                     $value = array();
                     $comma = strpos($fields[$i], ',');
                     if ($comma === false) {
                         $build = explode('.', $fields[$i]);
                         if (!Set::numeric($build)) {
                             $fields[$i] = $this->name(implode('.', $build));
                         }
                     }
                 }
                 $fields[$i] = $prepend . $fields[$i];
             } elseif (preg_match('/\\(([\\.\\w]+)\\)/', $fields[$i], $field)) {
                 if (isset($field[1])) {
                     if (strpos($field[1], '.') === false) {
                         $field[1] = $this->name($alias . '.' . $field[1]);
                     } else {
                         $field[0] = explode('.', $field[1]);
                         if (!Set::numeric($field[0])) {
                             $field[0] = implode('.', array_map(array(&$this, 'name'), $field[0]));
                             $fields[$i] = preg_replace('/\\(' . $field[1] . '\\)/', '(' . $field[0] . ')', $fields[$i], 1);
                         }
                     }
                 }
             }
         }
     }
     if (!empty($virtual)) {
         $fields = array_merge($fields, $this->_constructVirtualFields($model, $alias, $virtual));
     }
     return $this->cacheMethod(__FUNCTION__, $cacheKey, array_unique($fields));
 }
Ejemplo n.º 23
0
 /**
  * Backwards compatible passthrough method for:
  * saveMany(), validateMany(), saveAssociated() and validateAssociated()
  *
  * Saves multiple individual records for a single model; Also works with a single record, as well as
  * all its associated records.
  *
  * #### Options
  *
  * - validate: Set to false to disable validation, true to validate each record before saving,
  *   'first' to validate *all* records before any are saved (default),
  *   or 'only' to only validate the records, but not save them.
  * - atomic: If true (default), will attempt to save all records in a single transaction.
  *   Should be set to false if database/table does not support transactions.
  * - fieldList: Equivalent to the $fieldList parameter in Model::save()
  *
  * @param array $data Record data to save. This can be either a numerically-indexed array (for saving multiple
  *     records of the same type), or an array indexed by association name.
  * @param array $options Options to use when saving record data, See $options above.
  * @return mixed If atomic: True on success, or false on failure.
  *    Otherwise: array similar to the $data array passed, but values are set to true/false
  *    depending on whether each record saved successfully.
  * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
  * @link http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array
  */
 public function saveAll($data = null, $options = array())
 {
     $options = array_merge(array('validate' => 'first'), $options);
     if (Set::numeric(array_keys($data))) {
         if ($options['validate'] === 'only') {
             return $this->validateMany($data, $options);
         }
         return $this->saveMany($data, $options);
     }
     if ($options['validate'] === 'only') {
         $validatesAssoc = $this->validateAssociated($data, $options);
         if (isset($this->validationErrors[$this->alias]) && $this->validationErrors[$this->alias] === false) {
             return false;
         }
         return $validatesAssoc;
     }
     return $this->saveAssociated($data, $options);
 }
Ejemplo n.º 24
0
 /**
  * Saves multiple individual records for a single model; Also works with a single record, as well as
  * all its associated records.
  *
  * @param array $data Record data to save.  This can be either a numerically-indexed array (for saving multiple
  * 						records of the same type), or an array indexed by association name.
  * @param array $options Options to use when saving record data, which are as follows:
  * 							- validate: Set to false to disable validation, true to validate each record before
  * 							  saving, 'first' to validate *all* records before any are saved, or 'only' to only
  * 							  validate the records, but not save them.
  * 							- atomic: If true (default), will attempt to save all records in a single transaction.
  *							  Should be set to false if database/table does not support transactions.
  *								If false, we return an array similar to the $data array passed, but values are set to true/false
  *								depending on whether each record saved successfully.
  *							- fieldList: Equivalent to the $fieldList parameter in Model::save()
  * @return mixed True on success, or false on failure
  * @access public
  * @link http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo
  * @link http://book.cakephp.org/view/75/Saving-Your-Data
  */
 function saveAll($data = null, $options = array())
 {
     if (empty($data)) {
         $data = $this->data;
     }
     $db =& ConnectionManager::getDataSource($this->useDbConfig);
     $options = array_merge(array('validate' => true, 'atomic' => true), $options);
     $this->validationErrors = $validationErrors = array();
     $validates = true;
     $return = array();
     if ($options['atomic'] && $options['validate'] !== 'only') {
         $db->begin($this);
     }
     if (Set::numeric(array_keys($data))) {
         while ($validates) {
             foreach ($data as $key => $record) {
                 if (!($currentValidates = $this->__save($this, $record, $options))) {
                     $validationErrors[$key] = $this->validationErrors;
                 }
                 if ($options['validate'] === 'only' || $options['validate'] === 'first') {
                     $validating = true;
                     if ($options['atomic']) {
                         $validates = $validates && $currentValidates;
                     } else {
                         $validates = $currentValidates;
                     }
                 } else {
                     $validating = false;
                     $validates = $currentValidates;
                 }
                 if (!$options['atomic']) {
                     $return[] = $validates;
                 } elseif (!$validates && !$validating) {
                     break;
                 }
             }
             $this->validationErrors = $validationErrors;
             switch (true) {
                 case $options['validate'] === 'only':
                     return $options['atomic'] ? $validates : $return;
                     break;
                 case $options['validate'] === 'first':
                     $options['validate'] = true;
                     continue;
                     break;
                 default:
                     if ($options['atomic']) {
                         if ($validates && $db->commit($this) !== false) {
                             return true;
                         }
                         $db->rollback($this);
                         return false;
                     }
                     return $return;
                     break;
             }
         }
         return $return;
     }
     $associations = $this->getAssociated();
     while ($validates) {
         foreach ($data as $association => $values) {
             if (isset($associations[$association])) {
                 switch ($associations[$association]) {
                     case 'belongsTo':
                         if ($this->__save($this->{$association}, $values, $options)) {
                             $data[$this->alias][$this->belongsTo[$association]['foreignKey']] = $this->{$association}->id;
                             unset($data[$association]);
                         } else {
                             $validationErrors[$association] = $this->{$association}->validationErrors;
                             $validates = false;
                         }
                         if (!$options['atomic']) {
                             $return[$association][] = $validates;
                         }
                         break;
                 }
             }
         }
         if (!$this->__save($this, $data, $options)) {
             $validationErrors[$this->alias] = $this->validationErrors;
             $validates = false;
         }
         if (!$options['atomic']) {
             $return[$this->alias] = $validates;
         }
         $validating = $options['validate'] === 'only' || $options['validate'] === 'first';
         foreach ($data as $association => $values) {
             if (!$validates && !$validating) {
                 break;
             }
             if (isset($associations[$association])) {
                 $type = $associations[$association];
                 switch ($type) {
                     case 'hasOne':
                         $values[$this->{$type}[$association]['foreignKey']] = $this->id;
                         if (!$this->__save($this->{$association}, $values, $options)) {
                             $validationErrors[$association] = $this->{$association}->validationErrors;
                             $validates = false;
                         }
                         if (!$options['atomic']) {
                             $return[$association][] = $validates;
                         }
                         break;
                     case 'hasMany':
                         foreach ($values as $i => $value) {
                             $values[$i][$this->{$type}[$association]['foreignKey']] = $this->id;
                         }
                         $_options = array_merge($options, array('atomic' => false));
                         if ($_options['validate'] === 'first') {
                             $_options['validate'] = 'only';
                         }
                         $_return = $this->{$association}->saveAll($values, $_options);
                         if ($_return === false || is_array($_return) && in_array(false, $_return, true)) {
                             $validationErrors[$association] = $this->{$association}->validationErrors;
                             $validates = false;
                         }
                         if (is_array($_return)) {
                             foreach ($_return as $val) {
                                 if (!isset($return[$association])) {
                                     $return[$association] = array();
                                 } elseif (!is_array($return[$association])) {
                                     $return[$association] = array($return[$association]);
                                 }
                                 $return[$association][] = $val;
                             }
                         } else {
                             $return[$association] = $_return;
                         }
                         break;
                 }
             }
         }
         $this->validationErrors = $validationErrors;
         if (isset($validationErrors[$this->alias])) {
             $this->validationErrors = $validationErrors[$this->alias];
         }
         switch (true) {
             case $options['validate'] === 'only':
                 return $options['atomic'] ? $validates : $return;
                 break;
             case $options['validate'] === 'first':
                 $options['validate'] = true;
                 continue;
                 break;
             default:
                 if ($options['atomic']) {
                     if ($validates) {
                         return $db->commit($this) !== false;
                     } else {
                         $db->rollback($this);
                     }
                 }
                 return $return;
                 break;
         }
     }
 }
Ejemplo n.º 25
0
 function afterFind($results, $primary)
 {
     if (!empty($results)) {
         $doubleMulti = isset($results[0][$this->alias][0]);
         if ($doubleMulti) {
             $results = $results[0];
         }
         $test = $results;
         $rootnamed = isset($results[$this->alias]);
         if ($rootnamed) {
             $results = $results[$this->alias];
         }
         $multi = Set::numeric(array_keys($results));
         if (!$multi) {
             $results = array($results);
         }
         foreach ($results as $key => $box) {
             $boxnamed = isset($box[$this->alias]);
             if ($boxnamed) {
                 $box = $box[$this->alias];
             }
             if (isset($box["data"]) && empty($box["data"])) {
                 $box["data"] = array();
             }
             if (!empty($box["data"]) && !is_array($box["data"])) {
                 if (preg_match('/^{"/', $box["data"])) {
                     //old format
                     $box["data"] = $this->json_dec($box["data"]);
                 } else {
                     $box["data"] = unserialize($box["data"]);
                 }
                 if (!empty($box['data']['types'])) {
                     foreach ($box['data']['types'] as $field => $type) {
                         if (method_exists($this, '_' . $type . '_after')) {
                             $res = $this->{'_' . $type . '_after'}($box['data'][$field], $box);
                             if (!is_null($res)) {
                                 $box['data'][$field] = $res;
                             }
                         }
                     }
                 }
                 if (isset($box["data"]["file"])) {
                     $box["file"] = $box["data"]["file"];
                     unset($box["data"]["file"]);
                 }
             }
             $box['TemplateConfig'] = $this->getConfig($box);
             if (!empty($box['TemplateConfig'])) {
                 $res = $box['TemplateConfig']->afterFind($this, $box);
                 if (!empty($res)) {
                     $box = $res;
                 }
             }
             if ($boxnamed) {
                 $box = array($this->alias => $box);
             }
             $results[$key] = $box;
         }
         if (!$multi) {
             $results = $results[0];
         }
         if ($rootnamed) {
             $results = array($this->alias => $results);
         }
         if ($doubleMulti) {
             $results = array($results);
         }
     }
     //debug($test);
     //debug($results);
     return $results;
 }
Ejemplo n.º 26
0
 /**
  * Maps the given value as an object. If $value is an object,
  * it returns $value. Otherwise it maps $value as an object of
  * type $class, and identity $identity. If $value is not empty,
  * it will be used to set properties of returned object
  * (recursively).
  *
  * @param mixed $value Value to map
  * @param string $class Class name
  * @param string $identity Identity to assign to class
  * @return mixed Mapped object
  * @access private
  */
 function __map($value, $class, $identity = null)
 {
     if (is_object($value)) {
         return $value;
     }
     if (!empty($value) && Set::numeric(array_keys($value))) {
         $ret = array();
         foreach ($value as $key => $val) {
             $ret[$key] = Set::__map($val, $class);
         }
     } else {
         $ret = new $class();
         if ($identity != null) {
             $ret->__identity__ = $identity;
         }
     }
     if (empty($value)) {
         return $ret;
     }
     $keys = array_keys($value);
     foreach ($value as $key => $val) {
         if (!is_numeric($key) && strlen($key) > 1) {
             if ($key[0] == strtoupper($key[0]) && $key[1] == strtolower($key[1]) && (is_array($val) || is_object($val))) {
                 if ($key == $keys[0]) {
                     $ret = Set::__map($val, $class, $key);
                 } else {
                     $ret->{$key} = Set::__map($val, $class, $key);
                 }
             } else {
                 $ret->{$key} = $val;
             }
         }
     }
     return $ret;
 }
Ejemplo n.º 27
0
 /**
  * POST :user/:list_id/create_all
  *
  * Adds multiple members to a list, by specifying a comma-separated list of
  * member ids or screen names. The authenticated user must own the list to
  * be able to add members to it. Lists are limited to having 500 members,
  * and you are limited to adding up to 100 members to a list at a time with
  * this method.
  *
  * @param string $user
  * @param string $list_id
  * @param array  $params
  *  *Optional*
  *      user_id: A comma separated list of user IDs, up to 100 are allowed in a single request.
  *      screen_name: A comma separated list of screen names, up to 100 are allowed in a single request.
  *
  * @return array|false
  * @see http://dev.twitter.com/doc/post/:user/:list_id/create_all
  *
  * NOTE: http://groups.google.com/group/twitter-development-talk/browse_thread/thread/3e6ae4417160df39?pli=1
  *       now POST URL is https://api.twitter.com/1.1/:user/:list_id/members/create_all.json
  */
 public function post_list_members_create_all($user, $list_id, $params = array())
 {
     if (empty($user) || empty($list_id)) {
         return false;
     }
     // if $params = ('username1', 'username2', 'username3')
     if (!empty($params) && Set::numeric(array_keys($params))) {
         if (is_numeric($params[0])) {
             $params = array('user_id' => $params);
         } else {
             $params = array('screen_name' => $params);
         }
     }
     foreach (array('user_id', 'screen_name') as $key) {
         if (!empty($params[$key]) && is_array($params[$key])) {
             $params[$key] = join(',', $params[$key]);
         }
     }
     $url = sprintf('https://api.twitter.com/1.1/%s/%s/members/create_all.json', $user, $list_id);
     $method = 'POST';
     // request
     return $this->_request($this->_buildRequest($url, $method, $params));
 }
Ejemplo n.º 28
0
 /**
  * Returns ids from searchCriteria or false if there's other criteria involved
  *
  * @param array $searchCriteria
  *
  * @return false or array
  */
 protected function _uidsByCriteria($searchCriteria)
 {
     if (is_numeric($searchCriteria) || Set::numeric($searchCriteria)) {
         // We already know the id, or list of ids
         $results = $searchCriteria;
         if (!is_array($results)) {
             $results = array($results);
         }
         return $results;
     }
     return false;
 }
Ejemplo n.º 29
0
 /**
  * Implements partial support for XPath 2.0. If $path does not contain a '/' the call
  * is delegated to Set::classicExtract(). Also the $path and $data arguments are
  * reversible.
  *
  * #### Currently implemented selectors:
  *
  * - /User/id (similar to the classic {n}.User.id)
  * - /User[2]/name (selects the name of the second User)
  * - /User[id>2] (selects all Users with an id > 2)
  * - /User[id>2][<5] (selects all Users with an id > 2 but < 5)
  * - /Post/Comment[author_name=john]/../name (Selects the name of all Posts that have at least one Comment written by john)
  * - /Posts[name] (Selects all Posts that have a 'name' key)
  * - /Comment/.[1] (Selects the contents of the first comment)
  * - /Comment/.[:last] (Selects the last comment)
  * - /Comment/.[:first] (Selects the first comment)
  * - /Comment[text=/cakephp/i] (Selects the all comments that have a text matching the regex /cakephp/i)
  * - /Comment/@* (Selects the all key names of all comments)
  *
  * #### Other limitations:
  *
  * - Only absolute paths starting with a single '/' are supported right now
  *
  * **Warning**: Even so it has plenty of unit tests the XPath support has not gone through a lot of
  * real-world testing. Please report Bugs as you find them. Suggestions for additional features to
  * implement are also very welcome!
  *
  * @param string $path An absolute XPath 2.0 path
  * @param array $data An array of data to extract from
  * @param array $options Currently only supports 'flatten' which can be disabled for higher XPath-ness
  * @return array An array of matched items
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::extract
  */
 public static function extract($path, $data = null, $options = array())
 {
     if (is_string($data)) {
         $tmp = $data;
         $data = $path;
         $path = $tmp;
     }
     if (strpos($path, '/') === false) {
         return Set::classicExtract($data, $path);
     }
     if (empty($data)) {
         return array();
     }
     if ($path === '/') {
         return $data;
     }
     $contexts = $data;
     $options = array_merge(array('flatten' => true), $options);
     if (!isset($contexts[0])) {
         $current = current($data);
         if (is_array($current) && count($data) < 1 || !is_array($current) || !Set::numeric(array_keys($data))) {
             $contexts = array($data);
         }
     }
     $tokens = array_slice(preg_split('/(?<!=|\\\\)\\/(?![a-z-\\s]*\\])/', $path), 1);
     do {
         $token = array_shift($tokens);
         $conditions = false;
         if (preg_match_all('/\\[([^=]+=\\/[^\\/]+\\/|[^\\]]+)\\]/', $token, $m)) {
             $conditions = $m[1];
             $token = substr($token, 0, strpos($token, '['));
         }
         $matches = array();
         foreach ($contexts as $key => $context) {
             if (!isset($context['trace'])) {
                 $context = array('trace' => array(null), 'item' => $context, 'key' => $key);
             }
             if ($token === '..') {
                 if (count($context['trace']) == 1) {
                     $context['trace'][] = $context['key'];
                 }
                 $parent = implode('/', $context['trace']) . '/.';
                 $context['item'] = Set::extract($parent, $data);
                 $context['key'] = array_pop($context['trace']);
                 if (isset($context['trace'][1]) && $context['trace'][1] > 0) {
                     $context['item'] = $context['item'][0];
                 } elseif (!empty($context['item'][$key])) {
                     $context['item'] = $context['item'][$key];
                 } else {
                     $context['item'] = array_shift($context['item']);
                 }
                 $matches[] = $context;
                 continue;
             }
             if ($token === '@*' && is_array($context['item'])) {
                 $matches[] = array('trace' => array_merge($context['trace'], (array) $key), 'key' => $key, 'item' => array_keys($context['item']));
             } elseif (is_array($context['item']) && array_key_exists($token, $context['item']) && !(strval($key) === strval($token) && count($tokens) == 1 && $tokens[0] === '.')) {
                 $items = $context['item'][$token];
                 if (!is_array($items)) {
                     $items = array($items);
                 } elseif (!isset($items[0])) {
                     $current = current($items);
                     $currentKey = key($items);
                     if (!is_array($current) || is_array($current) && count($items) <= 1 && !is_numeric($currentKey)) {
                         $items = array($items);
                     }
                 }
                 foreach ($items as $key => $item) {
                     $ctext = array($context['key']);
                     if (!is_numeric($key)) {
                         $ctext[] = $token;
                         $tok = array_shift($tokens);
                         if (isset($items[$tok])) {
                             $ctext[] = $tok;
                             $item = $items[$tok];
                             $matches[] = array('trace' => array_merge($context['trace'], $ctext), 'key' => $tok, 'item' => $item);
                             break;
                         } elseif ($tok !== null) {
                             array_unshift($tokens, $tok);
                         }
                     } else {
                         $key = $token;
                     }
                     $matches[] = array('trace' => array_merge($context['trace'], $ctext), 'key' => $key, 'item' => $item);
                 }
             } elseif ($key === $token || ctype_digit($token) && $key == $token || $token === '.') {
                 $context['trace'][] = $key;
                 $matches[] = array('trace' => $context['trace'], 'key' => $key, 'item' => $context['item']);
             }
         }
         if ($conditions) {
             foreach ($conditions as $condition) {
                 $filtered = array();
                 $length = count($matches);
                 foreach ($matches as $i => $match) {
                     if (Set::matches(array($condition), $match['item'], $i + 1, $length)) {
                         $filtered[$i] = $match;
                     }
                 }
                 $matches = $filtered;
             }
         }
         $contexts = $matches;
         if (empty($tokens)) {
             break;
         }
     } while (1);
     $r = array();
     foreach ($matches as $match) {
         if ((!$options['flatten'] || is_array($match['item'])) && !is_int($match['key'])) {
             $r[] = array($match['key'] => $match['item']);
         } else {
             $r[] = $match['item'];
         }
     }
     return $r;
 }
Ejemplo n.º 30
0
 /**
  * testNumericArrayCheck method
  *
  * @access public
  * @return void
  */
 function testNumericArrayCheck()
 {
     $data = array('one');
     $this->assertTrue(Set::numeric(array_keys($data)));
     $data = array(1 => 'one');
     $this->assertFalse(Set::numeric($data));
     $data = array('one');
     $this->assertFalse(Set::numeric($data));
     $data = array('one' => 'two');
     $this->assertFalse(Set::numeric($data));
     $data = array('one' => 1);
     $this->assertTrue(Set::numeric($data));
     $data = array(0);
     $this->assertTrue(Set::numeric($data));
     $data = array('one', 'two', 'three', 'four', 'five');
     $this->assertTrue(Set::numeric(array_keys($data)));
     $data = array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
     $this->assertTrue(Set::numeric(array_keys($data)));
     $data = array('1' => 'one', 2 => 'two', 3 => 'three', 4 => 'four', 5 => 'five');
     $this->assertTrue(Set::numeric(array_keys($data)));
     $data = array('one', 2 => 'two', 3 => 'three', 4 => 'four', 'a' => 'five');
     $this->assertFalse(Set::numeric(array_keys($data)));
 }