Example #1
0
 /**
  * Retrieves a list of models based on the current search/filter conditions.
  *
  * Typical usecase:
  * - Initialize the model fields with values from filter form.
  * - Execute this method to get CActiveDataProvider instance which will filter
  * models according to data in model fields.
  * - Pass data provider to CGridView, CListView or any similar widget.
  *
  * @return CActiveDataProvider the data provider that can return the models
  * based on the search/filter conditions.
  */
 public function search(&$columns = array())
 {
     // @todo Please modify the following code to remove attributes that should not be searched.
     $criteria = new CDbCriteria();
     $criteria->order = 't.date';
     $criteria->with = array('user', 'user.country', 'user.province', 'user.city', 'competition');
     $criteria->compare('t.id', $this->id, true);
     $criteria->compare('t.competition_id', $this->competition_id);
     $criteria->compare('t.user_id', $this->user_id);
     $criteria->compare('t.events', $this->events, true);
     $criteria->compare('t.comments', $this->comments, true);
     $criteria->compare('t.date', $this->date, true);
     $criteria->compare('t.status', $this->status);
     $criteria->compare('user.status', User::STATUS_NORMAL);
     $registrations = $this->findAll($criteria);
     $number = 1;
     $statistics = array();
     $statistics['number'] = 0;
     $statistics['new'] = 0;
     $statistics['paid'] = 0;
     $statistics['unpaid'] = 0;
     $statistics[User::GENDER_MALE] = 0;
     $statistics[User::GENDER_FEMALE] = 0;
     //detect sort attribute
     $sort = Yii::app()->controller->sGet('sort');
     $sort = explode('.', $sort);
     if (isset($sort[1]) && $sort[1] === 'desc') {
         self::$sortDesc = true;
     }
     $sort = $sort[0];
     if ($sort !== '') {
         switch ($sort) {
             case 'name':
             case 'gender':
             case 'country_id':
             case 'birthday':
             case 'email':
             case 'mobile':
                 self::$sortByUserAttribute = true;
             case 'number':
             case 'user_id':
             case 'location_id':
                 self::$sortAttribute = $sort;
                 break;
             default:
                 self::$sortByEvent = true;
                 self::$sortAttribute = $sort;
                 break;
         }
     }
     $wcaIds = array();
     foreach ($registrations as $registration) {
         if ($registration->isAccepted()) {
             $registration->number = $number++;
         }
         $statistics['number']++;
         $statistics[$registration->user->gender]++;
         if ($registration->user->wcaid === '') {
             $statistics['new']++;
         }
         foreach ($registration->events as $event) {
             if (!isset($statistics[$event])) {
                 $statistics[$event] = 0;
             }
             $statistics[$event]++;
         }
         $fee = $registration->getTotalFee();
         if ($registration->isPaid()) {
             $statistics['paid'] += $fee;
         } else {
             $statistics['unpaid'] += $fee;
         }
         //store wcaids
         if ($registration->user->wcaid) {
             $wcaIds[$registration->user->wcaid] = $registration;
         }
     }
     if (self::$sortByEvent === true && !empty($wcaIds)) {
         switch ($sort) {
             case '333bf':
             case '444bf':
             case '555bf':
             case '333mbf':
                 $modelName = 'RanksSingle';
                 break;
             default:
                 $modelName = 'RanksAverage';
                 break;
         }
         $results = $modelName::model()->findAllByAttributes(array('eventId' => $sort, 'personId' => array_keys($wcaIds)));
         foreach ($results as $result) {
             $wcaIds[$result->personId]->best = $result->best;
         }
     }
     $statistics['gender'] = $statistics[User::GENDER_MALE] . '/' . $statistics[User::GENDER_FEMALE];
     $statistics['old'] = $statistics['number'] - $statistics['new'];
     $statistics['name'] = $statistics['new'] . '/' . $statistics['old'];
     $statistics['fee'] = $statistics['paid'] . '/' . $statistics['unpaid'];
     foreach ($columns as $key => $column) {
         if (isset($column['name']) && isset($statistics[$column['name']])) {
             $columns[$key]['footer'] = $statistics[$column['name']];
         }
     }
     if ($sort !== '') {
         usort($registrations, array($this, 'sortRegistration'));
         if (count($registrations) > 0 && self::$sortByEvent === true && self::$sortDesc !== true) {
             $best = $registrations[0]->best;
             $pos = 1;
             foreach ($registrations as $i => $registration) {
                 if ($registration->best < 0) {
                     break;
                 }
                 if ($registration->best > $best) {
                     $best = $registration->best;
                     $pos = $i + 1;
                 }
                 $registration->pos = $pos;
             }
         }
         if (self::$sortDesc === true && self::$sortByEvent !== true) {
             $registrations = array_reverse($registrations);
         }
     }
     return new NonSortArrayDataProvider($registrations, array('sort' => $this->getSort($columns), 'pagination' => false));
 }