'/^[-a-z0-9 _]+$/i', //'color' => '/^[a-z0-9]{6}$/i', 'pass' => '/^[a-z0-9]{32}$/i', 'email' => '#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([a-z0-9._-]+\.+[a-z]{2,4}))$#si', ); function getPrimaryKey() { return 'user_id'; } function getTable() { return 'k4_users'; } function getSelectGroupby() { return 'user_id'; } } class UsersRecord extends FARecord { var $_offsets = array(); var $_perms = array(); var $_roles; var $_cache = array(); //-------------------------------------------- // Does this user have permission to do $operation // in permission object $object? //-------------------------------------------- function canDo($operation, $object) { $perms = &$this->get('perms'); return $perms->canDo($operation, $object); } //-------------------------------------------- // Get a users permissions and cache them. //-------------------------------------------- function &getPerms() { if (!isset($this->_cache['perms'])) { $perms = &new k4Permissions($this->_dba, $this->getId()); $this->_perms = &$perms; $this->_cache['perms'] = TRUE; } return $this->_perms; } //-------------------------------------------- // Get a users roles. //-------------------------------------------- function &getRoles() { if (!isset($this->_cache['roles'])) { $finder = &$this->getFinder('user_roles'); $roles = &$finder->findAllByJoin($join); $roles = &new k4Permissions($this->_dba, $this->getId()); $this->_perms = &$perms; $this->_cache['roles'] = TRUE; } return $this->_perms; } //-------------------------------------------- // Give a user a role. //-------------------------------------------- function setRole($role_id) { $finder = &$this->getFinder('user_roles'); $role = &$finder->createRecord(); $role->set('role_id', $role_id); $role->set('user_id', $this->getId()); return $role->save(); } //-------------------------------------------- // Is this user a member or a guest? //-------------------------------------------- function isMember() { return ($this->getId() != K4_GUEST_ID); } //-------------------------------------------- // This user has personally tagged a thread. So, go // and link the tags to the user :D //-------------------------------------------- function addThreadTags($thread_id, $tags = array()) { $tags_to_find = array(); $new_tags = array(); $tag_sql = ""; $sep = ""; //-------------------------------------------- // Loop through the tags passed in and add them // to an array. Also, create some SQL that we can // pass to an IN() statement. //-------------------------------------------- /* $i = 0; foreach($tags as $tag) { //-------------------------------------------- // Format the tag names and restrict the character // set. //-------------------------------------------- $tag = trim($tag); $tag = preg_replace(K4_REGEX_TAGS, "", $tag); $tag = substr_utf($tag, 0, 20); //-------------------------------------------- // If we still have a tag left after the restrictive // formatting :P //-------------------------------------------- if($tag != '') { $new_tags[$tag] = FALSE; $tags_to_find[] = $tag; $i++; } //-------------------------------------------- // Limit the number of allowed tags to 10. //-------------------------------------------- if($i >= 10) { break; } } */ $i = 0; foreach(clean_tags($tags) as $tag) { $new_tags[$tag] = FALSE; $tags_to_find[] = $tag; $i++; } //-------------------------------------------- // Get all of the tags out of the ones the user // has specified that exist. //-------------------------------------------- $finder = &$this->getFinder('tags'); $existing_tags = &$finder->findAllWhere('LOWER(ts.tag_name) IN ('. substr(str_repeat("?,", $i), 0, -1) .')', $tags_to_find); //-------------------------------------------- // Loop through the already existing tags and // build some arrays with info in them. //-------------------------------------------- $num_existing_tags = 0; $tags_to_find = array(); if($existing_tags !== NULL) { while($existing_tags->next()) { $tag = &$existing_tags->current(); $new_tags[$tag->get('tag_name')] = $tag->getId(); $tags_to_find[] = $tag->getId(); $num_existing_tags++; } } //-------------------------------------------- // Delete all of the links that currently link // tags to users to threads for this specific // thread and tag. //-------------------------------------------- $finder = &$this->getFinder('thread_user_tags'); $finder->deleteWhere("user_id=? and thread_id=?", array($this->getId(), $thread_id)); $num_tags_to_add = 0; $existing_tags = array(); $tags_to_add = array(); //-------------------------------------------- // Our final $new_tags array now holds tag names // => tag id's or to FALSE where the tag doesn't // exist yet. Loop through the $new_tags array // and create some SQL queries. //-------------------------------------------- foreach($new_tags as $name => $tag_id) { if(!$tag_id) { $num_tags_to_add++; $tags_to_add[] = $name; } } //-------------------------------------------- // Add the tags to the database that don't yet // exist. (if needed) //-------------------------------------------- if($num_tags_to_add > 0) { $sql = "INSERT INTO k4_tags (tag_name) VALUES ". substr_utf(str_repeat("(?),", $num_tags_to_add), 0, -1); $this->_dba->executeUpdate($sql, $tags_to_add); //-------------------------------------------- // By this point, ALL of the tags exist. Now, // we simply need to get all of their id's and // add them as thread user tags. //-------------------------------------------- $finder = &$this->getFinder('tags'); $these_tags = &$finder->findAllWhere("LOWER(ts.tag_name) IN(". substr_utf(str_repeat("(?),", $num_tags_to_add), 0, -1) .")", $tags_to_add); //-------------------------------------------- // Loop through the tags that we just added. //-------------------------------------------- if($these_tags !== NULL) { while($these_tags->next()) { $tag = &$these_tags->current(); $new_tags[$tag->get('tag_name')] = $tag->getId(); } } } //-------------------------------------------- // Our final $new_tags array now holds tag names // => tag id's or to FALSE where the tag doesn't // exist yet. Loop through the $new_tags array // and create some SQL queries. //-------------------------------------------- $num_tags_to_add = 0; $sql = "INSERT INTO k4_thread_user_tags (thread_id,tag_id,user_id) VALUES "; foreach($new_tags as $name => $tag_id) { if($tag_id) { $num_tags_to_add++; $sql .= "($thread_id,$tag_id,". $this->getId() ."),"; } } //-------------------------------------------- // Add the tags that already exist but arn't yet // linked to this thread and user to the thread_ // user_tags table. (if needed) //-------------------------------------------- if($num_tags_to_add > 0) { $this->_dba->executeUpdate(substr_utf($sql, 0, -1), array()); } //-------------------------------------------- // Now add the bookmark. This might seem a bit // redundant at first but it is a nice way, for in // the future if I want to keep bookmark functionality // but not thread tags, and also to keep track of the // number of times a user has viewed the bookmarked // thread so the thread can be 'ranked' in terms // of popularity. //-------------------------------------------- $finder = &$this->getFinder('bookmarked_threads'); if($finder->findWhere("bt.thread_id=? AND bt.user_id=?", array($thread_id, $this->getId())) === NULL) { $bookmark = &$finder->createRecord(); $bookmark->set('thread_id', $thread_id); $bookmark->set('user_id', $this->getId()); $bookmark->save(); } //-------------------------------------------- // Yay! We're done. //-------------------------------------------- return TRUE; } } ?>