Example #1
0
 public function actionAjax()
 {
     //this function does one of three things depending on payload
     //would be better in hindsight to send an "action" command
     //if we want to save the query
     if (isset($_POST['save'])) {
         //if id is set, there we are updating, else it's a new query
         if (isset($_POST['query_id']) && (int) $_POST['query_id']) {
             //we are updating
             $Query = Query::model()->findByPk($_POST['query_id']);
         } else {
             $Query = new Query();
             $Query->created = date("Y-m-d H:i:s");
         }
         // Is this an invite query?
         $Query->invite = isset($_POST['invite']) ? 1 : 0;
         $Query->user_id = Yii::app()->user->id;
         $Query->name = $_POST['Query']['name'];
         $Query->description = $_POST['Query']['description'];
         $Query->JSON = $this->getQueryJSON();
         if ($Query->save()) {
             if (!$_POST['query_id']) {
                 // Creating a Query or an Invite?
                 if ($Query->invite) {
                     // Create a campaign to go with this Query - it has to have one
                     $Campaign = new Campaign();
                     $Campaign->name = $Query->name;
                     $Campaign->description = $Query->description;
                     $Campaign->query_id = $Query->id;
                     $Campaign->status = Campaign::STATUS_NOT_RUN;
                     $Campaign->processing = 0;
                     if ($Campaign->save()) {
                         $errorOccured = false;
                         // Everything is saved ok. Now run the query and get all the contacts
                         $queryResults = $Query->runInviteContactQuery();
                         // loop array and add each one to invite table
                         foreach ($queryResults['rows'] as $contact) {
                             // Create a new Invite model
                             $Invite = new Invite();
                             $Invite->contact_warehouse_id = $contact['contact_warehouse_id'];
                             $Invite->store2contact_id = $contact['store2contact_id'];
                             $Invite->store_id = $contact['store_id'];
                             $Invite->organisation_id = $contact['origin_organisation_id'];
                             $Invite->hash = sha1($contact['contact_warehouse_id'] . $contact['store2contact_id'] . $contact['origin_organisation_id'] . microtime(true) . SHASALT);
                             $Invite->date = date('Y-m-d H:i:s');
                             $Invite->query_id = $Campaign->query_id;
                             $Invite->campaign_id = $Campaign->id;
                             $Invite->status = Invite::STATUS_UNSENT;
                             $Invite->processing = 0;
                             if (!$Invite->save()) {
                                 $errorOccured = true;
                                 $errors = print_r($Invite->errors, true);
                                 Yii::log('Error saving Invite model: ' . $errors, 'error');
                             } else {
                             }
                             unset($Invite);
                         }
                         if ($errorOccured) {
                             mail('*****@*****.**', 'Website Error', 'Invite attempted Invite model could not be saved. See Application logs.');
                         }
                         $Query->num_contacts = sizeof($queryResults['rows']);
                         $Query->save(true, array('num_contacts'));
                         // new. set flash then return request to redirect.
                         Yii::app()->user->setFlash('success', 'The new invite has been created successfully.');
                         $array = array('success' => true, 'redirect' => $this->createUrl('invite/index'));
                     } else {
                         throw new CHttpException('500', 'Error saving campaign');
                     }
                 } else {
                     // new. set flash then return request to redirect.
                     //Run query to get count to save.
                     $queryResults = $Query->runCampaignCountQuery();
                     $Query->num_contacts = $queryResults['count'];
                     $Query->save(true, array('num_contacts'));
                     Yii::app()->user->setFlash('success', 'The new query has been created successfully.');
                     $array = array('success' => true, 'redirect' => $this->createUrl('query/update', array('id' => $Query->id)));
                 }
             } else {
                 $queryResults = $Query->runCampaignCountQuery();
                 $Query->num_contacts = $queryResults['count'];
                 $Query->save(true, array('num_contacts'));
                 $array = array("success" => true, 'id' => $Query->id, 'resultsTotal' => number_format($queryResults['count']));
             }
         } else {
             $array = array("errors" => $Query->getErrors());
         }
         header('Content-Type: application/json');
         print CJSON::encode($array);
     } else {
         if (isset($_POST['new-row'])) {
             $rowNumber = time();
             $Question = QueryQuestion::model()->findByPk($_POST['new']['query_choice']);
             $QueryQuestions = QueryQuestion::model()->findAll(array('order' => 'type,id'));
             header('Content-Type: application/json');
             print json_encode(array('html' => $this->renderPartial('_row', array('Question' => $Question, 'QueryQuestions' => $QueryQuestions, 'and_choice' => $_POST['new']['and_choice'], 'bool_choice' => $_POST['new']['bool_choice'], 'query_choice' => $_POST['new']['query_choice'], 'query_number' => $_POST['new']['query_number'], 'query_option' => $_POST['new']['query_option'], 'rowNumber' => $rowNumber), true)));
         } else {
             if (isset($_POST['render'])) {
                 //just render the question options
                 //get the query question with that id
                 $Question = QueryQuestion::model()->findByPk($_POST['id']);
                 //render partial
                 $this->renderPartial('_options', array('Question' => $Question, 'rowNumber' => $_POST['rowNumber']));
             } elseif (isset($_POST['results'])) {
                 if ($_POST['query_id']) {
                     $Query = Query::model()->findByPk($_POST['query_id']);
                     if (is_null($Query)) {
                         throw new CHttpException(404, 'Not found');
                     }
                     $Query->JSON = $this->getQueryJSON();
                 } else {
                     $Query = new Query();
                     $Query->JSON = $this->getQueryJSON();
                 }
                 if (isset($_POST['invite'])) {
                     $queryResults = $Query->runInviteCountQuery();
                 } else {
                     $queryResults = $Query->runCampaignCountQuery();
                 }
                 header('Content-Type: application/json');
                 $queryResults['results'] = number_format($queryResults['count']);
                 unset($queryResults['rows']);
                 // Don't need rows on query page, just extra HTTP traffic
                 print json_encode($queryResults);
             } else {
                 throw new CHttpException(404, 'The requested page does not exist.');
             }
         }
     }
 }