1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47:
48: class PersonObject extends AlphaDAO {
49: 50: 51: 52: 53: 54:
55: protected $displayName;
56:
57: 58: 59: 60: 61: 62:
63: protected $email;
64:
65: 66: 67: 68: 69: 70:
71: protected $password;
72:
73: 74: 75: 76: 77: 78:
79: protected $rights;
80:
81: 82: 83: 84: 85: 86:
87: protected $actions;
88:
89: 90: 91: 92: 93: 94:
95: protected $dataLabels = array("OID"=>"Member ID#",
96: "displayName"=>"Display Name",
97: "email"=>"E-mail Address",
98: "password"=>"Password",
99: "state"=>"Account state",
100: "URL"=>"Your site address",
101: "rights"=>"Rights Group Membership",
102: "actions"=>"Actions");
103:
104: 105: 106: 107: 108: 109:
110: const TABLE_NAME = 'Person';
111:
112: 113: 114: 115: 116: 117:
118: protected $state;
119:
120: 121: 122: 123: 124: 125:
126: protected $URL;
127:
128: 129: 130: 131: 132: 133:
134: private static $logger = null;
135:
136: 137: 138: 139: 140:
141: public function __construct() {
142: self::$logger = new Logger('PersonObject');
143: self::$logger->debug('>>__construct()');
144:
145:
146: parent::__construct();
147: $this->displayName = new String();
148: $this->displayName->setRule(AlphaValidator::REQUIRED_USERNAME);
149: $this->displayName->setSize(70);
150: $this->displayName->setHelper('Please provide a name for display on the website (only letters, numbers, and .-_ characters are allowed!).');
151: $this->email = new String();
152: $this->email->setRule(AlphaValidator::REQUIRED_EMAIL);
153: $this->email->setSize(70);
154: $this->email->setHelper('Please provide a valid e-mail address as your username.');
155: $this->password = new String();
156: $this->password->setSize(70);
157: $this->password->setHelper('Please provide a password for logging in.');
158: $this->password->isPassword(true);
159: $this->state = new Enum(array(
160: 'Active',
161: 'Disabled'));
162: $this->state->setValue('Active');
163: $this->URL = new String();
164: $this->URL->setRule(AlphaValidator::OPTIONAL_HTTP_URL);
165: $this->URL->setHelper('URLs must be in the format http://some_domain/ or left blank!');
166:
167: $this->markUnique('displayName');
168: $this->markUnique('email');
169:
170: $this->rights = new Relation();
171: $this->markTransient('rights');
172:
173: $this->actions = new Relation();
174: $this->markTransient('actions');
175:
176: $this->setupRels();
177:
178: self::$logger->debug('<<__construct');
179: }
180:
181: 182: 183: 184: 185:
186: protected function after_load_callback() {
187: $this->setupRels();
188: }
189:
190: 191: 192: 193: 194:
195: protected function after_loadByAttribute_callback() {
196: $this->setupRels();
197: }
198:
199: 200: 201: 202: 203: 204: 205:
206: protected function after_save_callback() {
207: if($this->getVersionNumber()->getValue() == 1) {
208: $standardGroup = new RightsObject();
209:
210: $this->setupRels();
211:
212: if(!$this->inGroup('Standard')) {
213: try {
214: $standardGroup->loadByAttribute('name', 'Standard');
215: }catch (BONotFoundException $e) {
216: $standardGroup->set('name', 'Standard');
217: $standardGroup->save();
218: }
219:
220: $lookup = $this->rights->getLookup();
221: $lookup->setValue(array($this->getID(), $standardGroup->getID()));
222: $lookup->save();
223: }
224: }
225: }
226:
227: 228: 229: 230: 231:
232: protected function before_populateFromPost_callback() {
233: if(isset($_POST['password']) && preg_match(AlphaValidator::REQUIRED_STRING, $_POST['password']))
234: $_POST['password'] = crypt($_POST['password']);
235: }
236:
237: 238: 239: 240: 241:
242: protected function setupRels() {
243:
244: if(isset($this->rights)) {
245: $this->rights->setRelatedClass('PersonObject', 'left');
246: $this->rights->setRelatedClassDisplayField('email', 'left');
247: $this->rights->setRelatedClass('RightsObject', 'right');
248: $this->rights->setRelatedClassDisplayField('name', 'right');
249: $this->rights->setRelationType('MANY-TO-MANY');
250: $this->rights->setValue($this->getID());
251: }
252:
253: if(isset($this->actions)) {
254: $this->actions->setValue($this->OID);
255: $this->actions->setRelatedClass('ActionLogObject');
256: $this->actions->setRelatedClassField('created_by');
257: $this->actions->setRelatedClassDisplayField('message');
258: $this->actions->setRelationType('ONE-TO-MANY');
259: }
260: }
261:
262: 263: 264: 265: 266: 267:
268: public function setDisplayName($displayName) {
269: $this->displayName->setValue($displayName);
270: }
271:
272: 273: 274: 275: 276: 277:
278: public function getDisplayName() {
279: return $this->displayName;
280: }
281:
282: 283: 284: 285: 286: 287: 288:
289: public function inGroup($groupName) {
290: if(self::$logger == null)
291: self::$logger = new Logger('PersonObject');
292: self::$logger->debug('>>inGroup(groupName=['.$groupName.'])');
293:
294: $group = new RightsObject();
295:
296: try {
297: $group->loadByAttribute('name', $groupName);
298: }catch (BONotFoundException $e) {
299: self::$logger->error('Unable to load the group named ['.$groupName.']');
300: self::$logger->debug('<<inGroup [false]');
301: return false;
302: }
303:
304: $rel = $group->getMembers();
305:
306: try {
307:
308: $lookUps = $rel->getLookup()->loadAllByAttribute('leftID', $this->getID());
309: foreach($lookUps as $lookUp) {
310:
311: $ids = $lookUp->getValue();
312:
313: if($ids[1] == $group->getID()) {
314: self::$logger->debug('<<inGroup [true]');
315: return true;
316: }
317: }
318: }catch (BONotFoundException $e) {
319: self::$logger->debug('<<inGroup [false]');
320: return false;
321: }
322:
323: self::$logger->debug('<<inGroup [false]');
324: return false;
325: }
326:
327: 328: 329: 330: 331: 332: 333: 334:
335: public function sendMail($message, $subject) {
336: global $config;
337:
338: $body = '<html><head></head><body><p>Dear '.$this->getDisplayName().',</p>';
339:
340: $body .= $message;
341:
342: $body .= '</body></html>';
343:
344: $headers = 'MIME-Version: 1.0'."\n";
345: $headers .= 'Content-type: text/html; charset=iso-8859-1'."\n";
346: $headers .= "From: ".$config->get('email.reply.to')."\n";
347:
348: try {
349: mail($this->get('email'), $subject, $body, $headers);
350: }catch (PHPException $e) {
351: throw new MailNotSentException('Error sending a mail to ['.$this->get('email').']');
352: }
353: }
354:
355: 356: 357: 358: 359: 360:
361: public function generatePassword() {
362: $alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
363:
364: $newPassword = '';
365: for ($i = 0; $i < 7; $i++) {
366: $newPassword.= $alphabet[rand(0,25)];
367: }
368: $newPassword.= rand(0,100);
369: $newPassword.= rand(0,100);
370:
371: return $newPassword;
372: }
373:
374: 375: 376: 377: 378: 379: 380:
381: public function () {
382: $temp = new ArticleCommentObject();
383:
384: $sqlQuery = "SELECT COUNT(OID) AS post_count FROM ".$temp->getTableName()." WHERE created_by='".$this->OID."';";
385:
386: $result = $this->query($sqlQuery);
387:
388: if(!isset($result[0])) {
389: throw new AlphaException('Failed to get the count of the comments posted for the person ['.$this->getDisplayName().'], query is ['.$sqlQuery.']');
390: return 0;
391: }
392:
393: $row = $result[0];
394:
395: if(isset($row['post_count']))
396: return $row['post_count'];
397: else
398: return 0;
399: }
400: }
401:
402: ?>