/inc/app/sitelinks/lib/Item.php

https://github.com/durand54/sitellite · PHP · 330 lines · 263 code · 39 blank · 28 comment · 27 complexity · 16eb024bf783dc97a12d2243353d6f77 MD5 · raw file

  1. <?php
  2. loader_import ('saf.Database.Generic');
  3. loader_import ('saf.Database.PropertySet');
  4. class SiteLinks_Item extends Generic {
  5. /**
  6. * Constructor method.
  7. */
  8. function SiteLinks_Item () {
  9. parent::Generic ('sitelinks_item', 'id');
  10. $this->usePermissions = true;
  11. if (session_admin ()) {
  12. $this->perms = session_allowed_sql ();
  13. } elseif (session_valid ()) {
  14. $this->perms = '((' . session_approved_sql () . ') or user_id = "' . session_username () . '")';
  15. } else {
  16. $this->perms = session_approved_sql ();
  17. }
  18. //die ($this->perms);
  19. }
  20. function get ($id) {
  21. $item = db_single ('select * from sitelinks_item where id = ? and ' . $this->perms, $id);
  22. if (! $item) {
  23. return $item;
  24. }
  25. $ps = new PropertySet ('sitelinks_item', $id);
  26. foreach ($ps->get () as $k => $v) {
  27. $item->{$k} = $v;
  28. }
  29. $item->views = $this->views ($id);
  30. $item->hits = $this->hits ($id);
  31. $rating = $this->rating ($id);
  32. $item->votes = $rating->votes;
  33. $item->rating = $rating->rating;
  34. return $item;
  35. }
  36. /**
  37. * Returns the number of views of the specified item.
  38. */
  39. function views ($item) {
  40. return db_shift ('select count(*) from sitelinks_view where item_id = ?', $item);
  41. }
  42. /**
  43. * Returns the number of hits of the specified item.
  44. */
  45. function hits ($item) {
  46. return db_shift ('select count(*) from sitelinks_hit where item_id = ?', $item);
  47. }
  48. /**
  49. * Returns an object with the properties $obj->rating and $obj->votes,
  50. * where the rating is the average of all the votes for this item,
  51. * and votes is the number of votes placed.
  52. */
  53. function rating ($item) {
  54. $res = db_single (
  55. 'select avg(rating) as rating, count(*) as votes from sitelinks_rating where item_id = ?',
  56. $item
  57. );
  58. $res->rating = number_format ($res->rating, 2);
  59. return $res;
  60. }
  61. /**
  62. * Increases the number of views of the specified item.
  63. */
  64. function addView ($item) {
  65. $res = db_shift (
  66. 'select count(*) from sitelinks_view where item_id = ? and ts > date_sub(now(), interval 1 day) and ip = ? and ua = ?',
  67. $item,
  68. $_SERVER['REMOTE_ADDR'],
  69. $_SERVER['HTTP_USER_AGENT']
  70. );
  71. if ($res > 0) {
  72. return false;
  73. }
  74. $res = db_execute (
  75. 'insert into sitelinks_view (id, item_id, ts, ip, ua) values (null, ?, now(), ?, ?)',
  76. $item,
  77. $_SERVER['REMOTE_ADDR'],
  78. $_SERVER['HTTP_USER_AGENT']
  79. );
  80. if (! $res) {
  81. $this->error = db_error ();
  82. return false;
  83. }
  84. return true;
  85. }
  86. /**
  87. * Increases the number of hits (aka "click throughs") of the specified item.
  88. */
  89. function addHit ($item) {
  90. $res = db_shift (
  91. 'select count(*) from sitelinks_hit where item_id = ? and ts > date_sub(now(), interval 1 day) and ip = ? and ua = ?',
  92. $item,
  93. $_SERVER['REMOTE_ADDR'],
  94. $_SERVER['HTTP_USER_AGENT']
  95. );
  96. if ($res > 0) {
  97. return false;
  98. }
  99. $res = db_execute (
  100. 'insert into sitelinks_hit (id, item_id, ts, ip, ua) values (null, ?, now(), ?, ?)',
  101. $item,
  102. $_SERVER['REMOTE_ADDR'],
  103. $_SERVER['HTTP_USER_AGENT']
  104. );
  105. if (! $res) {
  106. $this->error = db_error ();
  107. return false;
  108. }
  109. return true;
  110. }
  111. /**
  112. * Determines whether the current visitor has voted for the specified item within the
  113. * past month.
  114. */
  115. function hasVoted ($item) {
  116. return db_shift (
  117. 'select count(*) from sitelinks_rating where item_id = ? and ip = ? and ua = ? and ts > date_sub(now(), interval 1 month)',
  118. $item,
  119. $_SERVER['REMOTE_ADDR'],
  120. $_SERVER['HTTP_USER_AGENT']
  121. );
  122. }
  123. /**
  124. * Makes a rating on the specified item. $rating should be from 0 to 5.
  125. */
  126. function addRating ($item, $rating) {
  127. if ($this->hasVoted ($item)) {
  128. return false;
  129. }
  130. $res = db_execute (
  131. 'insert into sitelinks_rating (id, item_id, rating, ts, ip, ua) values (null, ?, ?, now(), ?, ?)',
  132. $item,
  133. $rating,
  134. $_SERVER['REMOTE_ADDR'],
  135. $_SERVER['HTTP_USER_AGENT']
  136. );
  137. if (! $res) {
  138. $this->error = db_error ();
  139. return false;
  140. }
  141. $r = $this->rating ($item);
  142. loader_import ('cms.Versioning.Rex');
  143. $rex = new Rex ('sitelinks_item');
  144. if (! $rex->collection) {
  145. $this->error = 'SiteLinks collection is not installed. Please copy install/sitelinks_item.php into inc/app/cms/conf/collections.';
  146. return false;
  147. }
  148. $rex->modify ($item, array ('user_rating' => $r->rating));
  149. return true;
  150. }
  151. function getCategories () {
  152. return db_fetch_array ('select category, count(*) as items from sitelinks_item where category != "" and ' . $this->perms . ' group by category asc');
  153. }
  154. function getTypes () {
  155. return db_fetch_array ('select ctype, count(*) as items from sitelinks_item where ctype != "" and ' . $this->perms . ' group by ctype asc');
  156. }
  157. function getCategory ($cat = false, $limit = false, $offset = false) {
  158. if (! $limit) {
  159. if ($cat) {
  160. $res = db_fetch_array (
  161. 'select * from sitelinks_item where category = ? and ' . $this->perms . ' order by rank desc, user_rating desc, ts desc',
  162. $cat
  163. );
  164. } else {
  165. $res = db_fetch_array (
  166. 'select * from sitelinks_item where ' . $this->perms . ' order by rank desc, user_rating desc, ts desc'
  167. );
  168. }
  169. } else {
  170. if ($cat) {
  171. $q = db_query (
  172. 'select * from sitelinks_item where category = ? and ' . $this->perms . ' order by rank desc, user_rating desc, ts desc'
  173. );
  174. $b = array ($cat);
  175. } else {
  176. $q = db_query (
  177. 'select * from sitelinks_item where ' . $this->perms . ' order by rank desc, user_rating desc, ts desc'
  178. );
  179. $b = array ();
  180. }
  181. if ($q->execute ($b)) {
  182. $res = $q->fetch ($offset, $limit);
  183. $this->total = $q->rows ();
  184. $q->free ();
  185. } else {
  186. $this->error = $q->error ();
  187. return false;
  188. }
  189. }
  190. $ps = new PropertySet ('sitelinks_item', false);
  191. foreach (array_keys ($res) as $key) {
  192. $ps->entity = $res[$key]->id;
  193. foreach ($ps->get () as $k => $v) {
  194. $res[$key]->{$k} = $v;
  195. }
  196. }
  197. return $res;
  198. }
  199. function getByType ($type = false, $limit = false, $offset = false) {
  200. if (! $limit) {
  201. if ($type) {
  202. $res = db_fetch_array (
  203. 'select * from sitelinks_item where ctype = ? and ' . $this->perms . ' order by rank desc, user_rating desc, ts desc',
  204. $type
  205. );
  206. } else {
  207. $res = db_fetch_array (
  208. 'select * from sitelinks_item where ' . $this->perms . ' order by rank desc, user_rating desc, ts desc'
  209. );
  210. }
  211. } else {
  212. if ($type) {
  213. $q = db_query (
  214. 'select * from sitelinks_item where ctype = ? and ' . $this->perms . ' order by rank desc, user_rating desc, ts desc'
  215. );
  216. $b = array ($type);
  217. } else {
  218. $q = db_query (
  219. 'select * from sitelinks_item where ' . $this->perms . ' order by rank desc, user_rating desc, ts desc'
  220. );
  221. $b = array ();
  222. }
  223. if ($q->execute ($b)) {
  224. $res = $q->fetch ($offset, $limit);
  225. $this->total = $q->rows ();
  226. $q->free ();
  227. } else {
  228. $this->error = $q->error ();
  229. return false;
  230. }
  231. }
  232. $ps = new PropertySet ('sitelinks_item', false);
  233. foreach (array_keys ($res) as $key) {
  234. $ps->entity = $res[$key]->id;
  235. foreach ($ps->get () as $k => $v) {
  236. $res[$key]->{$k} = $v;
  237. }
  238. }
  239. return $res;
  240. }
  241. function myLinks ($user = false) {
  242. if (! $user) {
  243. $user = session_username ();
  244. }
  245. $res = db_fetch_array (
  246. 'select * from sitelinks_item where user_id = ? order by title asc',
  247. $user
  248. );
  249. $ps = new PropertySet ('sitelinks_item', false);
  250. foreach (array_keys ($res) as $key) {
  251. $ps->entity = $res[$key]->id;
  252. foreach ($ps->get () as $k => $v) {
  253. $res[$key]->{$k} = $v;
  254. }
  255. }
  256. return $res;
  257. }
  258. function getNewest ($limit = 10) {
  259. $res = db_fetch_array (
  260. 'select * from sitelinks_item where ' . $this->perms . ' order by ts desc limit ' . $limit
  261. );
  262. $ps = new PropertySet ('sitelinks_item', false);
  263. foreach (array_keys ($res) as $key) {
  264. $ps->entity = $res[$key]->id;
  265. foreach ($ps->get () as $k => $v) {
  266. $res[$key]->{$k} = $v;
  267. }
  268. }
  269. return $res;
  270. }
  271. function getTop ($limit = 10) {
  272. $res = db_fetch_array (
  273. 'select * from sitelinks_item where ' . $this->perms . ' order by user_rating desc limit ' . $limit
  274. );
  275. $ps = new PropertySet ('sitelinks_item', false);
  276. foreach (array_keys ($res) as $key) {
  277. $r = $this->rating ($res[$key]->id);
  278. $res[$key]->rating = $r->rating;
  279. $ps->entity = $res[$key]->id;
  280. foreach ($ps->get () as $k => $v) {
  281. $res[$key]->{$k} = $v;
  282. }
  283. }
  284. return $res;
  285. }
  286. function getRelated ($item) {
  287. $uid = db_shift ('select user_id from sitelinks_item where id = ?', $item);
  288. return db_fetch_array ('select id, title from sitelinks_item where user_id = ? and id != ? and ' . $this->perms . ' order by title asc limit 5', $uid, $item);
  289. }
  290. }
  291. ?>