/** * Custom validation for unqiue name for query and user * **/ public function uniqueQueryName($attribute, $params) { $user_id = Yii::app()->user->getId(); $result = Query::model()->findByAttributes(array('user_id' => $user_id, 'name' => $this->name)); //check name is unqie if ($result) { $this->addError($attribute, 'You already have a query with that name.'); } }
/** * Create or update a particular model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id the ID of the model to be updated */ public function actionCreateUpdate() { $step = 1; if ($_GET['step']) { $step = $_GET['step']; } $Campaign = new Campaign(); $Campaign->scenario = "insiderCampaign"; $CampaignFile = new CampaignFile(); $NewOutcome = new CampaignOutcome(); $NewGroup = new CampaignGroup(); $letters = array('A', 'B', 'C', 'D'); if ($_GET['id']) { $Campaign = $this->loadModel($_GET['id']); $this->redirectRunCampaign($Campaign); // the following is to cope with groups on various dev environments with no groups if (!sizeof($Campaign->groups)) { foreach (array('A', 'B') as $letter) { $CampaignGroup = new CampaignGroup(); $CampaignGroup->campaign_id = $Campaign->id; $CampaignGroup->name = $letter; $CampaignGroup->fraction = 50; $CampaignGroup->save(); } $this->refresh(); } } if (!$Campaign->isNewRecord && !is_null($Campaign->query) && (int) $Campaign->query->invite === 1) { // not allowed here as it's an invite campaign. throw new CHttpException('401', 'Forbidden'); } if (!$Campaign->isNewRecord && isset($_GET['add-initial']) && !sizeof($Campaign->outcomes)) { // add an initial default outcome $NewOutcome = new CampaignOutcome(); $NewOutcome->campaign_id = $Campaign->id; $NewOutcome->name = 'An Outcome'; $NewOutcome->description = 'An outcome description can help explain the desired result of this outcome. Delete this outcome and create your own below.'; $NewOutcome->save(); $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id)); } if (sizeof($_POST['CampaignOutcome'])) { // adding outcome to campaign $NewOutcome = new CampaignOutcome(); $NewOutcome->campaign_id = $Campaign->id; $NewOutcome->name = $_POST['CampaignOutcome']['name']; $NewOutcome->description = $_POST['CampaignOutcome']['description']; $NewOutcome->url = $_POST['CampaignOutcome']['url']; if (!strlen($NewOutcome->url)) { // save null, not $NewOutcome->url = null; } if ($NewOutcome->save()) { Yii::app()->user->setFlash('outcome-success', "Successfully created new outcome '" . $NewOutcome->name . "'"); $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 2, '#' => 'outcomes')); } else { Yii::app()->user->setFlash('outcome-danger', 'Failed to create new outcome. See errors below.'); } } if (isset($_GET['remove-outcome'])) { $Criteria = new CDbCriteria(); $Criteria->compare('id', (int) $_GET['remove-outcome']); $Criteria->compare('campaign_id', (int) $_GET['campaign_id']); $Outcomes = CampaignOutcome::model()->findAll($Criteria); if (sizeof($Outcomes) === 1) { // deleting one $Outcomes[0]->delete(); } Yii::app()->user->setFlash('outcome-success', "Successfully removed the outcome."); $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 2, '#' => 'outcomes')); } if (isset($_GET['remove-group'])) { $Criteria = new CDbCriteria(); $Criteria->compare('id', (int) $_GET['remove-group']); $Criteria->compare('campaign_id', $Campaign->id); CampaignGroup::model()->deleteAll($Criteria); Yii::app()->user->setFlash('group-success', "Successfully removed the group."); $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 3, '#' => 'groups')); } if (sizeof($_POST['CampaignGroup'])) { $step = 3; if ($_POST['CampaignGroup']['id']) { $CampaignGroup = CampaignGroup::model()->findByPk($_POST['CampaignGroup']['id']); $CampaignGroup->setAttributes($_POST['CampaignGroup']); if ($CampaignGroup->save()) { Yii::app()->user->setFlash('group-success', 'Group "' . $CampaignGroup->name . '" updated successfully'); $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 3, '#' => 'groups')); } else { $errors = ''; foreach ($CampaignGroup->errors as $error) { $errors .= $error[0] . ' '; } Yii::app()->user->setFlash('group-danger', $errors); } } else { //its new $NewGroup->setAttributes($_POST['CampaignGroup']); $NewGroup->campaign_id = $Campaign->id; if ($NewGroup->save()) { Yii::app()->user->setFlash('group-success', 'Group "' . $NewGroup->name . '" updated successfully'); $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 3, '#' => 'groups')); } else { $errors = ''; foreach ($NewGroup->errors as $error) { $errors .= $error[0] . ' '; } Yii::app()->user->setFlash('group-danger', $errors); } } } if ($_POST['Campaign']) { $Campaign->setAttributes($_POST['Campaign']); if ($Campaign->save()) { if (!(int) $_GET['id']) { // new. Add default 50% groups foreach (array('A', 'B') as $letter) { $CampaignGroup = new CampaignGroup(); $CampaignGroup->campaign_id = $Campaign->id; $CampaignGroup->name = $letter; $CampaignGroup->fraction = 50; $CampaignGroup->save(); } } Yii::app()->user->setFlash('campaign-success', 'Campaign ' . ((int) $_GET['id'] ? 'updated' : 'created') . ' successfully'); //if it's a new record then go to step 2, otherwise go to step 1 if ($Campaign->isNewRecord) { $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 2, '#' => 'outcomes')); } else { $this->redirect(array('campaign/createUpdate', 'id' => $Campaign->id, 'step' => 1)); } } } // check group totals $totalPercentage = 0; foreach ($Campaign->groups as $CampaignGroup) { $totalPercentage += $CampaignGroup->fraction; } if ((int) $totalPercentage !== 100) { Yii::app()->user->setFlash('group-danger', 'The percentage splits of campaign groups must total 100. Currently group percentage splits total ' . $totalPercentage . '.'); } // check for subjectless groups $SubjectlessCampaignGroups = $Campaign->groups(array('condition' => 'isnull(`groups`.`subject`) || LENGTH(`groups`.`subject`) < 1')); if (!$Campaign->groups) { $MissingTemplates = true; } else { $MissingTemplates = false; } // check for templateless groups foreach ($Campaign->groups as $Group) { if (!$Group->email_template) { $MissingTemplates = true; break; } } $Queries = Query::model()->findAll(array('condition' => 'invite = 0', 'index' => 'id', 'order' => 'name ASC')); if (isset($Queries[$Campaign->query_id])) { $Query = null; if (!$Campaign->isNewRecord) { if ($Campaign->size > 0) { // we have to faff here. // we need to add a limit to an existing query as we can't pass it through the run params. // so we decode, add it and then encode again so when it's run it has the right limit $JSON = json_decode($Queries[$Campaign->query_id]->JSON); $JSON->limit = $Campaign->size; $Queries[$Campaign->query_id]->JSON = json_encode($JSON); } $Query = $Queries[$Campaign->query_id]->run(); } } $this->pageTitle = ((int) $_GET['id'] ? 'Update' : 'Create') . ' Campaign | ' . Yii::app()->name; $this->breadcrumbs = array('Campaigns' => array('index'), ((int) $_GET['id'] ? 'Update' : 'Create') . ' Campaign'); $Campaign->refresh(); $this->render('createUpdate', array('Campaign' => $Campaign, 'CampaignFile' => $CampaignFile, 'Queries' => $Queries, 'Query' => $Query, 'NewOutcome' => $NewOutcome, 'NewGroup' => $NewGroup, 'step' => $step, 'totalPercentage' => $totalPercentage, 'SubjectlessCampaignGroups' => $SubjectlessCampaignGroups, 'MissingTemplates' => $MissingTemplates)); }
switch ($Question->option_id) { case QueryQuestion::OPTION_VENUE: if (is_null($Venues)) { $Venues = Venue::model()->findAll(array('condition' => 'active = 1', 'index' => 'id')); } print ' ' . $Venues[$row->query_option]; break; case QueryQuestion::OPTION_ORGANISATION: if (is_null($Organisations)) { $Organisations = Organisation::model()->findAll(array('condition' => 'active = 1', 'index' => 'id')); } print ' ' . $Organisations[$row->query_option]->title; break; case QueryQuestion::OPTION_INVITE: if (is_null($InviteQueries)) { $InviteQueries = Query::model()->findAll(array('condition' => 'invite = 1', 'index' => 'id')); } print ' ' . $InviteQueries[$row->query_option]->name; break; case QueryQuestion::OPTION_CS: if (is_null($CultureSegments)) { $CultureSegments = CultureSegment::model()->findAll(array('index' => 'id')); } print ' ' . $CultureSegments[$row->query_option]->name; break; case QueryQuestion::OPTION_ARTFORM: if (is_null($Artforms)) { $Artforms = Artform::model()->findAll(array('index' => 'id', 'order' => 'title ASC')); } print ' ' . $CultureSegments[$row->query_option]->title; break;
switch ($Question->option_id) { // Venue case QueryQuestion::OPTION_VENUE: $Venue = Venue::model()->findByPk($query_option); print $Venue->title; break; // Organisation // Organisation case QueryQuestion::OPTION_ORGANISATION: $Organisation = Organisation::model()->findByPk($query_option); print $Organisation->title; break; // Invite // Invite case QueryQuestion::OPTION_INVITE: $InviteQuery = Query::model()->findByPk($query_option); print $InviteQuery->name; break; // Culture Segment // Culture Segment case QueryQuestion::OPTION_CS: $CultureSegment = CultureSegment::model()->findByPk($query_option); print $CultureSegment->name; break; // Artforms // Artforms case QueryQuestion::OPTION_ARTFORM: $Artforms = Artform::model()->findByPk($query_option); print $Artform->title; // Level of engagement // Level of engagement
/** * Delete an entry in the source * * @param Query $query * @param array $options * @return boolean */ public function delete($query, array $options = array()) { $model = $query->model(); $source = $model::meta('source'); $key = $this->locate($source, array('id' => $options['record']->id)); if ($key === false) { return false; } if ($this->__data[$source][$key] == end($this->__data[$source])) { unset($this->__data[$source][$key]); } else { $this->__data[$source][$key] = array_pop($this->__data[$source]); } return true; }
public function actionDownload($id) { // do not include log info in generated file foreach (Yii::app()->log->routes as $route) { if ($route instanceof CProfileLogRoute) { $route->enabled = false; } } // download (filtered) results of a query $Query = Query::model()->findByAttributes(array('id' => $id)); if (is_null($Query)) { throw new CHttpException('404', 'File not found'); } $result = $Query->run(array('*'), (int) Yii::app()->user->organisation_id ? (int) Yii::app()->user->organisation_id : null); if (!sizeof($result['rows'])) { throw new CHttpException('404', 'Results not found'); } $cleanQueryName = preg_replace(array("@[^a-z0-9\\-\\s]@i", "@\\s+@", "@\\-{2,}@"), array("", "-", "-"), $Query->name) . '-' . date("Y-m-d"); $csv = fopen('php://output', 'w'); header("Expires: Tue, 03 Jul 2001 06:00:00 GMT"); header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header('Content-Encoding: UTF-8'); header('Content-type: text/csv; charset=UTF-8'); // disposition / encoding on response body header("Content-Disposition: attachment;filename=" . $cleanQueryName . ".csv"); header("Content-Transfer-Encoding: binary"); $columns = array('contact_warehouse_id', 'salutation', 'first_name', 'last_name', 'email', 'phone', 'mobile', 'address_line_1', 'address_line_2', 'address_line_3', 'address_line_4', 'address_town', 'address_postcode', 'address_county'); // header row fputcsv($csv, $columns); $Store = new Store(); foreach ($result['rows'] as $row) { $data = array(); foreach ($columns as $column) { switch ($column) { case 'email': $data[$column] = $Store->decryptEmail($row[$column]); break; case 'last_name': $data[$column] = $Store->decryptLastName($row[$column]); break; case 'phone': $data[$column] = $Store->decryptPhone($row[$column]); if (strlen($data[$column]) && mb_substr($data[$column], 0, 1, 'utf-8') != 0) { // add a zero to anything missing one $data[$column] = '0' . $data[$column]; } break; case 'mobile': $data[$column] = $Store->decryptMobile($row[$column]); if (strlen($data[$column]) && mb_substr($data[$column], 0, 1, 'utf-8') != 0) { // add a zero to anything missing one $data[$column] = '0' . $data[$column]; } break; case 'address_line_1': $data[$column] = $Store->decryptAddress1($row[$column]); break; default: $data[$column] = $row[$column]; } } fputcsv($csv, $data); } fclose($csv); exit; }