Skip to content

Commit

Permalink
Apply formatting change
Browse files Browse the repository at this point in the history
  • Loading branch information
ajparsons committed Oct 1, 2024
1 parent 7d8f9f7 commit 7d5383b
Show file tree
Hide file tree
Showing 368 changed files with 8,737 additions and 8,526 deletions.
9 changes: 4 additions & 5 deletions classes/AlertView.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
namespace MySociety\TheyWorkForYou;

class AlertView {

const ALERT_EXISTS = -2;
const CREATE_FAILED = -1;
public const ALERT_EXISTS = -2;
public const CREATE_FAILED = -1;

protected $user;
protected $db;
protected $alert;

public function __construct($THEUSER = null) {
$this->user = $THEUSER;
$this->db = new \ParlDB;
$this->alert = new \ALERT;
$this->db = new \ParlDB();
$this->alert = new \ALERT();
}

protected function confirmAlert($token) {
Expand Down
79 changes: 39 additions & 40 deletions classes/AlertView/Simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace MySociety\TheyWorkForYou\AlertView;

class Simple extends \MySociety\TheyWorkForYou\AlertView {

public function display() {
$data = array();
$data = [];
$data['recent_election'] = false;
if ( $this->user->loggedin() ) {
if ($this->user->loggedin()) {
$data['user_signed_in'] = true;
}

Expand All @@ -17,21 +16,21 @@ public function display() {
$data['postcode'] = trim(get_http_var('postcode'));

$result = $this->createAlertForPostCode($data['email'], $data['postcode']);
$data = array_merge( $data, $result );
$data = array_merge($data, $result);
} elseif (get_http_var('update')) {
$result = $this->getNewMP(get_http_var('update'));
$data = array_merge( $data, $result );
$data = array_merge($data, $result);
} elseif (get_http_var('update-alert')) {
$success = $this->replaceAlert( get_http_var('confirmation') );
$success = $this->replaceAlert(get_http_var('confirmation'));
$data['confirmation_received'] = $success;
} elseif (get_http_var('confirmed')) {
$success = $this->confirmAlert( get_http_var('confirmed') );
$success = $this->confirmAlert(get_http_var('confirmed'));
$data['confirmation_received'] = $success;
} else {
$data['email'] = $this->user->email() ? $this->user->email() : '';
$data['postcode'] = $this->user->postcode_is_set() ? $this->user->postcode() : '';

if ( $this->isEmailSignedUpForPostCode( $data['email'], $data['postcode'] ) ) {
if ($this->isEmailSignedUpForPostCode($data['email'], $data['postcode'])) {
$data['already_signed_up'] = true;
$mp = $this->getPersonFromPostcode($data['postcode']);
$data['mp_name'] = $mp->full_name();
Expand All @@ -42,10 +41,10 @@ public function display() {
}

private function getPersonFromPostcode($postcode) {
$args = array(
$args = [
'postcode' => $postcode,
'house' => 1
);
'house' => 1,
];

$member = new \MySociety\TheyWorkForYou\Member($args);
return $member;
Expand All @@ -66,30 +65,30 @@ private function validateDetails($email, $postcode) {
}

private function createAlertForPostCode($email, $postcode) {
if ( !$this->validateDetails($email, $postcode) ) {
return array('invalid-postcode-or-email' => true);
if (!$this->validateDetails($email, $postcode)) {
return ['invalid-postcode-or-email' => true];
}

try {
$person = $this->getPersonFromPostcode($postcode);
} catch ( \MySociety\TheyWorkForYou\MemberException $e ) {
return array('bad-constituency' => true);
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
return ['bad-constituency' => true];
}

$details = array(
$details = [
'email' => $email,
'pid' => $person->person_id,
'pc' => $postcode,
'confirm_base' => 'https://' . DOMAIN . '/alert/by-postcode?confirmed=',
);
];

$data = array();
$data = [];
$not_logged_in = $this->user->loggedin ? false : true;
$result = $this->alert->add($details, $not_logged_in);

switch ($result) {
case self::ALERT_EXISTS:
if ( $not_logged_in ) {
if ($not_logged_in) {
// no logged in user so send them an email to let them
// know someone tried to create an alert
$this->alert->send_already_signedup_email($details);
Expand All @@ -102,7 +101,7 @@ private function createAlertForPostCode($email, $postcode) {
$data['error'] = true;
break;
default: // alert created
if ( $not_logged_in ) {
if ($not_logged_in) {
$data['confirmation_sent'] = true;
} else {
$data['signedup_no_confirm'] = true;
Expand All @@ -116,53 +115,53 @@ private function replaceAlert($confirmation) {
$existing = $this->alert->fetch_by_token($confirmation);
preg_match('/speaker:(\d+)/', $existing['criteria'], $matches);
$old_mp_id = $matches[1];
$old_mp = new \MySociety\TheyWorkForYou\Member(array( 'person_id' => $old_mp_id ) );
$new_mp = new \MySociety\TheyWorkForYou\Member(array( 'constituency' => $old_mp->constituency, 'house' => 1 ));
$old_mp = new \MySociety\TheyWorkForYou\Member([ 'person_id' => $old_mp_id ]);
$new_mp = new \MySociety\TheyWorkForYou\Member([ 'constituency' => $old_mp->constituency, 'house' => 1 ]);

$q = $this->db->query(
"SELECT alert_id, criteria, registrationtoken FROM alerts
WHERE email = :email
AND criteria LIKE :criteria
AND confirmed = 1
AND deleted = 0",
array(
[
':email' => $existing['email'],
':criteria' => '%speaker:' . $old_mp_id . '%'
)
':criteria' => '%speaker:' . $old_mp_id . '%',
]
);

foreach ($q as $row) {
// need to reset this otherwise delete does not work
$this->alert->token_checked = null;
$other_criteria = trim(preg_replace('/speaker:\d+/', '', $row['criteria']));

$details = array(
$details = [
'email' => $existing['email'],
'pid' => $new_mp->person_id,
'pc' => '',
);
if ( $other_criteria ) {
];
if ($other_criteria) {
$details['keyword'] = $other_criteria;
}

$this->alert->delete($row['alert_id'] . '::' . $row['registrationtoken']);
$this->alert->add($details, false);
}

return array(
return [
'signedup_no_confirm' => true,
'new_mp' => $new_mp->full_name(),
);
];
}

private function isEmailSignedUpForPostCode($email, $postcode) {
$is_signed_up = false;

if ( $email && $postcode ) {
if ($email && $postcode) {
try {
$person = $this->getPersonFromPostcode($postcode);
$is_signed_up = $this->alert->fetch_by_mp($email, $person->person_id);
} catch ( \MySociety\TheyWorkForYou\MemberException $e ) {
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
$is_signed_up = false;
}
}
Expand All @@ -171,27 +170,27 @@ private function isEmailSignedUpForPostCode($email, $postcode) {

private function getNewMP($confirmation) {
if (!$confirmation) {
return array();
return [];
}

$existing = $this->alert->fetch_by_token($confirmation);
preg_match('/speaker:(\d+)/', $existing['criteria'], $matches);
$criteria = $matches[1];

$old_mp = new \MySociety\TheyWorkForYou\Member(array( 'person_id' => $criteria ) );
$new_mp = new \MySociety\TheyWorkForYou\Member(array( 'constituency' => $old_mp->constituency, 'house' => 1 ));
$old_mp = new \MySociety\TheyWorkForYou\Member([ 'person_id' => $criteria ]);
$new_mp = new \MySociety\TheyWorkForYou\Member([ 'constituency' => $old_mp->constituency, 'house' => 1 ]);

if ( $this->alert->fetch_by_mp( $existing['email'], $new_mp->person_id) ) {
$data = array(
if ($this->alert->fetch_by_mp($existing['email'], $new_mp->person_id)) {
$data = [
'already_signed_up' => true,
'old_mp' => $old_mp->full_name(),
'mp_name' => $new_mp->full_name(),
);
];
} else {
$data = array(
$data = [
'old_mp' => $old_mp->full_name(),
'new_mp' => $new_mp->full_name(),
);
];
}

$data['update'] = true;
Expand Down
60 changes: 30 additions & 30 deletions classes/AlertView/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Standard extends \MySociety\TheyWorkForYou\AlertView {

public function __construct($THEUSER = null) {
parent::__construct($THEUSER);
$this->data = array();
$this->data = [];
}

public function display() {
Expand Down Expand Up @@ -111,7 +111,7 @@ private function getBasicData() {
private function checkInput() {
global $SEARCHENGINE;

$errors = array();
$errors = [];

// Check each of the things the user has input.
// If there is a problem with any of them, set an entry in the $errors array.
Expand Down Expand Up @@ -159,28 +159,28 @@ private function searchForConstituenciesAndMembers() {
// Do the search
if ($this->data['alertsearch']) {
$this->data['members'] = \MySociety\TheyWorkForYou\Utility\Search::searchMemberDbLookupWithNames($this->data['alertsearch'], true);
list ($this->data['constituencies'], $this->data['valid_postcode']) = \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery($this->data['alertsearch']);
[$this->data['constituencies'], $this->data['valid_postcode']] = \MySociety\TheyWorkForYou\Utility\Search::searchConstituenciesByQuery($this->data['alertsearch']);
} else {
$this->data['members'] = array();
$this->data['members'] = [];
}

# If the above search returned one result for constituency
# search by postcode, use it immediately
if (isset($this->data['constituencies']) && count($this->data['constituencies']) == 1 && $this->data['valid_postcode']) {
$MEMBER = new \MEMBER(array('constituency' => $this->data['constituencies'][0], 'house' => 1));
$MEMBER = new \MEMBER(['constituency' => $this->data['constituencies'][0], 'house' => 1]);
$this->data['pid'] = $MEMBER->person_id();
$this->data['pc'] = $this->data['alertsearch'];
unset($this->data['constituencies']);
$this->data['alertsearch'] = '';
}

if (isset($this->data['constituencies'])) {
$cons = array();
$cons = [];
foreach ($this->data['constituencies'] as $constituency) {
try {
$MEMBER = new \MEMBER(array('constituency'=>$constituency, 'house' => 1));
$MEMBER = new \MEMBER(['constituency' => $constituency, 'house' => 1]);
$cons[$constituency] = $MEMBER;
} catch ( \MySociety\TheyWorkForYou\MemberException $e ) {
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
// do nothing
}
}
Expand All @@ -200,17 +200,17 @@ private function addAlert() {

// If this goes well, the alert will be added to the database and a confirmation email
// will be sent to them.
$success = $this->alert->add( $this->data, $confirm );
$success = $this->alert->add($this->data, $confirm);

if ($success>0 && !$confirm) {
if ($success > 0 && !$confirm) {
$result = 'alert-added';
} elseif ($success>0) {
} elseif ($success > 0) {
$result = 'alert-confirmation';
} elseif ($success == -2) {
// we need to make sure we know that the person attempting to sign up
// for the alert has that email address to stop people trying to work
// out what alerts they are signed up to
if ( $this->data['email_verified'] || ( $this->user->loggedin && $this->user->email() == $this->data['email'] ) ) {
if ($this->data['email_verified'] || ($this->user->loggedin && $this->user->email() == $this->data['email'])) {
$result = 'alert-exists';
} else {
// don't throw an error message as that implies that they have already signed
Expand All @@ -235,7 +235,7 @@ private function addAlert() {


private function formatSearchTerms() {
if ( $this->data['alertsearch'] ) {
if ($this->data['alertsearch']) {
$this->data['alertsearch_pretty'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->data['alertsearch']);
$this->data['search_text'] = $this->data['alertsearch'];
} else {
Expand All @@ -244,15 +244,15 @@ private function formatSearchTerms() {
}

private function checkForCommonMistakes() {
$mistakes = array();
$mistakes = [];
if (strstr($this->data['alertsearch'], ',') > -1) {
$mistakes['multiple'] = 1;
}

if (
preg_match('#([A-Z]{1,2}\d+[A-Z]? ?\d[A-Z]{2})#i', $this->data['alertsearch'], $m) &&
strlen($this->data['alertsearch']) > strlen($m[1]) &&
validate_postcode($m[1])
preg_match('#([A-Z]{1,2}\d+[A-Z]? ?\d[A-Z]{2})#i', $this->data['alertsearch'], $m) &&
strlen($this->data['alertsearch']) > strlen($m[1]) &&
validate_postcode($m[1])
) {
$this->data['postcode'] = $m[1];
$mistakes['postcode_and'] = 1;
Expand All @@ -262,11 +262,11 @@ private function checkForCommonMistakes() {
}

private function formatSearchMemberData() {
if ( isset($this->data['postcode']) ) {
if (isset($this->data['postcode'])) {
try {
$postcode = $this->data['postcode'];

$MEMBER = new \MEMBER( array('postcode' => $postcode) );
$MEMBER = new \MEMBER(['postcode' => $postcode]);
// move the postcode to the front just to be tidy
$tidy_alertsearch = $postcode . " " . trim(str_replace("$postcode", "", $this->data['alertsearch']));
$alertsearch_display = str_replace("$postcode ", "", $tidy_alertsearch);
Expand All @@ -275,39 +275,39 @@ private function formatSearchMemberData() {
$this->data['member_displaysearch'] = $alertsearch_display;
$this->data['member'] = $MEMBER;

if ( isset($this->data['mistakes']['postcode_and']) ) {
if (isset($this->data['mistakes']['postcode_and'])) {
$constituencies = \MySociety\TheyWorkForYou\Utility\Postcode::postcodeToConstituencies($postcode);
if ( isset($constituencies['SPC']) ) {
$MEMBER = new \MEMBER(array('constituency' => $constituencies['SPC'], 'house' => HOUSE_TYPE_SCOTLAND));
if (isset($constituencies['SPC'])) {
$MEMBER = new \MEMBER(['constituency' => $constituencies['SPC'], 'house' => HOUSE_TYPE_SCOTLAND]);
$this->data['scottish_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch);
$this->data['scottish_member'] = $MEMBER;
} elseif ( isset($constituencies['WAC']) ) {
$MEMBER = new \MEMBER(array('constituency' => $constituencies['WAC'], 'house' => HOUSE_TYPE_WALES));
} elseif (isset($constituencies['WAC'])) {
$MEMBER = new \MEMBER(['constituency' => $constituencies['WAC'], 'house' => HOUSE_TYPE_WALES]);
$this->data['welsh_alertsearch'] = str_replace("$postcode", "speaker:" . $MEMBER->person_id, $tidy_alertsearch);
$this->data['welsh_member'] = $MEMBER;
}
}
} catch ( \MySociety\TheyWorkForYou\MemberException $e ) {
} catch (\MySociety\TheyWorkForYou\MemberException $e) {
$this->data['member_error'] = 1;
}
}

if ( $this->data['pid'] ) {
$MEMBER = new \MEMBER( array('person_id' => $this->data['pid']) );
if ($this->data['pid']) {
$MEMBER = new \MEMBER(['person_id' => $this->data['pid']]);
$this->data['pid_member'] = $MEMBER;
}

if ( $this->data['keyword'] ) {
if ($this->data['keyword']) {
$this->data['display_keyword'] = \MySociety\TheyWorkForYou\Utility\Alert::prettifyCriteria($this->data['keyword']);
}
}

private function setUserData() {
$this->data['current_mp'] = false;
$this->data['alerts'] = array();
$this->data['alerts'] = [];
if ($this->data['email_verified']) {
if ($this->user->postcode()) {
$current_mp = new \MEMBER(array('postcode' => $this->user->postcode()));
$current_mp = new \MEMBER(['postcode' => $this->user->postcode()]);
if (!$this->alert->fetch_by_mp($this->data['email'], $current_mp->person_id())) {
$this->data['current_mp'] = $current_mp;
}
Expand Down
Loading

0 comments on commit 7d5383b

Please sign in to comment.