2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2007-09-16 02:44:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Member classes
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The member class which represents the users of the system
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Member extends DataObject {
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $db = array(
|
|
|
|
'FirstName' => "Varchar",
|
|
|
|
'Surname' => "Varchar",
|
|
|
|
'Email' => "Varchar",
|
2007-09-15 23:51:37 +02:00
|
|
|
'Password' => "Varchar(64)", // support for up to SHA256!
|
2007-09-14 21:10:18 +02:00
|
|
|
'RememberLoginToken' => "Varchar(50)",
|
2007-07-19 12:40:28 +02:00
|
|
|
'NumVisit' => "Int",
|
|
|
|
'LastVisited' => 'Datetime',
|
2007-09-16 03:59:59 +02:00
|
|
|
'Bounced' => 'Boolean', // Note: This does not seem to be used anywhere.
|
2007-09-16 02:32:48 +02:00
|
|
|
'AutoLoginHash' => 'Varchar(30)',
|
2007-09-14 21:10:18 +02:00
|
|
|
'AutoLoginExpired' => 'Datetime',
|
|
|
|
'BlacklistedEmail' => 'Boolean',
|
2007-09-15 23:51:37 +02:00
|
|
|
'PasswordEncryption' => "Enum('none', 'none')",
|
|
|
|
'Salt' => "Varchar(50)"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-15 23:51:37 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $belongs_many_many = array(
|
|
|
|
"Groups" => "Group",
|
|
|
|
|
|
|
|
);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $has_many = array(
|
|
|
|
'UnsubscribedRecords' => 'Member_UnsubscribeRecord'
|
|
|
|
);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $default_sort = "Surname, FirstName";
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
static $indexes = array(
|
|
|
|
'Email' => true,
|
2007-09-16 02:32:48 +02:00
|
|
|
'AutoLoginHash' => 'unique (AutoLoginHash)'
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-15 23:51:37 +02:00
|
|
|
/**
|
|
|
|
* This method is used to initialize the static database members
|
|
|
|
*
|
|
|
|
* Since PHP doesn't support any expressions for the initialization of
|
|
|
|
* static member variables we need a method that does that.
|
|
|
|
*
|
|
|
|
* This method adds all supported encryption algorithms to the
|
|
|
|
* PasswordEncryption Enum field.
|
|
|
|
*
|
|
|
|
* @todo Maybe it would be useful to define this in DataObject and call
|
|
|
|
* it automatically?
|
|
|
|
*/
|
|
|
|
public static function init_db_fields() {
|
|
|
|
self::$db['PasswordEncryption'] = "Enum(array('none', '" .
|
|
|
|
implode("', '", array_map("addslashes",
|
|
|
|
Security::get_encryption_algorithms())) .
|
|
|
|
"'), 'none')";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
/**
|
|
|
|
* Check if the passed password matches the stored one
|
|
|
|
*
|
|
|
|
* @param string $password The clear text password to check
|
|
|
|
* @return bool Returns TRUE if the passed password is valid, otherwise
|
|
|
|
* FALSE.
|
|
|
|
*/
|
|
|
|
public function checkPassword($password) {
|
|
|
|
$encryption_details = Security::encrypt_password($password, $this->Salt,
|
|
|
|
$this->PasswordEncryption);
|
|
|
|
|
|
|
|
return ($this->Password === $encryption_details['password']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Logs this member in
|
|
|
|
*
|
|
|
|
* @param bool $remember If set to TRUE, the member will be logged in
|
|
|
|
* automatically the next time.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
function logIn($remember = false) {
|
|
|
|
session_regenerate_id(true);
|
2007-07-19 12:40:28 +02:00
|
|
|
Session::set("loggedInAs", $this->ID);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->NumVisit++;
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
if($remember) {
|
|
|
|
|
|
|
|
$token = substr(md5(uniqid(rand(), true)), 0, 49 - strlen($this->ID));
|
|
|
|
$this->RememberLoginToken = $token;
|
|
|
|
Cookie::set('alc_enc', $this->ID . ':' . $token);
|
|
|
|
} else {
|
|
|
|
$this->RememberLoginToken = null;
|
|
|
|
Cookie::set('alc_enc', null);
|
|
|
|
Cookie::forceExpiry('alc_enc');
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->write();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Log the user in if the "remember login" cookie is set
|
|
|
|
*
|
|
|
|
* The <i>remember login token</i> will be changed on every successful
|
|
|
|
* auto-login.
|
|
|
|
*/
|
|
|
|
static function autoLogin() {
|
2007-09-15 23:43:04 +02:00
|
|
|
if(strpos(Cookie::get('alc_enc'), ':') && !Session::get("loggedInAs")) {
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-15 23:43:04 +02:00
|
|
|
list($uid, $token) = explode(':', Cookie::get('alc_enc'), 2);
|
2007-09-15 02:27:40 +02:00
|
|
|
$SQL_uid = Convert::raw2sql($uid);
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
$member = DataObject::get_one(
|
2007-09-15 02:27:40 +02:00
|
|
|
"Member", "Member.ID = '$SQL_uid'");
|
|
|
|
|
|
|
|
if($member && $member->RememberLoginToken != $token) $member = null;
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
if($member) {
|
|
|
|
session_regenerate_id(true);
|
|
|
|
Session::set("loggedInAs", $member->ID);
|
|
|
|
|
|
|
|
$token = substr(md5(uniqid(rand(), true)),
|
|
|
|
0, 49 - strlen($member->ID));
|
|
|
|
$member->RememberLoginToken = $token;
|
|
|
|
Cookie::set('alc_enc', $member->ID . ':' . $token);
|
|
|
|
|
|
|
|
$member->NumVisit++;
|
|
|
|
$member->write();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Logs this member out.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
function logOut() {
|
2007-07-19 12:40:28 +02:00
|
|
|
Session::clear("loggedInAs");
|
2007-09-14 21:10:18 +02:00
|
|
|
session_regenerate_id(true);
|
|
|
|
|
|
|
|
$this->RememberLoginToken = null;
|
|
|
|
Cookie::set('alc_enc', null);
|
|
|
|
Cookie::forceExpiry('alc_enc');
|
|
|
|
|
|
|
|
$this->write();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate an auto login hash
|
|
|
|
*
|
2007-09-16 02:32:48 +02:00
|
|
|
* This creates an auto login hash that can be used to reset the password.
|
|
|
|
*
|
|
|
|
* @param int $lifetime The lifetime of the auto login hash in days
|
|
|
|
* (by default 2 days)
|
|
|
|
*
|
|
|
|
* @todo Make it possible to handle database errors such as a "duplicate
|
|
|
|
* key" error
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-09-16 02:32:48 +02:00
|
|
|
function generateAutologinHash($lifetime = 2) {
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
do {
|
|
|
|
$hash = substr(base_convert(md5(uniqid(mt_rand(), true)), 16, 36),
|
|
|
|
0, 30);
|
|
|
|
} while(DataObject::get_one('Member', "`AutoLoginHash` = '$hash'"));
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
$this->AutoLoginHash = $hash;
|
|
|
|
$this->AutoLoginExpired = date('Y-m-d', time() + (86400 * $lifetime));
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->write();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-16 02:32:48 +02:00
|
|
|
* Return the member for the auto login hash
|
|
|
|
*
|
|
|
|
* @param bool $login Should the member be logged in?
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-16 02:32:48 +02:00
|
|
|
static function autoLoginHash($RAW_hash, $login = false) {
|
2007-09-14 21:10:18 +02:00
|
|
|
$SQL_hash = Convert::raw2sql($RAW_hash);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
$member = DataObject::get_one('Member',"`AutoLoginHash`='" . $SQL_hash .
|
|
|
|
"' AND `AutoLoginExpired` > NOW()");
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-16 02:32:48 +02:00
|
|
|
if($login && $member)
|
2007-07-19 12:40:28 +02:00
|
|
|
$member->logIn();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $member;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send signup, change password or forgot password informations to an user
|
|
|
|
*
|
|
|
|
* @param string $type Information type to send ("signup",
|
|
|
|
* "changePassword" or "forgotPassword")
|
2007-09-16 02:32:48 +02:00
|
|
|
* @param array $data Additional data to pass to the email (can be used in
|
|
|
|
* the template)
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-09-16 02:32:48 +02:00
|
|
|
function sendInfo($type = 'signup', $data = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
switch($type) {
|
2007-09-14 21:10:18 +02:00
|
|
|
case "signup":
|
|
|
|
$e = new Member_SignupEmail();
|
|
|
|
break;
|
|
|
|
case "changePassword":
|
|
|
|
$e = new Member_ChangePasswordEmail();
|
|
|
|
break;
|
|
|
|
case "forgotPassword":
|
|
|
|
$e = new Member_ForgotPasswordEmail();
|
|
|
|
break;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-16 02:32:48 +02:00
|
|
|
|
|
|
|
if(is_array($data)) {
|
|
|
|
foreach($data as $key => $value)
|
|
|
|
$e->$key = $value;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$e->populateTemplate($this);
|
|
|
|
$e->send();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the fields for the member form
|
|
|
|
*
|
|
|
|
* @return FieldSet Returns a {@link FieldSet} containing the fields for
|
|
|
|
* the member form.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getMemberFormFields() {
|
|
|
|
return new FieldSet(
|
|
|
|
new TextField("FirstName", "First Name"),
|
|
|
|
new TextField("Surname", "Surname"),
|
|
|
|
new TextField("Email", "Email"),
|
|
|
|
new TextField("Password", "Password")
|
|
|
|
);
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method for the member validator
|
|
|
|
*
|
|
|
|
* @return Member_Validator Returns an instance of a
|
|
|
|
* {@link Member_Validator} object.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function getValidator() {
|
|
|
|
return new Member_Validator();
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Returns the current logged in user
|
|
|
|
*
|
|
|
|
* @return bool|Member Returns the member object of the current logged in
|
|
|
|
* user or FALSE.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
static function currentUser() {
|
2007-09-14 21:10:18 +02:00
|
|
|
$id = Session::get("loggedInAs");
|
|
|
|
if(!$id) {
|
|
|
|
self::autoLogin();
|
|
|
|
$id = Session::get("loggedInAs");
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if($id) {
|
2007-07-19 12:40:28 +02:00
|
|
|
return DataObject::get_one("Member", "Member.ID = $id");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Get the ID of the current logged in user
|
|
|
|
*
|
|
|
|
* @return int Returns the ID of the current logged in user or 0.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
static function currentUserID() {
|
|
|
|
$id = Session::get("loggedInAs");
|
2007-09-14 21:10:18 +02:00
|
|
|
if(!$id) {
|
|
|
|
self::autoLogin();
|
|
|
|
$id = Session::get("loggedInAs");
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return is_numeric($id) ? $id : 0;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Add the members email address to the blacklist
|
|
|
|
*
|
|
|
|
* With this method the blacklisted email table is updated to ensure that
|
|
|
|
* no promotional material is sent to the member (newsletters).
|
|
|
|
* Standard system messages are still sent such as receipts.
|
|
|
|
*
|
|
|
|
* @param bool $val Set to TRUE if the address should be added to the
|
|
|
|
* blacklist, otherwise to FALSE.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
function setBlacklistedEmail($val) {
|
|
|
|
if($val && $this->Email) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$blacklisting = new Email_BlackList();
|
|
|
|
$blacklisting->BlockedEmail = $this->Email;
|
|
|
|
$blacklisting->MemberID = $this->ID;
|
|
|
|
$blacklisting->write();
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
$this->setField("BlacklistedEmail", $val);
|
2007-09-16 03:53:02 +02:00
|
|
|
// Save the BlacklistedEmail field to the Member table
|
|
|
|
$this->write();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a random password
|
|
|
|
*
|
|
|
|
* BDC - added randomiser to kick in if there's no words file on the
|
|
|
|
* filesystem.
|
|
|
|
*
|
|
|
|
* @return string Returns a random password.
|
|
|
|
*/
|
|
|
|
static function createNewPassword() {
|
|
|
|
if(file_exists('/usr/share/silverstripe/wordlist.txt')) {
|
|
|
|
$words = file('/usr/share/silverstripe/wordlist.txt');
|
|
|
|
|
|
|
|
list($usec, $sec) = explode(' ', microtime());
|
|
|
|
srand($sec + ((float) $usec * 100000));
|
|
|
|
|
|
|
|
$word = trim($words[rand(0,sizeof($words)-1)]);
|
|
|
|
$number = rand(10,999);
|
|
|
|
|
|
|
|
return $word . $number;
|
|
|
|
} else {
|
|
|
|
$random = rand();
|
|
|
|
$string = md5($random);
|
|
|
|
$output = substr($string, 0, 6);
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler called before writing to the database
|
|
|
|
*
|
|
|
|
* If an email's filled out look for a record with the same email and if
|
|
|
|
* found update this record to merge with that member.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function onBeforeWrite() {
|
2007-09-15 23:51:37 +02:00
|
|
|
if(isset($this->changed['Password']) && $this->changed['Password']) {
|
|
|
|
// Password was changed: encrypt the password according the settings
|
|
|
|
$encryption_details = Security::encrypt_password($this->Password);
|
|
|
|
$this->Password = $encryption_details['password'];
|
|
|
|
$this->Salt = $encryption_details['salt'];
|
|
|
|
$this->PasswordEncryption = $encryption_details['algorithm'];
|
|
|
|
|
|
|
|
$this->changed['Salt'] = true;
|
|
|
|
$this->changed['PasswordEncryption'] = true;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($this->Email) {
|
2007-09-14 21:10:18 +02:00
|
|
|
if($this->ID) {
|
|
|
|
$idClause = "AND `Member`.ID <> $this->ID";
|
|
|
|
} else {
|
|
|
|
$idClause = "";
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
$existingRecord = DataObject::get_one(
|
|
|
|
"Member", "Email = '" . addslashes($this->Email) . "' $idClause");
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Debug::message("Found an existing member for email $this->Email");
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($existingRecord) {
|
|
|
|
$newID = $existingRecord->ID;
|
|
|
|
if($this->ID) {
|
|
|
|
DB::query("UPDATE Group_Members SET MemberID = $newID WHERE MemberID = $this->ID");
|
|
|
|
}
|
|
|
|
$this->ID = $newID;
|
|
|
|
// Merge existing data into the local record
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($existingRecord->getAllFields() as $k => $v) {
|
2007-08-14 00:04:03 +02:00
|
|
|
if(!isset($this->changed[$k]) || !$this->changed[$k]) $this->record[$k] = $v;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if the member is in one of the given groups
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
|
|
|
* @param array $groups Groups to check
|
|
|
|
* @return bool Returns TRUE if the member is in one of the given groups,
|
|
|
|
* otherwise FALSE.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
public function inGroups(array $groups) {
|
|
|
|
foreach($this->Groups() as $group)
|
2007-07-19 12:40:28 +02:00
|
|
|
$memberGroups[] = $group->Title;
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
return count(array_intersect($memberGroups, $groups)) > 0;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Check if the member is in the given group
|
|
|
|
*
|
|
|
|
* @param int $groupID ID of the group to check
|
|
|
|
* @return bool Returns TRUE if the member is in the given group,
|
|
|
|
* otherwise FALSE.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
public function inGroup($groupID) {
|
|
|
|
foreach($this->Groups() as $group) {
|
|
|
|
if($groupID == $group->ID)
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Alias for {@link inGroup}
|
|
|
|
*
|
|
|
|
* @param int $groupID ID of the group to check
|
|
|
|
* @return bool Returns TRUE if the member is in the given group,
|
|
|
|
* otherwise FALSE.
|
|
|
|
* @see inGroup()
|
|
|
|
*/
|
|
|
|
public function isInGroup($groupID) {
|
|
|
|
return $this->inGroup($groupID);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns true if this user is an administrator.
|
|
|
|
* Administrators have access to everything. The lucky bastards! ;-)
|
2007-09-14 21:10:18 +02:00
|
|
|
*
|
|
|
|
* @return Returns TRUE if this user is an administrator.
|
2007-08-06 02:47:31 +02:00
|
|
|
* @todo Should this function really exists? Is not {@link isAdmin()} the
|
|
|
|
* only right name for this?
|
|
|
|
* @todo Is {@link Group}::CanCMSAdmin not deprecated?
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function _isAdmin() {
|
|
|
|
if($groups = $this->Groups()) {
|
2007-09-14 21:10:18 +02:00
|
|
|
foreach($groups as $group) {
|
|
|
|
if($group->CanCMSAdmin)
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-08-06 02:47:31 +02:00
|
|
|
|
|
|
|
return Permission::check('ADMIN');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the user is an administrator
|
|
|
|
*
|
|
|
|
* Alias for {@link _isAdmin()} because the method is used in both ways
|
|
|
|
* all over the framework.
|
|
|
|
*
|
|
|
|
* @return Returns TRUE if this user is an administrator.
|
|
|
|
* @see _isAdmin()
|
|
|
|
*/
|
|
|
|
public function isAdmin() {
|
|
|
|
return $this->_isAdmin();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
function _isCMSUser() {
|
|
|
|
if($groups = $this->Groups()) {
|
2007-09-14 21:10:18 +02:00
|
|
|
foreach($groups as $group) {
|
|
|
|
if($group->CanCMS)
|
|
|
|
return true;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
//------------------- HELPER METHODS -----------------------------------//
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Get the complete name of the member
|
|
|
|
*
|
|
|
|
* @return string Returns the first- and surname of the member. If the ID
|
|
|
|
* of the member is equal 0, only the surname is returned.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getTitle() {
|
|
|
|
if($this->getField('ID') === 0)
|
|
|
|
return $this->getField('Surname');
|
|
|
|
return $this->getField('Surname') . ', ' . $this->getField('FirstName');
|
2007-09-14 20:23:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the complete name of the member
|
|
|
|
*
|
|
|
|
* @return string Returns the first- and surname of the member.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function getName() {
|
|
|
|
return $this->FirstName . ' ' . $this->Surname;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set first- and surname
|
|
|
|
*
|
|
|
|
* This method assumes that the last part of the name is the surname, e.g.
|
|
|
|
* <i>A B C</i> will result in firstname <i>A B</i> and surname <i>C</i>
|
|
|
|
*
|
|
|
|
* @param string $name The name
|
|
|
|
*/
|
|
|
|
public function setName($name) {
|
|
|
|
$nameParts = explode(' ', $name);
|
|
|
|
$this->Surname = array_pop($nameParts);
|
|
|
|
$this->FirstName = join(' ', $nameParts);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Alias for {@link setName}
|
|
|
|
*
|
|
|
|
* @param string $name The name
|
|
|
|
* @see setName()
|
|
|
|
*/
|
|
|
|
public function splitName($name) {
|
|
|
|
return $this->setName($name);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
//---------------------------------------------------------------------//
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
2007-09-15 02:23:44 +02:00
|
|
|
* Get a "many-to-many" map that holds for all members their group
|
|
|
|
* memberships
|
|
|
|
*
|
|
|
|
* @return Member_GroupSet Returns a map holding for all members their
|
|
|
|
* group memberships.
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function Groups() {
|
|
|
|
$groups = $this->getManyManyComponents("Groups");
|
2007-09-15 02:40:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$unsecure = DataObject::get("Group_Unsecure", "");
|
2007-09-14 21:10:18 +02:00
|
|
|
if($unsecure) {
|
|
|
|
foreach($unsecure as $unsecureItem) {
|
|
|
|
$groups->push($unsecureItem);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDs = $groups->column();
|
|
|
|
$collatedGroups = array();
|
|
|
|
foreach($groups as $group) {
|
2007-09-15 02:23:44 +02:00
|
|
|
$collatedGroups = array_merge((array)$collatedGroups,
|
|
|
|
$group->collateAncestorIDs());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$table = "Group_Members";
|
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
if(count($collatedGroups) > 0) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$collatedGroups = implode(", ", array_unique($collatedGroups));
|
|
|
|
|
|
|
|
$result = singleton('Group')->instance_get("`ID` IN ($collatedGroups)", "ID", "", "", "Member_GroupSet");
|
|
|
|
} else {
|
|
|
|
$result = new Member_GroupSet();
|
|
|
|
}
|
|
|
|
|
2007-09-15 02:23:44 +02:00
|
|
|
$result->setComponentInfo("many-to-many", $this, "Member", $table,
|
|
|
|
"Group");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
2007-09-15 02:23:44 +02:00
|
|
|
* Get member SQLMap
|
|
|
|
*
|
|
|
|
* @param string $filter Filter for the SQL statement (WHERE clause)
|
|
|
|
* @param string $sort Sorting function (ORDER clause)
|
|
|
|
* @param string $blank Shift a blank member in the items
|
|
|
|
* @return SQLMap Returns an SQLMap that returns all Member data.
|
|
|
|
*
|
|
|
|
* @todo Improve documentation of this function! (Markus)
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function map($filter = "", $sort = "", $blank="") {
|
|
|
|
$ret = new SQLMap(singleton('Member')->extendedSQL($filter, $sort));
|
2007-09-14 21:10:18 +02:00
|
|
|
if($blank) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$blankMember = new Member();
|
|
|
|
$blankMember->Surname = $blank;
|
|
|
|
$blankMember->ID = 0;
|
|
|
|
|
|
|
|
$ret->getItems()->shift($blankMember);
|
|
|
|
}
|
2007-09-15 02:23:44 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $ret;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
2007-09-15 02:23:44 +02:00
|
|
|
* Get a member SQLMap of members in specific groups
|
|
|
|
*
|
|
|
|
* @param mixed $groups Optional groups to include in the map. If NULL is
|
|
|
|
* passed, all groups are returned, i.e.
|
|
|
|
* {@link map()} will be called.
|
|
|
|
* @return SQLMap Returns an SQLMap that returns all Member data.
|
|
|
|
* @see map()
|
|
|
|
*
|
|
|
|
* @todo Improve documentation of this function! (Markus)
|
2007-09-14 21:10:18 +02:00
|
|
|
*/
|
|
|
|
public static function mapInGroups($groups = null) {
|
|
|
|
if(!$groups)
|
2007-07-19 12:40:28 +02:00
|
|
|
return Member::map();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = array();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if(is_a($groups, 'DataObjectSet')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach( $groups as $group )
|
|
|
|
$groupIDList[] = $group->ID;
|
2007-09-14 21:10:18 +02:00
|
|
|
} elseif(is_array($groups)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = $groups;
|
2007-09-14 21:10:18 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList[] = $groups;
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if(empty($groupIDList))
|
2007-09-14 20:23:28 +02:00
|
|
|
return Member::map();
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
return new SQLMap(singleton('Member')->extendedSQL(
|
|
|
|
"`GroupID` IN (" . implode( ',', $groupIDList ) .
|
|
|
|
")", "Surname, FirstName", "", "INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID`"));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Get a map of all members in the groups given that have CMS permissions
|
|
|
|
*
|
|
|
|
* If no groups are passed, all groups with CMS permissions will be used.
|
|
|
|
*
|
|
|
|
* @param array $groups Groups to consider or NULL to use all groups with
|
|
|
|
* CMS permissions.
|
|
|
|
* @return SQLMap Returns a map of all members in the groups given that
|
|
|
|
* have CMS permissions.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
public static function mapInCMSGroups($groups = null) {
|
|
|
|
if(!$groups || $groups->Count() == 0) {
|
|
|
|
$groups = DataObject::get('Group', "", "",
|
|
|
|
"INNER JOIN `Permission` ON `Permission`.GroupID = `Group`.ID AND `Permission`.Code IN ('ADMIN', 'CMS_ACCESS_AssetAdmin')");
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = array();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if(is_a($groups, 'DataObjectSet')) {
|
|
|
|
foreach($groups as $group)
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList[] = $group->ID;
|
2007-09-14 21:10:18 +02:00
|
|
|
} elseif(is_array($groups)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groupIDList = $groups;
|
2007-09-14 21:10:18 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/*if( empty( $groupIDList ) )
|
|
|
|
return Member::map(); */
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
$filterClause = ($groupIDList)
|
|
|
|
? "`GroupID` IN (" . implode( ',', $groupIDList ) . ")"
|
|
|
|
: "";
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
return new SQLMap(singleton('Member')->extendedSQL($filterClause,
|
|
|
|
"Surname, FirstName", "",
|
|
|
|
"INNER JOIN `Group_Members` ON `MemberID`=`Member`.`ID` INNER JOIN `Group` ON `Group`.`ID`=`GroupID`"));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Get the groups in which the member is NOT in
|
|
|
|
*
|
|
|
|
* When passed an array of groups, and a component set of groups, this
|
|
|
|
* function will return the array of groups the member is NOT in.
|
|
|
|
*
|
|
|
|
* @param array $groupList An array of group code names.
|
|
|
|
* @param array $memberGroups A component set of groups (if set to NULL,
|
|
|
|
* $this->groups() will be used)
|
|
|
|
* @return array Groups in which the member is NOT in.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
public function memberNotInGroups($groupList, $memberGroups = null){
|
|
|
|
if(!$memberGroups)
|
|
|
|
$memberGroups = $this->Groups();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
foreach($memberGroups as $group) {
|
|
|
|
if(in_array($group->Code, $groupList)) {
|
|
|
|
$index = array_search($group->Code, $groupList);
|
2007-07-19 12:40:28 +02:00
|
|
|
unset($groupList[$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $groupList;
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Return a {@link FieldSet} of fields that would appropriate for editing
|
|
|
|
* this member.
|
|
|
|
*
|
|
|
|
* @return FieldSet Return a FieldSet of fields that would appropriate for
|
|
|
|
* editing this member.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function getCMSFields() {
|
|
|
|
$fields = new FieldSet(
|
|
|
|
//new TextField("Salutation", "Title"),
|
|
|
|
new HeaderField( "Personal Details" ),
|
|
|
|
new TextField("FirstName", "First Name"),
|
|
|
|
new TextField("Surname", "Surname"),
|
|
|
|
new HeaderField( "User Details" ),
|
|
|
|
new TextField("Email", "Email"),
|
|
|
|
/*new TextField("Password", "Password")*/
|
|
|
|
new PasswordField("Password", "Password")
|
|
|
|
//new TextareaField("Address","Address"),
|
|
|
|
//new TextField("JobTitle", "Job Title"),
|
|
|
|
//new TextField( "Organisation", "Organisation" ),
|
|
|
|
//new OptionsetField("HTMLEmail","Mail Format", array( 1 => 'HTML', 0 => 'Text only' ) )
|
|
|
|
);
|
2007-09-15 02:40:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
// if($this->hasMethod('updateCMSFields')) $this->updateCMSFields($fields);
|
2007-09-15 02:40:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-15 02:40:28 +02:00
|
|
|
/**
|
|
|
|
* Add an extension to the Member class
|
|
|
|
*
|
|
|
|
* This method can be used to add behaviour to Member class. The extension
|
|
|
|
* affects the entire class - all members will get the additional
|
|
|
|
* behaviour. However, if you want to restrict things, you should add
|
|
|
|
* appropriate Permission::checkMember() calls to the extensions's
|
|
|
|
* methods.
|
|
|
|
*
|
|
|
|
* @param string $extensionName Class name of the extension to add
|
|
|
|
*/
|
|
|
|
public static function add_role($extensionName) {
|
|
|
|
DataObject::add_extension('Member', $extensionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Unsubscribe from newsletter
|
|
|
|
*
|
|
|
|
* @param NewsletterType $newsletterType Newsletter type to unsubscribe
|
|
|
|
* from
|
|
|
|
*/
|
|
|
|
function unsubscribeFromNewsletter(NewsletterType $newsletterType) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// record today's date in unsubscriptions
|
2007-09-14 21:10:18 +02:00
|
|
|
// this is a little bit redundant
|
|
|
|
$unsubscribeRecord = new Member_UnsubscribeRecord();
|
|
|
|
$unsubscribeRecord->unsubscribe($this, $newsletterType);
|
|
|
|
$this->Groups()->remove($newsletterType->GroupID);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function requireDefaultRecords() {
|
|
|
|
parent::requireDefaultRecords();
|
|
|
|
|
2007-07-20 01:15:05 +02:00
|
|
|
if(!DB::query("SELECT * FROM Member")->value() && isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
Security::findAnAdministrator($_REQUEST['username'], $_REQUEST['password']);
|
2007-08-24 05:31:14 +02:00
|
|
|
Database::alteration_message("Added admin account","created");
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Special kind of {@link ComponentSet} that has special methods for
|
|
|
|
* manipulating a user's membership
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Member_GroupSet extends ComponentSet {
|
|
|
|
/**
|
2007-09-14 20:23:28 +02:00
|
|
|
* Control group membership with a number of checkboxes.
|
2007-09-14 21:10:18 +02:00
|
|
|
* - If the checkbox fields are present in $data, then the member will be
|
|
|
|
* added to the group with the same codename.
|
|
|
|
* - If the checkbox fields are *NOT* present in $data, then the member
|
|
|
|
* will be removed from the group with the same codename.
|
|
|
|
*
|
|
|
|
* @param array $checkboxes An array list of the checkbox fieldnames (only
|
|
|
|
* values are used). E.g. array(0, 1, 2)
|
|
|
|
* @param array $data The form data. Uually in the format array(0 => 2)
|
|
|
|
* (just pass the checkbox data from your form)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
function setByCheckboxes(array $checkboxes, array $data) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($checkboxes as $checkbox) {
|
2007-09-14 21:10:18 +02:00
|
|
|
if($data[$checkbox]) {
|
2007-09-14 20:23:28 +02:00
|
|
|
$add[] = $checkbox;
|
2007-09-14 21:10:18 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$remove[] = $checkbox;
|
2007-09-14 20:23:28 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
if($add)
|
|
|
|
$this->addManyByCodename($add);
|
|
|
|
|
|
|
|
if($remove)
|
|
|
|
$this->removeManyByCodename($remove);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Allows you to set groups based on a CheckboxSetField
|
2007-09-14 20:23:28 +02:00
|
|
|
*
|
2007-09-14 21:10:18 +02:00
|
|
|
* Pass the form element from your post data directly to this method, and
|
|
|
|
* it will update the groups and add and remove the member as appropriate.
|
2007-09-14 20:23:28 +02:00
|
|
|
*
|
2007-09-14 21:10:18 +02:00
|
|
|
* On the form setup:
|
2007-09-14 20:23:28 +02:00
|
|
|
*
|
2007-09-14 21:10:18 +02:00
|
|
|
* <code>
|
|
|
|
* $fields->push(
|
|
|
|
* new CheckboxSetField(
|
|
|
|
* "NewsletterSubscriptions",
|
|
|
|
* "Receive email notification of events in ",
|
|
|
|
* $sourceitems = DataObject::get("NewsletterType")->toDropDownMap("GroupID","Title"),
|
|
|
|
* $selectedgroups = $member->Groups()->Map("ID","ID")
|
|
|
|
* )
|
|
|
|
* );
|
|
|
|
* </code>
|
2007-09-14 20:23:28 +02:00
|
|
|
*
|
|
|
|
* On the form handler:
|
|
|
|
*
|
2007-09-14 21:10:18 +02:00
|
|
|
* <code>
|
|
|
|
* $groups = $member->Groups();
|
|
|
|
* $checkboxfield = $form->Fields()->fieldByName("NewsletterSubscriptions");
|
|
|
|
* $groups->setByCheckboxSetField($checkboxfield);
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* @param CheckboxSetField $checkboxsetfield The CheckboxSetField (with
|
|
|
|
* data) from your form.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
function setByCheckboxSetField(CheckboxSetField $checkboxsetfield) {
|
2007-09-14 20:23:28 +02:00
|
|
|
// Get the values from the formfield.
|
2007-07-19 12:40:28 +02:00
|
|
|
$values = $checkboxsetfield->Value();
|
|
|
|
$sourceItems = $checkboxsetfield->getSource();
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if($sourceItems) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// If (some) values are present, add and remove as necessary.
|
2007-09-14 21:10:18 +02:00
|
|
|
if($values) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// update the groups based on the selections
|
2007-09-14 21:10:18 +02:00
|
|
|
foreach($sourceItems as $k => $item) {
|
|
|
|
if(in_array($k,$values)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$add[] = $k;
|
2007-09-14 21:10:18 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$remove[] = $k;
|
2007-09-14 20:23:28 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// else we should be removing all from the necessary groups.
|
2007-09-14 21:10:18 +02:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$remove = $sourceItems;
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
if($add)
|
|
|
|
$this->addManyByGroupID($add);
|
|
|
|
|
|
|
|
if($remove)
|
|
|
|
$this->RemoveManyByGroupID($remove);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
} else {
|
|
|
|
USER_ERROR("Member::setByCheckboxSetField() - No source items could be found for checkboxsetfield " .
|
|
|
|
$checkboxsetfield->Name(), E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Adds this member to the groups based on the group IDs
|
|
|
|
*
|
|
|
|
* @param array $ids Group identifiers.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function addManyByGroupID($groupIds){
|
|
|
|
$groups = $this->getGroupsFromIDs($groupIds);
|
2007-09-14 21:10:18 +02:00
|
|
|
if($groups) {
|
|
|
|
foreach($groups as $group) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->add($group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Removes the member from many groups based on the group IDs
|
|
|
|
*
|
|
|
|
* @param array $ids Group identifiers.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2007-09-14 21:10:18 +02:00
|
|
|
function removeManyByGroupID($groupIds) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$groups = $this->getGroupsFromIDs($groupIds);
|
2007-09-14 21:10:18 +02:00
|
|
|
if($groups) {
|
|
|
|
foreach($groups as $group) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->remove($group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Returns the groups from an array of group IDs
|
|
|
|
*
|
|
|
|
* @param array $ids Group identifiers.
|
|
|
|
* @return mixed Returns the groups from the array of Group IDs.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function getGroupsFromIDs($ids){
|
2007-09-14 21:10:18 +02:00
|
|
|
if($ids && count($ids) > 1) {
|
|
|
|
return DataObject::get("Group", "ID IN (" . implode(",", $ids) . ")");
|
|
|
|
} else {
|
|
|
|
return DataObject::get_by_id("Group", $ids[0]);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Adds this member to the groups based on the group codenames
|
|
|
|
*
|
|
|
|
* @param array $codenames Group codenames
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function addManyByCodename($codenames) {
|
|
|
|
$groups = $this->codenamesToGroups($codenames);
|
2007-09-14 21:10:18 +02:00
|
|
|
if($groups) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($groups as $group){
|
|
|
|
$this->add($group);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Removes this member from the groups based on the group codenames
|
|
|
|
*
|
|
|
|
* @param array $codenames Group codenames
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
function removeManyByCodename($codenames) {
|
|
|
|
$groups = $this->codenamesToGroups($codenames);
|
2007-09-14 21:10:18 +02:00
|
|
|
if($groups) {
|
|
|
|
foreach($groups as $group) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->remove($group);
|
2007-09-14 20:23:28 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Helper function to return the appropriate groups via a codenames
|
|
|
|
*
|
|
|
|
* @param array $codenames Group codenames
|
|
|
|
* @return array Returns the the appropriate groups.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
protected function codenamesToGroups($codenames) {
|
|
|
|
$list = "'" . implode("', '", $codenames) . "'";
|
|
|
|
$output = DataObject::get("Group", "Code IN ($list)");
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Some are missing - throw warnings
|
2007-09-14 21:10:18 +02:00
|
|
|
if(!$output || ($output->Count() != sizeof($list))) {
|
|
|
|
foreach($codenames as $codename)
|
|
|
|
$missing[$codename] = $codename;
|
|
|
|
|
|
|
|
if($output) {
|
|
|
|
foreach($output as $record)
|
|
|
|
unset($missing[$record->Code]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($missing)
|
|
|
|
user_error("The following group-codes aren't matched to any groups: " .
|
|
|
|
implode(", ", $missing) .
|
|
|
|
". You probably need to link up the correct group codes in phpMyAdmin",
|
|
|
|
E_USER_WARNING);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Class used as template to send an email to new members
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Member_SignupEmail extends Email_Template {
|
2007-09-14 20:23:28 +02:00
|
|
|
protected
|
2007-09-14 21:10:18 +02:00
|
|
|
$from = '', // setting a blank from address uses the site's default administrator email
|
2007-07-19 12:40:28 +02:00
|
|
|
$to = '$Email',
|
|
|
|
$subject = "Thanks for signing up",
|
|
|
|
$body = '
|
|
|
|
<h1>Welcome, $FirstName.</h1>
|
|
|
|
<p>Thanks for signing up to become a new member, your details are listed below for future reference.</p>
|
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
<p>You can login to the website using the credentials listed below:
|
2007-07-19 12:40:28 +02:00
|
|
|
<ul>
|
|
|
|
<li><strong>Email:</strong>$Email</li>
|
|
|
|
<li><strong>Password:</strong>$Password</li>
|
|
|
|
</ul>
|
|
|
|
</p>
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
<h3>Contact Information</h3>
|
|
|
|
<ul>
|
|
|
|
<li><strong>Name:</strong> $FirstName $Surname</li>
|
|
|
|
<% if Phone %>
|
|
|
|
<li><strong>Phone:</strong> $Phone</li>
|
|
|
|
<% end_if %>
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
<% if Mobile %>
|
|
|
|
<li><strong>Mobile:</strong> $Mobile</li>
|
|
|
|
<% end_if %>
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
<% if RuralAddressCheck %>
|
|
|
|
<li><strong>Rural Address:</strong>
|
|
|
|
$RapidResponse $Road<br/>
|
|
|
|
$RDNumber<br/>
|
|
|
|
$City $Postcode
|
|
|
|
</li>
|
|
|
|
<% else %>
|
|
|
|
<li><strong>Address:</strong>
|
|
|
|
<br/>
|
|
|
|
$Number $Street $StreetType<br/>
|
|
|
|
$Suburb<br/>
|
|
|
|
$City $Postcode
|
|
|
|
</li>
|
|
|
|
<% end_if %>
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
<% if DriversLicense5A %>
|
|
|
|
<li><strong>Drivers License:</strong> $DriversLicense5A<% if DriversLicense5B %> - $DriversLicense5B <% end_if %></li>
|
|
|
|
<% end_if %>
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
</ul>';
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function MemberData() {
|
|
|
|
return $this->template_data->listOfFields(
|
2007-09-14 21:10:18 +02:00
|
|
|
"FirstName", "Surname", "Email",
|
|
|
|
"Phone", "Mobile", "Street",
|
|
|
|
"Suburb", "City", "Postcode", "DriversLicense5A", "DriversLicense5B"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used as template to send an email saying that the password has been
|
|
|
|
* changed
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Member_ChangePasswordEmail extends Email_Template {
|
|
|
|
protected $from = ''; // setting a blank from address uses the site's default administrator email
|
|
|
|
protected $subject = "Your password has been changed";
|
|
|
|
protected $ss_template = 'ChangePasswordEmail';
|
2007-09-14 20:23:28 +02:00
|
|
|
protected $to = '$Email';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class used as template to send the forgot password email
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Member_ForgotPasswordEmail extends Email_Template {
|
2007-09-14 21:10:18 +02:00
|
|
|
protected $from = ''; // setting a blank from address uses the site's default administrator email
|
2007-09-16 02:32:48 +02:00
|
|
|
protected $subject = "Your password reset link";
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $ss_template = 'ForgotPasswordEmail';
|
2007-09-14 20:23:28 +02:00
|
|
|
protected $to = '$Email';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2007-09-14 21:10:18 +02:00
|
|
|
* Record to keep track of which records a member has unsubscribed from and
|
|
|
|
* when
|
|
|
|
*
|
|
|
|
* @todo Check if that email stuff ($from, $to, $subject, $body) is needed
|
|
|
|
* here! (Markus)
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class Member_UnsubscribeRecord extends DataObject {
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
static $has_one = array(
|
|
|
|
'NewsletterType' => 'NewsletterType',
|
|
|
|
'Member' => 'Member'
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsubscribe the member from a specific newsletter type
|
|
|
|
*
|
|
|
|
* @param int|Member $member Member object or ID
|
|
|
|
* @param int|NewsletterType $newsletterType Newsletter type object or ID
|
|
|
|
*/
|
|
|
|
function unsubscribe($member, $newsletterType) {
|
|
|
|
// $this->UnsubscribeDate()->setVal( 'now' );
|
|
|
|
$this->MemberID = (is_numeric($member))
|
|
|
|
? $member
|
|
|
|
: $member->ID;
|
|
|
|
|
|
|
|
$this->NewsletterTypeID = (is_numeric($newletterType))
|
|
|
|
? $newsletterType
|
|
|
|
: $newsletterType->ID;
|
|
|
|
|
|
|
|
$this->write();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-14 20:23:28 +02:00
|
|
|
protected
|
2007-09-14 21:10:18 +02:00
|
|
|
$from = '', // setting a blank from address uses the site's default administrator email
|
2007-07-19 12:40:28 +02:00
|
|
|
$to = '$Email',
|
|
|
|
$subject = "Your password has been changed",
|
|
|
|
$body = '
|
|
|
|
<h1>Here\'s your new password</h1>
|
|
|
|
<p>
|
|
|
|
<strong>Email:</strong> $Email<br />
|
|
|
|
<strong>Password:</strong> $Password
|
|
|
|
</p>
|
|
|
|
<p>Your password has been changed. Please keep this email, for future reference.</p>';
|
|
|
|
}
|
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Member Validator
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Member_Validator extends RequiredFields {
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
protected $customRequired = array('FirstName', 'Email'); //, 'Password');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
public function __construct() {
|
|
|
|
$required = func_get_args();
|
|
|
|
if(isset($required[0]) && is_array($required[0])) {
|
|
|
|
$required = $required[0];
|
|
|
|
}
|
|
|
|
$required = array_merge($required, $this->customRequired);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
parent::__construct($required);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
/**
|
|
|
|
* Check if the submitted member data is valid
|
|
|
|
*
|
|
|
|
* Check if a member with that email doesn't already exist, or if it does
|
|
|
|
* that it is this member.
|
|
|
|
*
|
|
|
|
* @param array $data Submitted data
|
|
|
|
* @return bool Returns TRUE if the submitted data is valid, otherwise
|
|
|
|
* FALSE.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function php($data) {
|
|
|
|
$valid = parent::php($data);
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-09-14 21:10:18 +02:00
|
|
|
$member = DataObject::get_one('Member',
|
|
|
|
"Email = '". Convert::raw2sql($data['Email']) ."'");
|
|
|
|
|
|
|
|
// if we are in a complex table field popup, use ctf[childID], else use
|
|
|
|
// ID
|
|
|
|
$id = (isset($_REQUEST['ctf']['childID']))
|
|
|
|
? $_REQUEST['ctf']['childID']
|
|
|
|
: $_REQUEST['ID'];
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_object($member) && $member->ID != $id) {
|
|
|
|
$emailField = $this->form->dataFieldByName('Email');
|
2007-09-14 21:10:18 +02:00
|
|
|
$this->validationError($emailField->id(),
|
|
|
|
"There already exists a member with this email",
|
|
|
|
"required");
|
2007-07-19 12:40:28 +02:00
|
|
|
$valid = false;
|
|
|
|
}
|
2007-09-14 20:23:28 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $valid;
|
|
|
|
}
|
|
|
|
}
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-15 23:51:37 +02:00
|
|
|
// Initialize the static DB variables to add the supported encryption
|
|
|
|
// algorithms to the PasswordEncryption Enum field
|
|
|
|
Member::init_db_fields();
|
2007-09-14 21:10:18 +02:00
|
|
|
|
2007-09-15 23:51:37 +02:00
|
|
|
?>
|