/deploy/application/models/admin.php

https://github.com/quancreative/quancreative.com · PHP · 346 lines · 339 code · 2 blank · 5 comment · 0 complexity · 8a167dcae77d2f4e1e2e696aa356ee3b MD5 · raw file

  1. <?php
  2. /*
  3. * Create a user account
  4. *
  5. * @param array $params
  6. */
  7. function create_user($params)
  8. {
  9. $connection = database_connect();
  10. $query = "insert into users
  11. set
  12. first_name = 'liz',
  13. last_name = 'bowman',
  14. email = 'liz.bowman@windwalker.com,
  15. password = 'test',
  16. account_type = 'admin',
  17. created_at = NOW()
  18. ";
  19. }
  20. /*
  21. * returns array of posts from database
  22. *
  23. * @return array
  24. */
  25. function find_user($id)
  26. {
  27. $connection = database_connect();
  28. $query = sprintf('select users.first_name, users.last_name, users.account_type
  29. from
  30. users
  31. where
  32. users.id = %s',
  33. mysql_real_escape_string($id)
  34. );
  35. $result = mysql_query($query);
  36. $number_of_users = mysql_num_rows($result);
  37. if ($number_of_users == 0)
  38. {
  39. return false;
  40. }
  41. $result = mysql_fetch_array($result);
  42. return $result;
  43. }
  44. /*
  45. * returns false if invalid login or it will set the user session and return true
  46. *
  47. * @param $username, $password
  48. * @return boo
  49. */
  50. function login($email, $password)
  51. {
  52. $connection = database_connect();
  53. $query = sprintf("select * from users
  54. where email = '%s' and
  55. password = '%s'
  56. ",
  57. mysql_real_escape_string($email),
  58. mysql_real_escape_string($password)
  59. );
  60. $result = mysql_query($query);
  61. $number_of_users = mysql_num_rows($result);
  62. if ($number_of_users == 0)
  63. {
  64. if (isset($_SESSION['user']))
  65. {
  66. $_SESSION['user'] = null;
  67. }
  68. return false;
  69. }
  70. $row = mysql_fetch_array($result);
  71. $_SESSION['user'] = $row;
  72. return true;
  73. }
  74. /*
  75. * returns array of posts from database
  76. *
  77. * @return array
  78. */
  79. function find_posts()
  80. {
  81. $connection = database_connect();
  82. $query = 'select posts.title, posts.body, posts.user_id, users.first_name
  83. from
  84. posts, users
  85. where
  86. posts.user_id = users.id';
  87. $result = mysql_query($query);
  88. $number_of_posts = mysql_num_rows($result);
  89. if ($number_of_posts == 0)
  90. {
  91. return false;
  92. }
  93. $result = result_to_array($result);
  94. return $result;
  95. }
  96. /*
  97. * returns array of portfolio_pieces from database
  98. *
  99. * @return array
  100. */
  101. function find_portfolio_pieces()
  102. {
  103. $connection = database_connect();
  104. $query = 'select
  105. portfolio_pieces.id,
  106. portfolio_pieces.title,
  107. portfolio_pieces.media,
  108. portfolio_pieces.description,
  109. portfolio_pieces.link,
  110. portfolio_pieces.position,
  111. portfolio_pieces.enable,
  112. portfolio_pieces.user_id
  113. from
  114. portfolio_pieces, users
  115. where
  116. portfolio_pieces.user_id = users.id';
  117. $result = mysql_query($query);
  118. $number_of_portfolio_pieces = mysql_num_rows($result);
  119. if ($number_of_portfolio_pieces == 0)
  120. {
  121. return false;
  122. }
  123. $result = result_to_array($result);
  124. return $result;
  125. }
  126. /*
  127. * Returns array of a single portfolio_piece from database
  128. *
  129. * @return array
  130. */
  131. function find_portfolio_piece($id)
  132. {
  133. $connection = database_connect();
  134. $query = sprintf(
  135. "select
  136. portfolio_pieces.title,
  137. portfolio_pieces.media,
  138. portfolio_pieces.description,
  139. portfolio_pieces.link,
  140. portfolio_pieces.position,
  141. portfolio_pieces.enable,
  142. portfolio_pieces.user_id
  143. from
  144. portfolio_pieces, users
  145. where
  146. portfolio_pieces.user_id = users.id and portfolio_pieces.id = %s
  147. ",
  148. mysql_real_escape_string($id)
  149. );
  150. $result = mysql_query($query);
  151. $number_of_posts = mysql_num_rows($result);
  152. if ($number_of_posts == 0)
  153. {
  154. return false;
  155. }
  156. $row = mysql_fetch_array($result);
  157. return $row;
  158. }
  159. /*
  160. * create a portfolio_piece
  161. *
  162. * @param array $params
  163. * @return bool
  164. */
  165. function create_portfolio_piece($params)
  166. {
  167. $connection = database_connect();
  168. $query = sprintf("insert into portfolio_pieces
  169. set
  170. title = '%s',
  171. media = '%s',
  172. description = '%s',
  173. link = '%s',
  174. position = '%s',
  175. enable = '%s',
  176. user_id = '%s',
  177. created_at = NOW()
  178. ",
  179. mysql_real_escape_string($params['title']),
  180. mysql_real_escape_string($params['media']),
  181. mysql_real_escape_string($params['description']),
  182. mysql_real_escape_string($params['link']),
  183. mysql_real_escape_string($params['position']),
  184. mysql_real_escape_string($params['enable']),
  185. mysql_real_escape_string($params['user_id'])
  186. );
  187. $result = mysql_query($query);
  188. echo '$result :: ' . $result . ' ... <br />';
  189. if (!$result)
  190. {
  191. return false;
  192. }
  193. else
  194. {
  195. return true;
  196. }
  197. }
  198. /*
  199. * updates a portfolio_piece
  200. *
  201. * @param array $params
  202. * @return bool
  203. */
  204. function update_portfolio_piece($params)
  205. {
  206. $connection = database_connect();
  207. $query = sprintf("update portfolio_pieces
  208. set
  209. title = '%s',
  210. media = '%s',
  211. description = '%s',
  212. link = '%s',
  213. position = '%s',
  214. enable = '%s',
  215. user_id = '%s'
  216. where id = %s
  217. ",
  218. mysql_real_escape_string($params['title']),
  219. mysql_real_escape_string($params['media']),
  220. mysql_real_escape_string($params['description']),
  221. mysql_real_escape_string($params['link']),
  222. mysql_real_escape_string($params['position']),
  223. mysql_real_escape_string($params['enable']),
  224. mysql_real_escape_string($params['user_id']),
  225. mysql_real_escape_string($params['id'])
  226. );
  227. $result = mysql_query($query);
  228. if ($result)
  229. {
  230. return true;
  231. }
  232. else
  233. {
  234. return false;
  235. }
  236. }
  237. /*
  238. * delete a portfolio_piece
  239. *
  240. * @param int $id
  241. * @return bool
  242. */
  243. function delete_portfolio_piece($id)
  244. {
  245. $connect = database_connect();
  246. $query = sprintf("delete from portfolio_pieces
  247. where id = %s",
  248. mysql_real_escape_string($id)
  249. );
  250. $result = mysql_query($query);
  251. if ($result)
  252. {
  253. return true;
  254. }
  255. else
  256. {
  257. return false;
  258. }
  259. }
  260. /*
  261. * returns array of a single post
  262. *
  263. * @return array
  264. */
  265. function find_post($id)
  266. {
  267. $connection = database_connect();
  268. $query = sprintf("select posts.title, posts.body, posts.user_id, users.first_name
  269. from
  270. posts, users
  271. where
  272. posts.user_id = users.id and posts.id = %s
  273. ",
  274. mysql_real_escape_string($id)
  275. );
  276. $result = mysql_query($query);
  277. $number_of_posts = mysql_num_rows($result);
  278. if ($number_of_posts == 0)
  279. {
  280. return false;
  281. }
  282. $row = mysql_fetch_array($result);
  283. return $row;
  284. }
  285. ?>