start.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Elgg Market Plugin
  4. * @package market
  5. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
  6. * @author slyhne
  7. * @copyright slyhne 2010-2015
  8. * @link http://tiger-inc.eu
  9. * @version 1.10
  10. */
  11. elgg_register_event_handler('init','system','market_init');
  12. function market_init() {
  13. elgg_register_library('market', elgg_get_plugins_path() . 'market/lib/market.php');
  14. // Add a site navigation item
  15. $item = new ElggMenuItem('market', elgg_echo('market:title'), 'market/category');
  16. elgg_register_menu_item('site', $item);
  17. // Extend owner block menu
  18. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'market_owner_block_menu');
  19. elgg_register_plugin_hook_handler('register', 'menu:page', 'market_page_menu');
  20. // Extend system CSS with our own styles
  21. elgg_extend_view('css/elgg','market/css');
  22. elgg_extend_view('css/admin','market/admincss');
  23. // register the market's JavaScript
  24. $market_js = elgg_get_simplecache_url('js', 'market');
  25. elgg_register_simplecache_view('js/market');
  26. elgg_register_js('market', $market_js, 'footer');
  27. // Add a new widget
  28. elgg_register_widget_type(
  29. 'market',
  30. elgg_echo('market:widget'),
  31. elgg_echo('market:widget:description')
  32. );
  33. // Register a page handler, so we can have nice URLs
  34. elgg_register_page_handler('market','market_page_handler');
  35. // override the default url to view a market object
  36. elgg_register_plugin_hook_handler('entity:url', 'object', 'market_set_url');
  37. // Register entity type
  38. elgg_register_entity_type('object', 'market');
  39. // Register granular notification for this type
  40. elgg_register_notification_event('object', 'market', array('create'));
  41. // Listen to notification events and supply a more useful message
  42. elgg_register_plugin_hook_handler('notify:entity:message', 'object', 'market_notify_message');
  43. // Setup cron job to delete old market posts
  44. elgg_register_plugin_hook_handler('cron', 'daily', 'market_expire_cron_hook');
  45. // Register actions
  46. $action_url = elgg_get_plugins_path() . "market/actions/";
  47. elgg_register_action("market/save", "{$action_url}save.php");
  48. elgg_register_action("market/delete", "{$action_url}delete.php");
  49. elgg_register_action("market/delete_img", "{$action_url}delete_img.php");
  50. }
  51. // market page handler; allows the use of fancy URLs
  52. function market_page_handler($page) {
  53. $pages = dirname(__FILE__) . '/pages/market';
  54. if (!isset($page[1])) {
  55. $page[1] = 'all';
  56. }
  57. if (!isset($page[2])) {
  58. $page[2] = 'all';
  59. }
  60. // Show market sidebar at top of sidebar
  61. elgg_extend_view("page/elements/sidebar", "market/sidebar");
  62. $page_type = $page[0];
  63. switch ($page_type) {
  64. case 'owned':
  65. set_input('username', $page[1]);
  66. include "$pages/owned.php";
  67. break;
  68. /*
  69. case 'friends':
  70. set_input('username' , $page[1]);
  71. include "$pages/friends.php";
  72. break;
  73. */
  74. case 'view':
  75. set_input('marketpost', $page[1]);
  76. include "$pages/view.php";
  77. break;
  78. case 'image':
  79. set_input('guid', $page[1]);
  80. set_input('imagenum', $page[2]);
  81. set_input('size', $page[3]);
  82. set_input('tu', $page[4]);
  83. include "$pages/image.php";
  84. break;
  85. case 'imagepopup':
  86. set_input('guid', $page[1]);
  87. set_input('imagenum', $page[2]);
  88. include "$pages/imagepopup.php";
  89. break;
  90. case 'add':
  91. elgg_load_library('market');
  92. include "$pages/add.php";
  93. break;
  94. case 'edit':
  95. elgg_load_library('market');
  96. set_input('guid', $page[1]);
  97. include "$pages/edit.php";
  98. break;
  99. case 'category':
  100. set_input('cat', $page[1]);
  101. set_input('type', $page[2]);
  102. include "$pages/category.php";
  103. break;
  104. case 'terms':
  105. include "$pages/terms.php";
  106. break;
  107. default:
  108. set_input('cat', $page[1]);
  109. set_input('type', $page[2]);
  110. include "$pages/category.php";
  111. break;
  112. }
  113. return true;
  114. }
  115. // Populates the ->getURL() method for market objects
  116. function market_set_url($hook, $type, $url, $params) {
  117. $entity = $params['entity'];
  118. if (elgg_instanceof($entity, 'object', 'market')) {
  119. $friendly_title = elgg_get_friendly_title($entity->title);
  120. return "market/view/{$entity->guid}/{$friendly_title}";
  121. }
  122. }
  123. // Add to the user block menu
  124. function market_owner_block_menu($hook, $type, $return, $params) {
  125. if (elgg_instanceof($params['entity'], 'user')) {
  126. $url = "market/owned/{$params['entity']->username}";
  127. $item = new ElggMenuItem('market', elgg_echo('market'), $url);
  128. $return[] = $item;
  129. }
  130. return $return;
  131. }
  132. /**
  133. * Add a page menu menu.
  134. *
  135. * @param string $hook
  136. * @param string $type
  137. * @param array $return
  138. * @param array $params
  139. */
  140. function market_page_menu($hook, $type, $return, $params) {
  141. if (elgg_is_logged_in()) {
  142. // only show buttons in market pages
  143. if (elgg_in_context('market')) {
  144. $user = elgg_get_logged_in_user_entity();
  145. $page_owner = elgg_get_page_owner_entity();
  146. if (!$page_owner) {
  147. $page_owner = elgg_get_logged_in_user_entity();
  148. }
  149. if ($page_owner != $user) {
  150. $usertitle = elgg_echo('market:user', array($page_owner->name));
  151. $return[] = new ElggMenuItem('market_owner', $usertitle, 'market/owned/' . $page_owner->username);
  152. //$friendstitle = elgg_echo('market:user:friends', array($page_owner->name));
  153. //$return[] = new ElggMenuItem('2userfriends', $friendstitle, 'market/friends/' . $page_owner->username);
  154. } else {
  155. $return[] = new ElggMenuItem('market_owner', elgg_echo('market:mine'), 'market/owned/' . $user->username);
  156. }
  157. }
  158. }
  159. return $return;
  160. }
  161. // Cron function to delete old market posts
  162. function market_expire_cron_hook($hook, $entity_type, $returnvalue, $params) {
  163. elgg_load_library('market');
  164. $market_ttl = elgg_get_plugin_setting('market_expire','market');
  165. if ($market_ttl == 0) {
  166. return true;
  167. }
  168. $time_limit = strtotime("-$market_ttl months");
  169. $ret = elgg_set_ignore_access(TRUE);
  170. $entities = elgg_get_entities(array(
  171. 'type' => 'object',
  172. 'subtype' => 'market',
  173. 'created_time_upper' => $time_limit,
  174. ));
  175. foreach ($entities as $entity) {
  176. $date = date('j/n-Y', $entity->time_created);
  177. $title = $entity->title;
  178. $owner = $entity->getOwnerEntity();
  179. notify_user($owner->guid,
  180. elgg_get_site_entity()->guid,
  181. elgg_echo('market:expire:subject'),
  182. elgg_echo('market:expire:body', array($owner->name, $title, $date, $market_ttl)),
  183. NULL,
  184. 'site');
  185. // Delete market post incl. pictures
  186. market_delete_post($entity);
  187. }
  188. $ret = elgg_set_ignore_access(FALSE);
  189. }
  190. /**
  191. * Returns the body of a notification message
  192. *
  193. * @param string $hook
  194. * @param string $entity_type
  195. * @param string $returnvalue
  196. * @param array $params
  197. */
  198. function market_notify_message($hook, $entity_type, $returnvalue, $params) {
  199. $entity = $params['entity'];
  200. $to_entity = $params['to_entity'];
  201. $method = $params['method'];
  202. if (($entity instanceof ElggEntity) && ($entity->getSubtype() == 'market')) {
  203. $descr = elgg_get_excerpt($entity->description);
  204. $title = $entity->title;
  205. $owner = $entity->getOwnerEntity();
  206. $market_type = elgg_echo("market:type:{$entity->market_type}");
  207. return elgg_echo('market:notification', array(
  208. $owner->name,
  209. $market_type,
  210. $title,
  211. $descr,
  212. $entity->getURL()
  213. ));
  214. }
  215. return null;
  216. }