Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MODELS ---------------------------------------
- models/user.php
- <?php
- class User extends Doctrine_Record {
- public function setTableDefinition() {
- // this contains the table definition
- $this->hasColumn('username', 'string', 255, array('unique' => 'true'));
- $this->hasColumn('password', 'string', 255);
- $this->hasColumn('karma', 'integer', 4);
- $this->hasColumn('ratings', 'array', 1000);
- }
- public function setUp() {
- // this contains additional methods
- $this->setTableName('user');
- /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
- $this->actAs('Timestampable');
- $this->hasMutator('password', '_encrypt_password');
- // a User has many Resource
- $this->hasMany('Resource', array(
- 'local' => 'id',
- 'foreign' => 'resource_id'
- ));
- // relation a user hasOne Profile User->Profile
- $this->hasOne('Profile', array(
- 'local' => 'id',
- 'foreign' => 'user_id'
- ));
- }
- protected function _encrypt_password($value) {
- $salt = '#*seCrEt!@-*%';
- $this->_set('password', md5($salt . $value));
- }
- }
- models/resource.php
- <?php
- class Resource extends Doctrine_Record {
- public function setTableDefinition() {
- $this->hasColumn('resource_id', 'integer',4); // foreign key for user->resource relation
- $this->hasColumn('user_id', 'integer',4);
- $this->hasColumn('name', 'string', 255, array('unique' => 'true'));
- $this->hasColumn('url', 'string', 255);
- $this->hasColumn('description', 'string', 255);
- $this->hasColumn('karma', 'integer', 4);
- $this->hasColumn('ratings','array', 1000);
- }
- public function setUp() {
- // this contains additional methods
- $this->setTableName('resource');
- /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
- $this->actAs('Timestampable');
- // relation for resources->user
- $this->hasOne('User', array(
- 'local' => 'resource_id',
- 'foreign' => 'id'
- ));
- //relation for resource->comments a resource has many comments
- $this->hasMany('Comment',array(
- 'local' => 'id',
- 'foreign' => 'comment_id'
- ));
- //relation for resource->file
- $this->hasMany('File',array(
- 'local' => 'id',
- 'foreign' => 'file_id'
- ));
- }
- }
- models/profile.php
- <?php
- class Profile extends Doctrine_Record {
- public function setTableDefinition() {
- $this->hasColumn('user_id', 'integer',array('unique' => 'true')); // foreign key for user->profile relation
- $this->hasColumn('title', 'string', 50);
- $this->hasColumn('first_name', 'string', 50);
- $this->hasColumn('last_name', 'string', 50);
- $this->hasColumn('town', 'string', 50);
- $this->hasColumn('country', 'string', 50);
- $this->hasColumn('email', 'string', 255, array('unique' => 'true'));
- $this->hasColumn('phone', 'integer', 20);
- $this->hasColumn('office', 'string', 100);
- $this->hasColumn('webpage', 'string', 100);
- $this->hasColumn('fields_of_research', 'string', 30);
- $this->hasColumn('research_summary', 'string',50);
- $this->hasColumn('institution', 'string', 50);
- $this->hasColumn('publications', 'string', 550);
- }
- public function setUp() {
- $this->setTableName('profile');
- $this->actAs('Timestampable');
- // a Profile has one User
- $this->hasOne('User', array(
- 'local' => 'user_id',
- 'foreign' => 'id'
- ));
- }
- }
- models/file.php
- <?php
- class File extends Doctrine_Record {
- public function setTableDefinition() {
- $this->hasColumn('name', 'string', 255, array('unique' => 'true'));
- $this->hasColumn('url', 'string', 255);
- $this->hasColumn('type', 'string',255);
- }
- public function setUp() {
- // this contains additional methods
- $this->setTableName('file');
- /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
- $this->actAs('Timestampable');
- // relations
- $this->hasOne('User', array(
- 'local' => 'user_id',
- 'foreign' => 'id'
- ));
- // relation for file->resources
- $this->hasOne('Resource',array(
- 'local' => 'file_id',
- 'foreign' => 'id'
- ));
- $this->hasMany('Tag', array(
- 'local' => 'resource_id',
- 'foreign' => 'tag_id',
- 'refClass' => 'ResourceTag'
- ));
- }
- }
- models/comment.php
- <?php
- class Comment extends Doctrine_Record {
- public function setTableDefinition() {
- $this->hasColumn('comment_id', 'integer',4); // foreign key for resource->comment relation
- $this->hasColumn('text', 'string', 255);
- }
- public function setUp() {
- // this contains additional methods
- $this->setTableName('comment');
- /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
- $this->actAs('Timestampable');
- // relation for resources->user, a resource has one user
- $this->hasOne('Resource',array(
- 'local' => 'comment_id',
- 'foreign' => 'id'
- ));
- // relation for comment->subcomment
- $this->hasMany('Subcomment',array(
- 'local' => 'id',
- 'foreign' => 'subcomment_id'
- ));
- }
- }
- models/subcomment.php
- <?php
- class Subcomment extends Doctrine_Record {
- public function setTableDefinition() {
- $this->hasColumn('subcomment_id', 'integer',4);
- $this->hasColumn('text', 'string', 255);
- }
- public function setUp() {
- // this contains additional methods
- $this->setTableName('subcomment');
- /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
- $this->actAs('Timestampable');
- // relation for subcomment->comment
- $this->hasOne('Comment', array(
- 'local' => 'subcomment_id',
- 'foreign' => 'id'
- ));
- }
- }
- and I also have a php5 class in the same directory
- models/current_user.php
- <?php
- class Current_User {
- /** Returns the User object for the logged in user. */
- private static $user;
- private function __construct() {
- }
- /** returns the private $user variable */
- public static function user() {
- if(!isset(self::$user)) {
- // we use the $CI object to load the session library because this is a static class.
- $CI =& get_instance();
- if (!$user_id = $CI->session->userdata('user_id')) {
- return FALSE;
- }
- if (!$u = Doctrine::getTable('User')->find($user_id)) {
- return FALSE;
- }
- self::$user = $u;
- }
- return self::$user;
- }
- /** returns the id of the current logged in user */
- public static function get_user_id() {
- $CI =& get_instance();
- $user_id = $CI->session->userdata('user_id');
- return $user_id;
- }
- public static function get_username() {
- return self::user()->username;
- }
- public static function login($username, $password) {
- // get User object by username
- if ($u = Doctrine::getTable('User')->findOneByUsername($username)) {
- // this mutates (encrypts) the input password
- $u_input = new User();
- $u_input->password = $password;
- // password match (comparing encrypted passwords)
- if ($u->password == $u_input->password) {
- unset($u_input);
- $CI =& get_instance();
- $CI->load->library('session');
- $CI->session->set_userdata('user_id',$u->id);
- self::$user = $u;
- return TRUE;
- }
- unset($u_input);
- }
- // login failed
- return FALSE;
- }
- public function __clone() {
- trigger_error('Clone is not allowed.', E_USER_ERROR);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement