public function actionRecordAndForward()
 {
     $CampaignContact2Outcome = CampaignContact2Outcome::model()->find(array('with' => array('campaign_outcome', 'campaign_contact' => array('with' => 'campaign')), 'condition' => "\n\t\t\t\t`t`.`id` = :campaign_contact2outcome_id\n\t\t\t\tAND `t`.`hash` = :campaign_contact2outcome_hash\n\t\t\t\tAND `campaign`.`id` = :campaign_id\n\t\t\t\tAND `campaign`.`status` != :campaign_not_run\n\t\t\t\tAND `campaign`.`hash` = :campaign_hash\n\t\t\t\tAND LENGTH(campaign_outcome.url) > 0\n\t\t\t", 'params' => array('campaign_contact2outcome_id' => $_GET['campaign_contact2outcome_id'], 'campaign_contact2outcome_hash' => $_GET['campaign_contact2outcome_hash'], 'campaign_id' => $_GET['campaign_id'], 'campaign_not_run' => Campaign::STATUS_NOT_RUN, 'campaign_hash' => $_GET['campaign_hash'])));
     if (is_null($CampaignContact2Outcome)) {
         // fail. we don't know where to send them. What do we do?
         throw new CHttpException('404', 'We can\'t find that page');
     } else {
         // success. Mark as success with a date not an int.
         $CampaignContact2Outcome->outcome = date("Y-m-d H:i:s");
         $CampaignContact2Outcome->save(false, array('outcome'));
         // now forward them
         $this->redirect($CampaignContact2Outcome->campaign_outcome->url);
     }
 }
Esempio n. 2
0
 public function actionUploadOutcome($id)
 {
     // ensure csv line endings are correctly recognised.
     ini_set('auto_detect_line_endings', true);
     $Campaign = Campaign::model()->with(array('outcomes' => array('index' => 'id')))->findByPk($id);
     if (!sizeof($Campaign->outcomes)) {
         Yii::app()->user->setFlash('Warning', 'This campaign has no outcomes and as such cannot be updated via file upload.');
     }
     // check for upload file
     if (sizeof($Campaign->outcomes) && sizeof($_FILES['file']) && strlen($_FILES['file']['tmp_name'])) {
         // confirm the organisation.
         if (Yii::app()->user->role < User::ROLE_MANAGER) {
             $organisation_id = Yii::app()->user->organisation_id;
         } else {
             // should have one in post.
             if (!is_numeric($_POST['organisation_id'])) {
                 throw new CHttpException('400', 'Bad Request - missing organisation_id');
             }
             $organisation_id = (int) $_POST['organisation_id'];
         }
         $Organisation = Organisation::model()->findByPk($organisation_id);
         // check outcome belongs to campaign
         if (!array_key_exists($_POST['outcome_id'], $Campaign->outcomes)) {
             // not a valid outcome
             throw new CHttpException('400', 'Bad Request - invalid outcome.');
         }
         //get the csv file
         $fh = fopen($_FILES['file']['tmp_name'], "r");
         // store number of successes so we can tell rows and individual columns;
         $successes = [];
         //loop through the csv file and gather unique ids
         for ($lines = 0; $data = fgetcsv($fh); $lines++) {
             if (strlen($data[0])) {
                 $uniqueIDs[] = $data[0];
             }
         }
         if (!sizeof($uniqueIDs)) {
             exit('no uniques');
         }
         $CDbCriteria = new CDbCriteria();
         $CDbCriteria->join = 'INNER JOIN store2contact ON `t`.warehouse_id = `store2contact`.`contact_warehouse_id`';
         $CDbCriteria->addCondition('`t`.`campaign_id` = :campaign_id');
         $CDbCriteria->addCondition('`store2contact`.origin_id = :origin_organisation_id');
         $CDbCriteria->params = array(':campaign_id' => $Campaign->id, ':origin_organisation_id' => (int) $_POST['organisation_id']);
         $CDbCriteria->compare('`store2contact`.`origin_unique_id`', $uniqueIDs);
         $CDbCriteria->index = 'id';
         $CampaignContacts = CampaignContact::model()->findAll($CDbCriteria);
         // update all who match against a campaign contact 2 outcome
         if (sizeof($CampaignContacts)) {
             // do update on those rows
             CampaignContact2Outcome::model()->updateAll(array('outcome' => date("Y-m-d H:i:s")), "campaign_contact_id IN (" . implode(", ", array_keys($CampaignContacts)) . ")\n\t\t\t\t\t\tAND campaign_outcome_id = :campaign_outcome_id\n\t\t\t\t", array(':campaign_outcome_id' => $_POST['outcome_id']));
         }
         // at least something got updated.
         Yii::app()->user->setFlash('success', sizeof($CampaignContacts) . ' (of ' . $lines . ') rows provided a matching contact to allow outcome update.');
         $this->refresh();
     }
     $this->pageTitle = 'Upload Campaign Outcome Users | ' . Yii::app()->name;
     $this->breadcrumbs = array('Campaigns' => array('index'), $Campaign->name => array('campaign/createUpdate', 'id' => $Campaign->id), 'Upload Campaign Outcome Users');
     // show form
     $this->render('uploadOutcome', array('Campaign' => $Campaign));
 }