Advertisement
Guest User

Untitled

a guest
May 30th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.58 KB | None | 0 0
  1. MODELS ---------------------------------------
  2. models/user.php
  3. <?php
  4. class User extends Doctrine_Record {
  5.  
  6. public function setTableDefinition() {
  7. // this contains the table definition
  8. $this->hasColumn('username', 'string', 255, array('unique' => 'true'));
  9. $this->hasColumn('password', 'string', 255);
  10. $this->hasColumn('karma', 'integer', 4);
  11. $this->hasColumn('ratings', 'array', 1000);
  12. }
  13.  
  14. public function setUp() {
  15. // this contains additional methods
  16. $this->setTableName('user');
  17. /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
  18. $this->actAs('Timestampable');
  19. $this->hasMutator('password', '_encrypt_password');
  20.  
  21. // a User has many Resource
  22. $this->hasMany('Resource', array(
  23. 'local' => 'id',
  24. 'foreign' => 'resource_id'
  25. ));
  26.  
  27. // relation a user hasOne Profile User->Profile
  28. $this->hasOne('Profile', array(
  29. 'local' => 'id',
  30. 'foreign' => 'user_id'
  31. ));
  32. }
  33.  
  34. protected function _encrypt_password($value) {
  35. $salt = '#*seCrEt!@-*%';
  36. $this->_set('password', md5($salt . $value));
  37. }
  38. }
  39.  
  40. models/resource.php
  41. <?php
  42. class Resource extends Doctrine_Record {
  43.  
  44. public function setTableDefinition() {
  45. $this->hasColumn('resource_id', 'integer',4); // foreign key for user->resource relation
  46. $this->hasColumn('user_id', 'integer',4);
  47. $this->hasColumn('name', 'string', 255, array('unique' => 'true'));
  48. $this->hasColumn('url', 'string', 255);
  49. $this->hasColumn('description', 'string', 255);
  50. $this->hasColumn('karma', 'integer', 4);
  51. $this->hasColumn('ratings','array', 1000);
  52.  
  53. }
  54.  
  55. public function setUp() {
  56. // this contains additional methods
  57. $this->setTableName('resource');
  58. /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
  59. $this->actAs('Timestampable');
  60.  
  61. // relation for resources->user
  62. $this->hasOne('User', array(
  63. 'local' => 'resource_id',
  64. 'foreign' => 'id'
  65. ));
  66.  
  67. //relation for resource->comments a resource has many comments
  68. $this->hasMany('Comment',array(
  69. 'local' => 'id',
  70. 'foreign' => 'comment_id'
  71. ));
  72.  
  73. //relation for resource->file
  74. $this->hasMany('File',array(
  75. 'local' => 'id',
  76. 'foreign' => 'file_id'
  77. ));
  78.  
  79. }
  80. }
  81.  
  82.  
  83. models/profile.php
  84. <?php
  85. class Profile extends Doctrine_Record {
  86. public function setTableDefinition() {
  87. $this->hasColumn('user_id', 'integer',array('unique' => 'true')); // foreign key for user->profile relation
  88. $this->hasColumn('title', 'string', 50);
  89. $this->hasColumn('first_name', 'string', 50);
  90. $this->hasColumn('last_name', 'string', 50);
  91. $this->hasColumn('town', 'string', 50);
  92. $this->hasColumn('country', 'string', 50);
  93. $this->hasColumn('email', 'string', 255, array('unique' => 'true'));
  94. $this->hasColumn('phone', 'integer', 20);
  95. $this->hasColumn('office', 'string', 100);
  96. $this->hasColumn('webpage', 'string', 100);
  97.  
  98. $this->hasColumn('fields_of_research', 'string', 30);
  99. $this->hasColumn('research_summary', 'string',50);
  100. $this->hasColumn('institution', 'string', 50);
  101. $this->hasColumn('publications', 'string', 550);
  102.  
  103. }
  104.  
  105. public function setUp() {
  106. $this->setTableName('profile');
  107. $this->actAs('Timestampable');
  108.  
  109. // a Profile has one User
  110. $this->hasOne('User', array(
  111. 'local' => 'user_id',
  112. 'foreign' => 'id'
  113. ));
  114. }
  115. }
  116. models/file.php
  117. <?php
  118. class File extends Doctrine_Record {
  119.  
  120. public function setTableDefinition() {
  121. $this->hasColumn('name', 'string', 255, array('unique' => 'true'));
  122. $this->hasColumn('url', 'string', 255);
  123. $this->hasColumn('type', 'string',255);
  124. }
  125.  
  126. public function setUp() {
  127. // this contains additional methods
  128. $this->setTableName('file');
  129. /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
  130. $this->actAs('Timestampable');
  131.  
  132. // relations
  133. $this->hasOne('User', array(
  134. 'local' => 'user_id',
  135. 'foreign' => 'id'
  136. ));
  137.  
  138. // relation for file->resources
  139. $this->hasOne('Resource',array(
  140. 'local' => 'file_id',
  141. 'foreign' => 'id'
  142. ));
  143.  
  144.  
  145. $this->hasMany('Tag', array(
  146. 'local' => 'resource_id',
  147. 'foreign' => 'tag_id',
  148. 'refClass' => 'ResourceTag'
  149. ));
  150. }
  151. }
  152. models/comment.php
  153. <?php
  154. class Comment extends Doctrine_Record {
  155.  
  156. public function setTableDefinition() {
  157. $this->hasColumn('comment_id', 'integer',4); // foreign key for resource->comment relation
  158. $this->hasColumn('text', 'string', 255);
  159. }
  160.  
  161. public function setUp() {
  162. // this contains additional methods
  163. $this->setTableName('comment');
  164. /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
  165. $this->actAs('Timestampable');
  166.  
  167. // relation for resources->user, a resource has one user
  168. $this->hasOne('Resource',array(
  169. 'local' => 'comment_id',
  170. 'foreign' => 'id'
  171. ));
  172.  
  173. // relation for comment->subcomment
  174. $this->hasMany('Subcomment',array(
  175. 'local' => 'id',
  176. 'foreign' => 'subcomment_id'
  177. ));
  178. }
  179. }
  180.  
  181. models/subcomment.php
  182. <?php
  183. class Subcomment extends Doctrine_Record {
  184.  
  185. public function setTableDefinition() {
  186. $this->hasColumn('subcomment_id', 'integer',4);
  187. $this->hasColumn('text', 'string', 255);
  188. }
  189.  
  190. public function setUp() {
  191. // this contains additional methods
  192. $this->setTableName('subcomment');
  193. /* adds 2 fields to the table and manages them. They are called created_at and updated_at. */
  194. $this->actAs('Timestampable');
  195.  
  196. // relation for subcomment->comment
  197. $this->hasOne('Comment', array(
  198. 'local' => 'subcomment_id',
  199. 'foreign' => 'id'
  200. ));
  201. }
  202. }
  203.  
  204. and I also have a php5 class in the same directory
  205. models/current_user.php
  206. <?php
  207. class Current_User {
  208. /** Returns the User object for the logged in user. */
  209. private static $user;
  210.  
  211. private function __construct() {
  212.  
  213. }
  214.  
  215. /** returns the private $user variable */
  216. public static function user() {
  217.  
  218. if(!isset(self::$user)) {
  219. // we use the $CI object to load the session library because this is a static class.
  220. $CI =& get_instance();
  221.  
  222. if (!$user_id = $CI->session->userdata('user_id')) {
  223. return FALSE;
  224. }
  225.  
  226. if (!$u = Doctrine::getTable('User')->find($user_id)) {
  227. return FALSE;
  228. }
  229.  
  230. self::$user = $u;
  231. }
  232. return self::$user;
  233. }
  234.  
  235. /** returns the id of the current logged in user */
  236. public static function get_user_id() {
  237. $CI =& get_instance();
  238. $user_id = $CI->session->userdata('user_id');
  239. return $user_id;
  240. }
  241.  
  242. public static function get_username() {
  243. return self::user()->username;
  244. }
  245.  
  246. public static function login($username, $password) {
  247.  
  248. // get User object by username
  249. if ($u = Doctrine::getTable('User')->findOneByUsername($username)) {
  250.  
  251. // this mutates (encrypts) the input password
  252. $u_input = new User();
  253. $u_input->password = $password;
  254.  
  255. // password match (comparing encrypted passwords)
  256. if ($u->password == $u_input->password) {
  257. unset($u_input);
  258.  
  259. $CI =& get_instance();
  260. $CI->load->library('session');
  261. $CI->session->set_userdata('user_id',$u->id);
  262. self::$user = $u;
  263.  
  264. return TRUE;
  265. }
  266.  
  267. unset($u_input);
  268. }
  269.  
  270. // login failed
  271. return FALSE;
  272.  
  273. }
  274.  
  275. public function __clone() {
  276. trigger_error('Clone is not allowed.', E_USER_ERROR);
  277. }
  278.  
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement