function set_sort_order($direction) { switch ($direction) { case 'asc': $this->contents = __::sortBy($this->contents, function ($block) { return -strtotime($block->connected_at); }); break; case 'desc': $this->contents = __::sortBy($this->contents, function ($block) { return strtotime($block->connected_at); }); break; case 'position': $this->contents = __::sortBy($this->contents, function ($block) { return $block->position; }); break; } }
$projectFeedIds[] = $project->feed_id; } } $criteria = new CDbCriteria(); $criteria->addColumnCondition(array('type' => Post::RESOURCE)); if (null !== $resource_type) { $criteria->addColumnCondition(array('resource_type' => $resource_type)); } $criteria->addInCondition('feed_id', $projectFeedIds); $projectResources = Post::model()->findAll($criteria); $projectResources = __::map($projectResources, function ($post) { return array('post' => $post, 'scope' => 'project'); }); // All posts $resources = __::sortBy(array_merge($regionResources, $locationResources, $projectResources), function ($post) { return -strtotime($post['post']->creation_time); }); // Render the view $title = '<a href="' . $this->createUrl('list', array('region' => $region->id)) . '">'; $title .= Yii::t('app', 'Resources'); $title .= '</a>'; $title .= 'skill' === $resource_type ? '<a class="type selected" ' : '<a class="type" '; $title .= 'href="' . $this->createUrl('list', array('region' => $region->id, 'type' => 'skill')) . '">'; $title .= Yii::t('app', 'Skills'); $title .= '</a>'; $title .= 'material' === $resource_type ? '<a class="type selected" ' : '<a class="type" '; $title .= 'href="' . $this->createUrl('list', array('region' => $region->id, 'type' => 'material')) . '">'; $title .= Yii::t('app', 'Materials'); $title .= '</a>'; $title .= 'tool' === $resource_type ? '<a class="type selected" ' : '<a class="type" '; $title .= 'href="' . $this->createUrl('list', array('region' => $region->id, 'type' => 'tool')) . '">';
public function actionSearch($query) { $query = preg_replace('/[^\\p{L}]+/u', ' ', $query); $query = trim($query); $query = preg_replace('/\\s+/', ' ', $query); if (3 > strlen($query)) { $this->render('search', array('query' => $query, 'errorMessage' => Yii::t('app', 'The search string must be atleast three characters long.'))); Yii::app()->end(); } $limit = 100; $results = array(); // Users $criteria = new CDbCriteria(); $criteria->addSearchCondition('first_name', $query, true, 'OR'); $criteria->addSearchCondition('last_name', $query, true, 'OR'); $criteria->addSearchCondition('description', $query, true, 'OR'); $criteria->order = 'registration_time'; $criteria->limit = $limit; $users = User::model()->findAll($criteria); foreach ($users as $user) { $results[] = array('name' => Yii::t('app', '{userName} – Registered @ {registrationTime}', array('{userName}' => $user->full_name, '{registrationTime}' => $user->registration_time)), 'description' => $user->description, 'url' => $this->createUrl('//user/view', array('id' => $user->id)), 'time' => $user->registration_time); } // Locations $criteria = new CDbCriteria(); $criteria->addSearchCondition('name', $query, true, 'OR'); $criteria->addSearchCondition('description', $query, true, 'OR'); $criteria->order = 'creation_time'; $criteria->limit = $limit; $locations = Location::model()->findAll($criteria); foreach ($locations as $location) { $nameParams = array('{placeName}' => $location->name, '{creationTime}' => $location->creation_time); if ($location->createdByUser) { $nameParams['{userName}'] = $location->createdByUser->full_name; $name = Yii::t('app', '{placeName} tagged by {userName} @ {creationTime}', $nameParams); } else { $name = Yii::t('app', '{placeName} tagged @ {creationTime}', $nameParams); } $results[] = array('name' => $name, 'description' => $location->description, 'url' => $this->createUrl('//location/view', array('id' => $location->id)), 'time' => $location->creation_time); } // Projects $criteria = new CDbCriteria(); $criteria->addSearchCondition('name', $query, true, 'OR'); $criteria->addSearchCondition('description', $query, true, 'OR'); $criteria->order = 'creation_time'; $criteria->limit = $limit; $projects = Project::model()->findAll($criteria); foreach ($projects as $project) { $results[] = array('name' => $project->name, 'description' => $project->description, 'url' => $this->createUrl('//location/view', array('id' => $project->location->id, '#' => $project->slug)), 'time' => $project->creation_time); } // Posts $criteria = new CDbCriteria(); $criteria->addSearchCondition('text', $query, true, 'OR'); $criteria->order = 'creation_time'; $criteria->limit = $limit; $posts = Post::model()->findAll($criteria); foreach ($posts as $post) { $nameParams = array('{userName}' => $post->user->full_name, '{creationTime}' => $post->creation_time); if ($post->feed->region) { $url = $this->createUrl('/resource/list', array('id' => $post->feed->region->id, '#' => 'post-' . $post->id)); $nameParams['{feedName}'] = $post->feed->region->name; } else { if ($post->feed->location) { $url = $this->createUrl('/location/view', array('id' => $post->feed->location->id, '#' => 'post-' . $post->id)); $nameParams['{feedName}'] = $post->feed->location->name; } else { if ($post->feed->project) { $url = $this->createUrl('/location/view', array('id' => $post->feed->project->location->id, '#' => 'post-' . $post->id)); $nameParams['{feedName}'] = $post->feed->project->name; } else { continue; } } } if (Post::TEXT === $post->type) { $name = Yii::t('app', 'Posted to {feedName} by {userName} @ {creationTime}', $nameParams); } else { if (Post::IMAGE == $post->type) { $name = Yii::t('app', 'Image added to {feedName} by {userName} @ {creationTime}', $nameParams); } else { if (Post::RESOURCE === $post->type) { $nameParams['{resourceType}'] = ucfirst(Yii::t('app', $post->resource_type)); $name = Yii::t('app', '{resourceType} added to {feedName} by {userName} @ {creationTime}', $nameParams); } else { continue; } } } $results[] = array('name' => $name, 'description' => $post->text, 'url' => $url, 'time' => $post->creation_time); } // Sort by $results = __::sortBy($results, function ($result) { return -strtotime($result['time']); }); $this->render('search', array('query' => $query, 'results' => $results)); }
public function testSortBy() { // from js $people = array((object) array('name' => 'curly', 'age' => 50), (object) array('name' => 'moe', 'age' => 30)); $people_sorted = __::sortBy($people, function ($person) { return $person->age; }); $this->assertEquals(array('moe', 'curly'), __::pluck($people_sorted, 'name'), 'stooges sorted by age'); // extra $stooges = array(array('name' => 'moe', 'age' => 40), array('name' => 'larry', 'age' => 50), array('name' => 'curly', 'age' => 60)); $this->assertEquals($stooges, __::sortBy($stooges, function ($stooge) { return $stooge['age']; })); $this->assertEquals(array($stooges[2], $stooges[1], $stooges[0]), __::sortBy($stooges, function ($stooge) { return $stooge['name']; })); $this->assertEquals(array(5, 4, 6, 3, 1, 2), __::sortBy(array(1, 2, 3, 4, 5, 6), function ($num) { return sin($num); })); $this->assertEquals($stooges, __($stooges)->sortBy(function ($stooge) { return $stooge['age']; }), 'works with OO-style call'); // docs $this->assertEquals(array(3, 2, 1), __::sortBy(array(1, 2, 3), function ($n) { return -$n; })); }