Register.php
00001 <?php
00012 class Dsao_Model_User_Register extends Dsao_Model_Abstract
00013 {
00014 protected function _init()
00015 {
00016 $this->_setDefaultTable('user_data');
00017 }
00018
00027 protected function _sendActivationMail(Dsao_User $user, Dsao_Hash_User $activationKey)
00028 {
00029
00030 $content = Dsao_View_Smarty::sprintf
00031 ('user_register_insert_activation_email_content',
00032 array
00033 (
00034 'activationUri' => $activationKey->getUri()
00035 ->getUriString(true, false, true),
00036 ));
00037
00038 $subject = $this->getTranslate()->
00039 _('user_register_insert_activation_email_subject');
00040
00041 $mail = new Dsao_Mail($user, $subject, $content);
00042
00043
00044 return $mail->send();
00045 }
00046
00053 public function activateUser(Dsao_Hash_User $activationKey)
00054 {
00055
00056 $user = new Dsao_User(array('id' => $activationKey->getUri()->idUser));
00057
00058
00059 if (!$user->refresh()->isSynchronous())
00060 {
00061 $this->getLog()->err('user_register_activate_error_invalid_id',
00062 __METHOD__);
00063 }
00064
00065
00066 else if ($user->activated)
00067 {
00068 $this->getLog()->err('user_register_activate_error_activated',
00069 __METHOD__);
00070 }
00071
00072
00073 else if (!$activationKey->isValid('idUser'))
00074 {
00075 $this->getLog()->err('user_register_activate_error_invalid_key',
00076 __METHOD__);
00077 }
00078
00079
00080 else
00081 {
00082 $user->activated = true;
00083
00084 $user->setValid(true)->update(array('activated'));
00085
00086
00087 $activationKey->deleteRow();
00088
00089 return true;
00090 }
00091
00092 return false;
00093 }
00094
00102 public function getUsernameSuggestions($username)
00103 {
00104
00105 $usernameSuggestions = new ArrayObject();
00106
00107
00108
00109 for ($i = 1; (count($usernameSuggestions) < 4); $i++)
00110 {
00111
00112 $suggestions = array
00113 (
00114 $username.$i,
00115 $username.'_'.$i,
00116 $i.$username,
00117 $i.'_'.$username,
00118 );
00119
00120 foreach ($suggestions as $suggestion)
00121 {
00122
00123 if (!$this->_getTable()->select()->where('username = ?', $suggestion)->fetchRow())
00124 {
00125 $usernameSuggestions->append($suggestion);
00126 }
00127 }
00128 }
00129
00130 return $usernameSuggestions;
00131 }
00132
00141 public function isUniqueEmailAddress($emailAddress)
00142 {
00143
00144 return !(bool) $this->_getTable()->select()->where
00145 ('emailAddress = ?', $emailAddress)->fetchRow();
00146 }
00147
00156 public function isUniqueUsername($username)
00157 {
00158
00159 return !(bool) $this->_getTable()->select()
00160 ->where('username = ?', $username)->fetchRow();
00161 }
00162
00169 public function insertUser(Dsao_User $user)
00170 {
00171
00172 $sqlUserData = array
00173 (
00174 'emailAddress' => $user->emailAddress,
00175 'password' => $user->password,
00176 'username' => $user->username,
00177 );
00178
00179
00180 $sqlUserProfile = array
00181 (
00182 'id' => 0,
00183 );
00184
00185 $sqlUserSettings = array
00186 (
00187 'id' => 0,
00188 );
00189
00190
00191 $this->_getTable()->getAdapter()->beginTransaction();
00192
00193 try
00194 {
00195
00196
00197 $sqlUserProfile['id'] = $sqlUserSettings['id'] =
00198 $this->_getTable()->insert($sqlUserData);
00199
00200
00201
00202 $user->setId($sqlUserProfile['id'], false, false);
00203
00204
00205 $this->_getTable('user_profile')->insert($sqlUserProfile);
00206
00207
00208 $this->_getTable('user_settings')->insert($sqlUserSettings);
00209
00210
00211 $activationKey = Dsao_Hash_User::factory(null,
00212 new Dsao_Uri_Http('/user/register/activate', array(
00213 'idUser' => $user->getId(),
00214 )));
00215
00216
00217 $activationKey->insert();
00218
00219
00220 $this->_getTable()->getAdapter()->commit();
00221 }
00222
00223
00224 catch (Zend_Db_Exception $e)
00225 {
00226 $this->_getTable()->getAdapter()->rollback();
00227
00228 $this->getLog()->err('global_error_mysql_write', __METHOD__);
00229
00230 return false;
00231 }
00232
00233
00234 $this->_sendActivationMail($user, $activationKey);
00235 }
00236 }