Object::doSearch PHP Method

doSearch() public method

Do Search Object based on its status
public doSearch ( type $type ) : CActiveDataProvider
$type type
return CActiveDataProvider
    public function doSearch($type = 0)
    {
        $criteria = new CDbCriteria();
        $sort = new CSort();
        $sort->attributes = array('object_id');
        $sort->defaultOrder = 'object_id DESC';
        switch ($type) {
            //If looking for DRAFT Content
            case ConstantDefine::OBJECT_STATUS_DRAFT:
                $criteria->condition = 'object_status = :status and object_author = :uid';
                $criteria->params = array(':status' => ConstantDefine::OBJECT_STATUS_DRAFT, ':uid' => user()->id);
                break;
                //If looking for Pending Content
            //If looking for Pending Content
            case ConstantDefine::OBJECT_STATUS_PENDING:
                $criteria->select = " t.*";
                $criteria->distinct = true;
                $current_user_roles = Rights::getAssignedRoles(user()->id, true);
                foreach ($current_user_roles as $r) {
                    $user_roles_allow[] = $r->name;
                }
                $criteria->join = "JOIN gxc_transfer as tr ON t.object_id = tr.object_id\n\t\t\tLEFT OUTER JOIN gxc_transfer tr2 ON (t.object_id = tr2.object_id AND \n\t\t\t(tr.time < tr2.time OR tr.time = tr2.time AND tr.transfer_id < tr2.transfer_id))";
                $criteria->condition = 'object_status = :status and tr2.transfer_id IS NULL and
			(( tr.type= :type_person and tr.to_user_id = :toperson ) or (
			   tr.type= :type_role and tr.note in ( "' . implode(",", $user_roles_allow) . '" )
			) )
			';
                $criteria->params = array(':status' => ConstantDefine::OBJECT_STATUS_PENDING, ':toperson' => user()->id, ':type_person' => ConstantDefine::TRANS_PERSON, ':type_role' => ConstantDefine::TRANS_ROLE);
                break;
                //If looking for Published Content
            //If looking for Published Content
            case ConstantDefine::OBJECT_STATUS_PUBLISHED:
                //Do nothing;
                $criteria->condition = 'object_status = :status';
                $criteria->params = array(':status' => ConstantDefine::OBJECT_STATUS_PUBLISHED);
                break;
        }
        $criteria->compare('object_id', $this->object_id, true);
        $criteria->compare('object_author', $this->object_author, true);
        $criteria->compare('object_date', $this->object_date);
        $criteria->compare('object_content', $this->object_content, true);
        $criteria->compare('object_title', $this->object_title, true);
        $criteria->compare('object_name', $this->object_name, true);
        return new CActiveDataProvider(get_class($this), array('criteria' => $criteria, 'sort' => $sort));
    }

Usage Example

 public function doAdminSearch($type = 0)
 {
     $result = null;
     switch ($type) {
         case ConstantDefine::OBJECT_STATUS_DRAFT:
             $model = new Object('draft');
             break;
         case ConstantDefine::OBJECT_STATUS_PENDING:
             $model = new Object('pending');
             break;
         case ConstantDefine::OBJECT_STATUS_PUBLISHED:
             $model = new Object('published');
             break;
         default:
             $model = new Object('search');
             break;
     }
     $result = $model->doSearch($type);
     $model->unsetAttributes();
     if (isset($_GET['Object'])) {
         $model->attributes = $_GET['Object'];
     }
     $result = $model->doSearch($type);
     $this->render('cmswidgets.views.object.object_manage_widget', array('model' => $model, 'result' => $result));
 }