protected function buildFrom() {
     $this->_selectBuilder->from->add(Pap_Db_Table_DirectLinkUrls::getName(), "l");
     $this->_selectBuilder->from->addInnerJoin(Pap_Db_Table_Users::getName(), "pu", "l.userid = pu.userid");
     $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_Users::getName(), "gu", "pu.accountuserid = gu.accountuserid");
     $this->_selectBuilder->from->addInnerJoin(Gpf_Db_Table_AuthUsers::getName(), "au", "gu.authid = au.authid");        
     $this->_selectBuilder->from->addLeftJoin(Pap_Db_Table_Channels::getName(), "ch", "ch.channelid = l.channelid");
     $this->_selectBuilder->from->addLeftJoin(Pap_Db_Table_Banners::getName(), "b", "b.bannerid = l.bannerid");
     $this->_selectBuilder->from->addLeftJoin(Pap_Db_Table_Campaigns::getName(), "c", "c.campaignid = l.campaignid");
 }
    public function getPendingDirectLinksCount() {
        $select = new Gpf_SqlBuilder_SelectBuilder();
        $select->select->add('l.'.Pap_Db_Table_DirectLinkUrls::ID);
        $select->from->add(Pap_Db_Table_DirectLinkUrls::getName(), 'l');
        $select->from->addLeftJoin(Pap_Db_Table_Banners::getName(), 'b', 'b.'.Pap_Db_Table_Banners::ID.' = l.'.Pap_Db_Table_DirectLinkUrls::BANNER_ID);
        $select->from->addLeftJoin(Pap_Db_Table_Campaigns::getName(), 'c', 'c.'.Pap_Db_Table_Campaigns::ID.' = l.'.Pap_Db_Table_DirectLinkUrls::CAMPAIGN_ID);
        $select->where->add('l.'.Pap_Db_Table_DirectLinkUrls::STATUS, "=", self::PENDING);

        Gpf_Plugins_Engine::extensionPoint('AffiliateNetwork.directLinksModifyWhere', $select);

        $result = $select->getAllRows();
        
        return $result->getSize();
    }
    public function checkUserApprovedDirectLinks($userId) {
        $result = new Gpf_Data_RecordSet();
        $selectBuilder = new Gpf_SqlBuilder_SelectBuilder();
        $selectBuilder->select->add('COUNT('.Pap_Db_Table_DirectLinkUrls::ID.')', 'count');
        $selectBuilder->from->add(Pap_Db_Table_DirectLinkUrls::getName());
        $selectBuilder->where->add(Pap_Db_Table_DirectLinkUrls::USER_ID, '=', $userId);
        $selectBuilder->where->add(Pap_Db_Table_DirectLinkUrls::STATUS, '=', Pap_Common_Constants::STATUS_APPROVED);

        $result->load($selectBuilder);

        if($result->getSize() == 0) {
            return false;
        }

        foreach($result as $record) {
            if($record->get('count') > 0) {
                return true;
            }
            break;
        }

        return false;
    }
    public function regenerateDirectLinksFile() {
    	$rs = new Gpf_Data_RecordSet();
        $selectBuilder = new Gpf_SqlBuilder_SelectBuilder();
        $selectBuilder->select->add('userid', 'userid');
        $selectBuilder->select->add('url', 'url');
        $selectBuilder->select->add('channelid', 'channelid');
        $selectBuilder->select->add('campaignid', 'campaignid');
        $selectBuilder->select->add('bannerid', 'bannerid');
        $selectBuilder->from->add(Pap_Db_Table_DirectLinkUrls::getName()); 
       	$selectBuilder->where->add('rstatus', '=', Pap_Common_Constants::STATUS_APPROVED);
		$selectBuilder->orderBy->add(Pap_Db_Table_DirectLinkUrls::MATCHES, false);

        $rs->load($selectBuilder);

    	$this->createTemporaryFile();

    	foreach($rs as $record) {
    		$this->addUrl($record->get('userid'), $record->get('url'),
    						$record->get('channelid'),
    						$record->get('campaignid'),
    						$record->get('bannerid'));
    	}
    	$this->commitTemporaryFile();
    }
 public static function getInstance() {
     if(self::$instance === null) {
         self::$instance = new self;
     }
     return self::$instance;
 }
    /**
     *
     * @param $params
     * @return Gpf_Rpc_Action
     */
    private function saveChangedStatusToDB(Gpf_Rpc_Params $params) {
        $action = new Gpf_Rpc_Action($params);
        $action->setInfoMessage($this->_("Status successfully set to selected url(s)"));
        $action->setErrorMessage($this->_("Failed to set status selected url(s)"));

        $update = new Gpf_SqlBuilder_UpdateBuilder();
        $update->from->add(Pap_Db_Table_DirectLinkUrls::getName());
        $update->set->add(Pap_Db_Table_DirectLinkUrls::STATUS, $action->getParam("status"));

        foreach ($action->getIds() as $id){
            $update->where->add(Pap_Db_Table_DirectLinkUrls::ID, "=", $id, "OR");
        }

        try {
            $update->execute();
            $action->addOk();

            Pap_Tracking_DirectLinksBase::getInstance()->regenerateDirectLinksFile();
        } catch(Gpf_DbEngine_NoRowException $e) {
            $action->addError();
        }
        return $action;
    }