model.php 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  1. <?php
  2. /**
  3. * Elgg event model
  4. *
  5. * @package event_calendar
  6. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
  7. * @author Kevin Jardine <kevin@radagast.biz>
  8. * @copyright Radagast Solutions 2008-2011
  9. * @link http://radagast.biz/
  10. *
  11. */
  12. function event_calendar_get_event_for_edit($event_id) {
  13. if ($event_id && $event = get_entity($event_id)) {
  14. if ($event->canEdit()) {
  15. return $event;
  16. } else {
  17. return false;
  18. }
  19. } else {
  20. return false;
  21. }
  22. }
  23. // converts to time in minutes since midnight
  24. function event_calendar_convert_to_time($hour,$minute,$meridian) {
  25. if ($meridian) {
  26. if ($meridian == 'am') {
  27. if ($hour == 12) {
  28. $hour = 0;
  29. }
  30. } else {
  31. if ($hour < 12) {
  32. $hour += 12;
  33. }
  34. }
  35. }
  36. return 60*$hour+$minute;
  37. }
  38. // returns the event or FALSE
  39. function event_calendar_set_event_from_form($event_guid,$group_guid) {
  40. $event_calendar_times = elgg_get_plugin_setting('times', 'event_calendar');
  41. $event_calendar_region_display = elgg_get_plugin_setting('region_display', 'event_calendar');
  42. $event_calendar_type_display = elgg_get_plugin_setting('type_display', 'event_calendar');
  43. $event_calendar_spots_display = elgg_get_plugin_setting('spots_display', 'event_calendar');
  44. $event_calendar_hide_end = elgg_get_plugin_setting('hide_end', 'event_calendar');
  45. $event_calendar_more_required = elgg_get_plugin_setting('more_required', 'event_calendar');
  46. $event_calendar_personal_manage = elgg_get_plugin_setting('personal_manage', 'event_calendar');
  47. $event_calendar_repeating_events = elgg_get_plugin_setting('repeating_events', 'event_calendar');
  48. $schedule_type = get_input('schedule_type');
  49. if ($event_calendar_more_required == 'yes') {
  50. $required_fields = array('title','venue','start_date',
  51. 'brief_description','fees','contact','organiser',
  52. 'tags');
  53. if ($event_calendar_times != 'no') {
  54. $required_fields[] = 'start_time';
  55. if ($event_calendar_hide_end != 'yes') {
  56. $required_fields[] = 'end_time';
  57. }
  58. }
  59. if ($event_calendar_region_display == 'yes') {
  60. $required_fields[] = 'region';
  61. }
  62. if ($event_calendar_type_display == 'yes') {
  63. $required_fields[] = 'event_type';
  64. }
  65. if ($event_calendar_spots_display == 'yes') {
  66. $required_fields[] = 'spots';
  67. }
  68. } else {
  69. $required_fields = array('title');
  70. }
  71. if ($event_guid) {
  72. $event = get_entity($event_guid);
  73. if (!elgg_instanceof($event, 'object', 'event_calendar')) {
  74. // do nothing because this is a bad event guid
  75. return FALSE;
  76. }
  77. } else {
  78. $user_guid = elgg_get_logged_in_user_guid();
  79. $event = new ElggObject();
  80. $event->subtype = 'event_calendar';
  81. $event->owner_guid = $user_guid;
  82. if ($group_guid) {
  83. $event->container_guid = $group_guid;
  84. } else {
  85. $event->container_guid = $event->owner_guid;
  86. }
  87. }
  88. $event->access_id = get_input('access_id');
  89. $event->title = get_input('title');
  90. $event->description = get_input('description');
  91. $event->venue = get_input('venue');
  92. if ($schedule_type != 'poll') {
  93. $start_date_text = trim(get_input('start_date'));
  94. /*$event->original_start_date = get_input('start_date');
  95. //$end_date = trim(get_input('end_date',''));
  96. // convert start date from current server time to GMT
  97. $start_date_text = gmdate("Y-m-d",$start_date);
  98. //$event->munged_start_date_string = $start_date_text." ".date_default_timezone_get();*/
  99. // TODO: is the timezone bit necessary?
  100. $event->start_date = strtotime($start_date_text." GMT");
  101. $end_date_text = trim(get_input('end_date',''));
  102. //$event->original_end_date = get_input('end_date');
  103. if ($end_date_text) {
  104. $event->end_date = strtotime($end_date_text." GMT");
  105. //$event->munged_end_date_string = $end_date_text." ".date_default_timezone_get();
  106. } else {
  107. $event->end_date = '';
  108. }
  109. if ($event_calendar_times != 'no') {
  110. $hour = get_input('start_time_hour','');
  111. $minute = get_input('start_time_minute','');
  112. $meridian = get_input('start_time_meridian','');
  113. if (is_numeric($hour) && is_numeric($minute)) {
  114. $event->start_time = event_calendar_convert_to_time($hour,$minute,$meridian);
  115. } else {
  116. $event->start_time = '';
  117. }
  118. $hour = get_input('end_time_hour','');
  119. $minute = get_input('end_time_minute','');
  120. $meridian = get_input('end_time_meridian','');
  121. if (is_numeric($hour) && is_numeric($minute)) {
  122. $event->end_time = event_calendar_convert_to_time($hour,$minute,$meridian);
  123. } else {
  124. $event->end_time = '';
  125. }
  126. if (is_numeric($event->start_date) && is_numeric($event->start_time)) {
  127. // Set start date to the Unix start time, if set.
  128. // This allows sorting by date *and* time.
  129. $event->start_date += $event->start_time*60;
  130. }
  131. }
  132. }
  133. if ($event_calendar_spots_display == 'yes') {
  134. $event->spots = trim(get_input('spots'));
  135. }
  136. if ($event_calendar_region_display == 'yes') {
  137. $event->region = get_input('region');
  138. }
  139. if ($event_calendar_type_display == 'yes') {
  140. $event->event_type = get_input('event_type');
  141. }
  142. if ($event_calendar_personal_manage == 'by_event') {
  143. $event->personal_manage = get_input('personal_manage');
  144. }
  145. if ($event_calendar_repeating_events != 'no') {
  146. $repeats = get_input('repeats');
  147. $event->repeats = $repeats;
  148. if ($repeats == 'yes') {
  149. $event->repeat_interval = get_input('repeat_interval');
  150. $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
  151. foreach ($dow as $w) {
  152. $v = 'event-calendar-repeating-'.$w.'-value';
  153. $event->$v = get_input($v);
  154. }
  155. }
  156. }
  157. $event->fees = get_input('fees');
  158. $event->contact = get_input('contact');
  159. $event->organiser = get_input('organiser');
  160. $event->tags = string_to_tag_array(get_input('tags'));
  161. $event->long_description = get_input('long_description');
  162. $event->schedule_type = $schedule_type;
  163. $event->send_reminder = get_input('send_reminder');
  164. $event->reminder_number = get_input('reminder_number');
  165. $event->reminder_interval = get_input('reminder_interval');
  166. $event->web_conference = get_input('web_conference');
  167. $event->real_end_time = event_calendar_get_end_time($event);
  168. foreach ($required_fields as $fn) {
  169. if (!trim($event->$fn)) {
  170. return FALSE;
  171. break;
  172. }
  173. }
  174. if ($event->save()) {
  175. if (!$event_guid && $event->web_conference) {
  176. if (!event_calendar_create_bbb_conf($event)) {
  177. register_error(elgg_echo('event_calendar:conference_create_error'));
  178. }
  179. }
  180. if ($group_guid && (elgg_get_plugin_setting('autogroup', 'event_calendar') == 'yes')) {
  181. event_calendar_add_personal_events_from_group($event->guid,$group_guid);
  182. }
  183. if (elgg_get_plugin_setting('add_users', 'event_calendar') == 'yes') {
  184. if (function_exists('autocomplete_member_to_user')) {
  185. $addusers = get_input('adduser',array());
  186. foreach($addusers as $adduser) {
  187. if ($adduser) {
  188. $user = autocomplete_member_to_user($adduser);
  189. $user_id = $user->guid;
  190. event_calendar_add_personal_event($event->guid,$user_id);
  191. if (elgg_get_plugin_setting('add_users_notify', 'event_calendar') == 'yes') {
  192. notify_user($user_id, $CONFIG->site->guid, elgg_echo('event_calendar:add_users_notify:subject'),
  193. sprintf(
  194. elgg_echo('event_calendar:add_users_notify:body'),
  195. $user->name,
  196. $event->title,
  197. $event->getURL()
  198. )
  199. );
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. return $event;
  207. }
  208. function event_calendar_get_events_between($start_date,$end_date,$is_count=FALSE,$limit=10,$offset=0,$container_guid=0,$region='-') {
  209. $polls_supported = elgg_is_active_plugin('event_poll');
  210. if ($is_count) {
  211. $count = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
  212. $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,true,$region);
  213. return $count;
  214. } else {
  215. $events = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
  216. $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,false,$region);
  217. $repeating_events = event_calendar_get_repeating_events_between($start_date,$end_date,$container_guid,$region);
  218. $all_events = event_calendar_merge_repeating_events($events, $repeating_events);
  219. if ($polls_supported) {
  220. elgg_load_library('elgg:event_poll');
  221. $all_events = event_poll_merge_poll_events($all_events,$start_date,$end_date);
  222. }
  223. return $all_events;
  224. }
  225. }
  226. function event_calendar_merge_repeating_events($events, $repeating_events) {
  227. $non_repeating_events = array();
  228. foreach($events as $e) {
  229. if ($e->repeats != 'yes') {
  230. $non_repeating_events[] = array('event' => $e,'data' => array(array('start_time' => $e->start_date, 'end_time' => $e->real_end_time)));
  231. }
  232. }
  233. return array_merge($non_repeating_events, $repeating_events);
  234. }
  235. function event_calendar_get_repeating_events_between($start_date,$end_date,$container_guid,$region) {
  236. // game plan: get all repeating events with start date <= $end_date and then generate all possible events
  237. // sanity check
  238. if ($start_date <= $end_date) {
  239. $options = array(
  240. 'type' => 'object',
  241. 'subtype' => 'event_calendar',
  242. 'limit' => 0,
  243. 'metadata_name_value_pairs' => array(
  244. array(
  245. 'name' => 'start_date',
  246. 'value' => $end_date,
  247. 'operand' => '<='
  248. ),
  249. array(
  250. 'name' => 'repeats',
  251. 'value' => 'yes'
  252. ),
  253. )
  254. );
  255. if ($container_guid) {
  256. if (is_array($container_guid)) {
  257. $options['container_guids'] = $container_guid;
  258. } else {
  259. $options['container_guid'] = $container_guid;
  260. }
  261. }
  262. if ($region && $region != '-') {
  263. $options['metadata_name_value_pairs'][] = array(
  264. 'name' => 'region',
  265. 'value' => $region
  266. );
  267. }
  268. $events = elgg_get_entities_from_metadata($options);
  269. }
  270. return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
  271. }
  272. function event_calendar_get_repeating_event_structure($events, $start_date, $end_date) {
  273. $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
  274. $repeating_events = array();
  275. if ($events) {
  276. foreach($events as $e) {
  277. $incs = array();
  278. $repeat_data = array();
  279. $day_num = date('N',$e->start_date)-1;
  280. for($d=0;$d<7;$d++) {
  281. $fn = 'event-calendar-repeating-'.$dow[$d].'-value';
  282. if ($e->$fn) {
  283. $increment = $d - $day_num;
  284. $incs[] = $increment;
  285. }
  286. }
  287. if ($incs) {
  288. sort($incs);
  289. $repeat_interval = $e->repeat_interval;
  290. $event_start_time = $e->start_date;
  291. $event_end_time = $e->real_end_time;
  292. $week = 0;
  293. if ($event_start_time <= $event_end_time) {
  294. $more_to_do = TRUE;
  295. $cur_start_time = $event_start_time;
  296. $cur_end_time = $event_end_time;
  297. // keep generating events until after $end_date
  298. // repeat_times is a sanity check to prevent infinite loops in case of bad data
  299. $repeat_times = 0;
  300. do {
  301. foreach($incs as $inc) {
  302. //$seconds = $inc*60*60*24;
  303. if ($inc >=0) {
  304. $tinc = "+ " . $inc;
  305. } else {
  306. $tinc = $inc;
  307. }
  308. $this_start_time = strtotime($tinc . " day", $cur_start_time);
  309. $this_end_time = strtotime($tinc . " day", $cur_end_time);
  310. if ($this_start_time > $end_date) {
  311. $more_to_do = FALSE;
  312. break;
  313. }
  314. if ($this_start_time >= $event_start_time) {
  315. $repeat_data[] = array (
  316. 'start_time' => $this_start_time,
  317. 'end_time' => $this_end_time,
  318. );
  319. }
  320. }
  321. // repeat_interval weeks later
  322. $week += $repeat_interval;
  323. $cur_start_time = strtotime("+" . $week . " week", $event_start_time);
  324. $cur_end_time = strtotime("+" . $week ." week", $event_end_time);
  325. $repeat_times += 1;
  326. } while ($repeat_times < 1000 && $more_to_do);
  327. }
  328. }
  329. $repeating_events[] = array('event'=>$e,'data'=>$repeat_data);
  330. }
  331. }
  332. return $repeating_events;
  333. }
  334. function event_calendar_get_open_events_between($start_date,$end_date,
  335. $is_count,$limit=10,$offset=0,$container_guid=0,$region='-', $meta_max = 'spots', $annotation_name = 'personal_event') {
  336. if ($is_count) {
  337. $count = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
  338. $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,true,$region,$meta_max,$annotation_name);
  339. return $count;
  340. } else {
  341. $events = event_calendar_get_entities_from_metadata_between2('start_date','end_date',
  342. $start_date, $end_date, "object", "event_calendar", 0, $container_guid, $limit,$offset,"",0,false,false,$region,$meta_max,$annotation_name);
  343. //return event_calendar_vsort($events,'start_date');
  344. $repeating_events = event_calendar_get_open_repeating_events_between($start_date,$end_date,$container_guid,$region);
  345. $all_events = event_calendar_merge_repeating_events($events, $repeating_events);
  346. return $all_events;
  347. }
  348. }
  349. function event_calendar_get_open_repeating_events_between($start_date,$end_date,$container_guid,$region) {
  350. $db_prefix = elgg_get_config('dbprefix');
  351. $meta_max = 'spots';
  352. $annotation_name = 'personal_event';
  353. $joins = array();
  354. $wheres = array();
  355. $meta_max_n = get_metastring_id($meta_max);
  356. $ann_n = get_metastring_id($annotation_name);
  357. if (!$meta_max_n || !$ann_n) {
  358. if ($count) {
  359. return 0;
  360. } else {
  361. return false;
  362. }
  363. }
  364. $joins[] = "LEFT JOIN {$dbprefix}metadata m4 ON (e.guid = m4.entity_guid AND m4.name_id=$meta_max_n) ";
  365. $joins[] = "LEFT JOIN {$dbprefix}metastrings ms4 ON (m4.value_id = ms4.id) ";
  366. $wheres[] = "((ms4.string is null) OR (ms4.string = \"\") OR (CONVERT(ms4.string,SIGNED) > (SELECT count(id) FROM {$dbprefix}annotations ann WHERE ann.entity_guid = e.guid AND name_id = $ann_n GROUP BY entity_guid)))";
  367. // sanity check
  368. if ($start_date <= $end_date) {
  369. $options = array(
  370. 'type' => 'object',
  371. 'subtype' => 'event_calendar',
  372. 'limit' => 0,
  373. 'metadata_name_value_pairs' => array(
  374. array(
  375. 'name' => 'start_date',
  376. 'value' => $end_date,
  377. 'operand' => '<='
  378. ),
  379. array(
  380. 'name' => 'repeats',
  381. 'value' => 'yes'
  382. ),
  383. ),
  384. 'joins' => $joins,
  385. 'wheres' => $wheres,
  386. );
  387. if ($container_guid) {
  388. if (is_array($container_guid)) {
  389. $options['container_guids'] = $container_guid;
  390. } else {
  391. $options['container_guid'] = $container_guid;
  392. }
  393. }
  394. if ($region && $region != '-') {
  395. $options['metadata_name_value_pairs'][] = array(
  396. 'name' => 'region',
  397. 'value' => $region
  398. );
  399. }
  400. $events = elgg_get_entities_from_metadata($options);
  401. }
  402. return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
  403. }
  404. function event_calendar_get_events_for_user_between($start_date,$end_date,$is_count,$limit=10,$offset=0,$user_guid,$container_guid=0,$region='-') {
  405. if ($is_count) {
  406. // old way
  407. $count = event_calendar_get_entities_from_metadata_between('start_date','end_date',
  408. $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,true,$region);
  409. return $count;
  410. } else {
  411. $events = event_calendar_get_entities_from_metadata_between('start_date','end_date',
  412. $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,false,$region);
  413. //return event_calendar_vsort($events,'start_date');
  414. return $events;
  415. }
  416. }
  417. function event_calendar_get_events_for_user_between2($start_date,$end_date,$is_count,$limit=10,$offset=0,$user_guid,$container_guid=0,$region='-') {
  418. $options_new_way = array(
  419. 'type' => 'object',
  420. 'subtype' => 'event_calendar',
  421. 'relationship' => 'personal_event',
  422. 'relationship_guid' => $user_guid,
  423. 'metadata_name_value_pairs' => array( array( 'name' => 'start_date',
  424. 'value' => $start_date,
  425. 'operand' => '>='),
  426. array( 'name' => 'real_end_time',
  427. 'value' => $end_date,
  428. 'operand' => '<=')
  429. ),
  430. );
  431. if ($container_guid) {
  432. $options_new_way['container_guid'] = $container_guid;
  433. }
  434. if ($region && $region != '-') {
  435. $options_new_way['metadata_name_value_pairs'][] = array('name'=>'region','value'=>sanitize_string($region));
  436. }
  437. if ($is_count) {
  438. // old way
  439. $count_old_way = event_calendar_get_entities_from_metadata_between('start_date','real_end_time',
  440. $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,true,$region);
  441. // new way
  442. $options_new_way['count'] = TRUE;
  443. $count_new_way = elgg_get_entities_from_relationship($options_new_way);
  444. return $count_old_way+$count_new_way;
  445. } else {
  446. $events_old_way = event_calendar_get_entities_from_metadata_between('start_date','real_end_time',
  447. $start_date, $end_date, "object", "event_calendar", $user_guid, $container_guid, $limit,$offset,"",0,true,false,$region);
  448. $options_new_way['limit'] = $limit;
  449. $options_new_way['offset'] = $offset;
  450. $options_new_way['order_by_metadata'] = array(array('name'=>'start_date','direction'=>'ASC','as'=>'integer'));
  451. //print_r($options_new_way);
  452. $events_new_way = elgg_get_entities_from_relationship($options_new_way);
  453. //return event_calendar_vsort($events,'start_date');
  454. $repeating_events = event_calendar_get_repeating_events_for_user_between($user_guid,$start_date,$end_date,$container_guid,$region);
  455. $all_events = event_calendar_merge_repeating_events(array_merge($events_old_way,$events_new_way), $repeating_events);
  456. return $all_events;
  457. }
  458. }
  459. function event_calendar_get_repeating_events_for_user_between($user_guid,$start_date,$end_date,$container_guid,$region) {
  460. $options = array(
  461. 'type' => 'object',
  462. 'subtype' => 'event_calendar',
  463. 'relationship' => 'personal_event',
  464. 'relationship_guid' => $user_guid,
  465. 'metadata_name_value_pairs' => array(
  466. array(
  467. 'name' => 'start_date',
  468. 'value' => $end_date,
  469. 'operand' => '<='
  470. ),
  471. array(
  472. 'name' => 'repeats',
  473. 'value' => 'yes'
  474. ),
  475. )
  476. );
  477. if ($container_guid) {
  478. if (is_array($container_guid)) {
  479. $options['container_guids'] = $container_guid;
  480. } else {
  481. $options['container_guid'] = $container_guid;
  482. }
  483. }
  484. if ($region && $region != '-') {
  485. $options['metadata_name_value_pairs'][] = array(
  486. 'name' => 'region',
  487. 'value' => $region
  488. );
  489. }
  490. $events = elgg_get_entities_from_relationship($options);
  491. return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
  492. }
  493. function event_calendar_get_repeating_events_for_friends_between($user_guid,$friend_list,$start_date,$end_date,$container_guid=0,$region='-') {
  494. $db_prefix = elgg_get_config('dbprefix');
  495. $options = array(
  496. 'type' => 'object',
  497. 'subtype' => 'event_calendar',
  498. 'metadata_name_value_pairs' => array(
  499. array( 'name' => 'start_date',
  500. 'value' => $end_date,
  501. 'operand' => '<='
  502. ),
  503. array( 'name' => 'repeats',
  504. 'value' => 'yes'
  505. )
  506. ),
  507. 'joins' => array("JOIN {$db_prefix}entity_relationships r ON (r.guid_two = e.guid)"),
  508. 'wheres' => array("r.relationship = 'personal_event'","r.guid_one IN ($friend_list)"),
  509. );
  510. if ($container_guid) {
  511. if (is_array($container_guid)) {
  512. $options['container_guids'] = $container_guid;
  513. } else {
  514. $options['container_guid'] = $container_guid;
  515. }
  516. }
  517. if ($region && $region != '-') {
  518. $options['metadata_name_value_pairs'][] = array('name'=>'region','value'=>sanitize_string($region));
  519. }
  520. $events = elgg_get_entities_from_relationship($options);
  521. return event_calendar_get_repeating_event_structure($events, $start_date, $end_date);
  522. }
  523. function event_calendar_get_events_for_friends_between($start_date,$end_date,$is_count,$limit=10,$offset=0,$user_guid,$container_guid=0,$region='-') {
  524. if ($user_guid) {
  525. $friends = get_user_friends($user_guid,"",5000);
  526. if ($friends) {
  527. $friend_guids = array();
  528. foreach($friends as $friend) {
  529. $friend_guids[] = $friend->getGUID();
  530. }
  531. $friend_list = implode(",",$friend_guids);
  532. // elgg_get_entities_from_relationship does not take multiple relationship guids, so need some custom joins and wheres
  533. $db_prefix = elgg_get_config('dbprefix');
  534. $options_new_way = array(
  535. 'type' => 'object',
  536. 'subtype' => 'event_calendar',
  537. 'metadata_name_value_pairs' => array(array( 'name' => 'start_date',
  538. 'value' => $start_date,
  539. 'operand' => '>='),
  540. array( 'name' => 'real_end_time',
  541. 'value' => $end_date,
  542. 'operand' => '<=')
  543. ),
  544. 'joins' => array("JOIN {$db_prefix}entity_relationships r ON (r.guid_two = e.guid)"),
  545. 'wheres' => array("r.relationship = 'personal_event'","r.guid_one IN ($friend_list)"),
  546. );
  547. if ($container_guid) {
  548. $options_new_way['container_guid'] = $container_guid;
  549. }
  550. if ($region && $region != '-') {
  551. $options_new_way['metadata_name_value_pairs'][] = array('name'=>'region','value'=>sanitize_string($region));
  552. }
  553. if ($is_count) {
  554. $count_old_way = event_calendar_get_entities_from_metadata_between('start_date','end_date',
  555. $start_date, $end_date, "object", "event_calendar", $friend_guids, $container_guid, $limit,$offset,"",0,true,true,$region);
  556. $options_new_way['count'] = TRUE;
  557. $count_new_way = elgg_get_entities_from_metadata($options_new_way);
  558. return $count_old_way + $count_new_way;
  559. } else {
  560. $events_old_way = event_calendar_get_entities_from_metadata_between('start_date','end_date',
  561. $start_date, $end_date, "object", "event_calendar", $friend_guids, $container_guid, $limit,$offset,"",0,true,false,$region);
  562. //return event_calendar_vsort($events,'start_date');
  563. $options_new_way['limit'] = $limit;
  564. $options_new_way['offset'] = $offset;
  565. $options_new_way['order_by_metadata'] = array(array('name'=>'start_date','direction'=>'ASC','as'=>'integer'));
  566. //print_r($options_new_way);
  567. $events_new_way = elgg_get_entities_from_metadata($options_new_way);
  568. $repeating_events = event_calendar_get_repeating_events_for_friends_between($user_guid,$friend_list,$start_date,$end_date,$container_guid,$region);
  569. $all_events = event_calendar_merge_repeating_events(array_merge($events_old_way,$events_new_way), $repeating_events);
  570. return $all_events;
  571. }
  572. }
  573. }
  574. return array();
  575. }
  576. function event_calendar_vsort($original,$field, $descending = false) {
  577. if (!$original) {
  578. return $original;
  579. }
  580. $sortArr = array();
  581. foreach ( $original as $key => $item ) {
  582. $sortArr[ $key ] = $item->$field;
  583. }
  584. if ( $descending ) {
  585. arsort( $sortArr );
  586. } else {
  587. asort( $sortArr );
  588. }
  589. $resultArr = array();
  590. foreach ( $sortArr as $key => $value ) {
  591. $resultArr[ $key ] = $original[ $key ];
  592. }
  593. return $resultArr;
  594. }
  595. // TODO - replace with Elgg API if possible
  596. /**
  597. * Return a list of entities based on the given search criteria.
  598. * In this case, returns entities with the given metadata between two values inclusive
  599. *
  600. * @param mixed $meta_start_name
  601. * @param mixed $meta_end_name
  602. * @param mixed $meta_start_value - start of metadata range, must be numerical value
  603. * @param mixed $meta_end_value - end of metadata range, must be numerical value
  604. * @param string $entity_type The type of entity to look for, eg 'site' or 'object'
  605. * @param string $entity_subtype The subtype of the entity.
  606. * @param mixed $owner_guid Either one integer user guid or an array of user guids
  607. * @param int $container_guid If supplied, the result is restricted to events associated with a specific container
  608. * @param int $limit
  609. * @param int $offset
  610. * @param string $order_by Optional ordering.
  611. * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
  612. * @param boolean $filter Filter by events in personal calendar if true
  613. * @param true|false $count If set to true, returns the total number of entities rather than a list. (Default: false)
  614. *
  615. * @return int|array A list of entities, or a count if $count is set to true
  616. */
  617. function event_calendar_get_entities_from_metadata_between($meta_start_name, $meta_end_name, $meta_start_value, $meta_end_value, $entity_type = "", $entity_subtype = "", $owner_guid = 0, $container_guid = 0, $limit = 10, $offset = 0, $order_by = "", $site_guid = 0, $filter = false, $count = false, $region='-')
  618. {
  619. global $CONFIG;
  620. // This should not be possible, but a sanity check just in case
  621. if (!is_numeric($meta_start_value) || !is_numeric($meta_end_value)) {
  622. return FALSE;
  623. }
  624. $meta_start_n = get_metastring_id($meta_start_name);
  625. $meta_end_n = get_metastring_id($meta_end_name);
  626. if ($region && $region != '-') {
  627. $region_n = get_metastring_id('region');
  628. $region_value_n = get_metastring_id($region);
  629. if (!$region_n || !$region_value_n) {
  630. if ($count) {
  631. return 0;
  632. } else {
  633. return FALSE;
  634. }
  635. }
  636. }
  637. $entity_type = sanitise_string($entity_type);
  638. $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
  639. $limit = (int)$limit;
  640. $offset = (int)$offset;
  641. //if ($order_by == "") $order_by = "e.time_created desc";
  642. if ($order_by == "") $order_by = "v.string asc";
  643. $order_by = sanitise_string($order_by);
  644. $site_guid = (int) $site_guid;
  645. if ((is_array($owner_guid) && (count($owner_guid)))) {
  646. foreach($owner_guid as $key => $guid) {
  647. $owner_guid[$key] = (int) $guid;
  648. }
  649. } else {
  650. $owner_guid = (int) $owner_guid;
  651. }
  652. if ((is_array($container_guid) && (count($container_guid)))) {
  653. foreach($container_guid as $key => $guid) {
  654. $container_guid[$key] = (int) $guid;
  655. }
  656. } else {
  657. $container_guid = (int) $container_guid;
  658. }
  659. if ($site_guid == 0)
  660. $site_guid = $CONFIG->site_guid;
  661. //$access = get_access_list();
  662. $where = array();
  663. if ($entity_type!="")
  664. $where[] = "e.type='$entity_type'";
  665. if ($entity_subtype)
  666. $where[] = "e.subtype=$entity_subtype";
  667. $where[] = "m.name_id='$meta_start_n'";
  668. $where[] = "m2.name_id='$meta_end_n'";
  669. $where[] = "((v.string >= $meta_start_value AND v.string <= $meta_end_value) OR ( v2.string >= $meta_start_value AND v2.string <= $meta_end_value) OR (v.string <= $meta_start_value AND v2.string >= $meta_start_value) OR ( v2.string <= $meta_end_value AND v2.string >= $meta_end_value))";
  670. if ($region && $region != '-') {
  671. $where[] = "m3.name_id='$region_n'";
  672. $where[] = "m3.value_id='$region_value_n'";
  673. }
  674. if ($site_guid > 0)
  675. $where[] = "e.site_guid = {$site_guid}";
  676. if ($filter) {
  677. if (is_array($owner_guid)) {
  678. $where[] = "ms2.string in (".implode(",",$owner_guid).")";
  679. } else if ($owner_guid > 0) {
  680. $where[] = "ms2.string = {$owner_guid}";
  681. }
  682. $where[] = "ms.string = 'personal_event'";
  683. } else {
  684. if (is_array($owner_guid)) {
  685. $where[] = "e.owner_guid in (".implode(",",$owner_guid).")";
  686. } else if ($owner_guid > 0) {
  687. $where[] = "e.owner_guid = {$owner_guid}";
  688. }
  689. }
  690. if (is_array($container_guid)) {
  691. $where[] = "e.container_guid in (".implode(",",$container_guid).")";
  692. } else if ($container_guid > 0)
  693. $where[] = "e.container_guid = {$container_guid}";
  694. if (!$count) {
  695. $query = "SELECT distinct e.* ";
  696. } else {
  697. $query = "SELECT count(distinct e.guid) as total ";
  698. }
  699. $query .= "from {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}metadata m on e.guid = m.entity_guid JOIN {$CONFIG->dbprefix}metadata m2 on e.guid = m2.entity_guid ";
  700. if ($filter) {
  701. $query .= "JOIN {$CONFIG->dbprefix}annotations a ON (a.entity_guid = e.guid) ";
  702. $query .= "JOIN {$CONFIG->dbprefix}metastrings ms ON (a.name_id = ms.id) ";
  703. $query .= "JOIN {$CONFIG->dbprefix}metastrings ms2 ON (a.value_id = ms2.id) ";
  704. }
  705. if ($region && $region != '-') {
  706. $query .= "JOIN {$CONFIG->dbprefix}metadata m3 ON (e.guid = m3.entity_guid) ";
  707. }
  708. $query .= "JOIN {$CONFIG->dbprefix}metastrings v on v.id = m.value_id JOIN {$CONFIG->dbprefix}metastrings v2 on v2.id = m2.value_id where";
  709. foreach ($where as $w)
  710. $query .= " $w and ";
  711. $query .= get_access_sql_suffix("e"); // Add access controls
  712. $query .= ' and ' . get_access_sql_suffix("m"); // Add access controls
  713. $query .= ' and ' . get_access_sql_suffix("m2"); // Add access controls
  714. if (!$count) {
  715. $query .= " order by $order_by";
  716. if ($limit) {
  717. $query .= " limit $offset, $limit"; // Add order and limit
  718. }
  719. $entities = get_data($query, "entity_row_to_elggstar");
  720. if (elgg_get_plugin_setting('add_to_group_calendar', 'event_calendar') == 'yes') {
  721. if (get_entity($container_guid) instanceOf ElggGroup) {
  722. $entities = event_calendar_get_entities_from_metadata_between_related($meta_start_name, $meta_end_name,
  723. $meta_start_value, $meta_end_value, $entity_type,
  724. $entity_subtype, $owner_guid, $container_guid,
  725. 0, 0, "", 0,
  726. false, false, '-',$entities);
  727. }
  728. }
  729. return $entities;
  730. } else {
  731. if ($row = get_data_row($query))
  732. return $row->total;
  733. }
  734. return false;
  735. }
  736. // adds any related events (has the display_on_group relation)
  737. // that meet the appropriate criteria
  738. function event_calendar_get_entities_from_metadata_between_related($meta_start_name, $meta_end_name,
  739. $meta_start_value, $meta_end_value, $entity_type = "",
  740. $entity_subtype = "", $owner_guid = 0, $container_guid = 0,
  741. $limit = 10, $offset = 0, $order_by = "", $site_guid = 0,
  742. $filter = false, $count = false, $region='-',$main_events) {
  743. $main_list = array();
  744. if ($main_events) {
  745. foreach ($main_events as $event) {
  746. $main_list[$event->guid] = $event;
  747. }
  748. }
  749. $related_list = array();
  750. $related_events = elgg_get_entities_from_relationship(array(
  751. 'relationship' => 'display_on_group',
  752. 'relationship_guid' => $container_guid,
  753. 'inverse_relationship' => TRUE,
  754. ));
  755. if ($related_events) {
  756. foreach ($related_events as $event) {
  757. $related_list[$event->guid] = $event;
  758. }
  759. }
  760. // get all the events (across all containers) that meet the criteria
  761. $all_events = event_calendar_get_entities_from_metadata_between($meta_start_name, $meta_end_name,
  762. $meta_start_value, $meta_end_value, $entity_type, $entity_subtype, $owner_guid,
  763. 0, $limit, $offset, $order_by, $site_guid, $filter, $count, $region);
  764. if ($all_events) {
  765. foreach($all_events as $event) {
  766. if (array_key_exists($event->guid,$related_list)
  767. && !array_key_exists($event->guid,$main_list)) {
  768. // add to main events
  769. $main_events[] = $event;
  770. }
  771. }
  772. }
  773. return event_calendar_vsort($main_events,$meta_start_name);
  774. }
  775. // TODO: try to replace this with new Elgg 1.7 API
  776. /**
  777. * Return a list of entities based on the given search criteria.
  778. * In this case, returns entities with the given metadata between two values inclusive
  779. *
  780. * @param mixed $meta_start_name
  781. * @param mixed $meta_end_name
  782. * @param mixed $meta_start_value - start of metadata range, must be numerical value
  783. * @param mixed $meta_end_value - end of metadata range, must be numerical value
  784. * @param string $entity_type The type of entity to look for, eg 'site' or 'object'
  785. * @param string $entity_subtype The subtype of the entity.
  786. * @param mixed $owner_guid Either one integer user guid or an array of user guids
  787. * @param int $container_guid If supplied, the result is restricted to events associated with a specific container
  788. * @param int $limit
  789. * @param int $offset
  790. * @param string $order_by Optional ordering.
  791. * @param int $site_guid The site to get entities for. Leave as 0 (default) for the current site; -1 for all sites.
  792. * @param boolean $filter Filter by events in personal calendar if true
  793. * @param true|false $count If set to true, returns the total number of entities rather than a list. (Default: false)
  794. * @param string $meta_max metadata name containing maximum annotation count
  795. * @param string $annotation_name annotation name to count
  796. *
  797. * @return int|array A list of entities, or a count if $count is set to true
  798. *
  799. * TODO: see if the new API is robust enough to avoid this custom query
  800. */
  801. function event_calendar_get_entities_from_metadata_between2
  802. ($meta_start_name, $meta_end_name, $meta_start_value, $meta_end_value,
  803. $entity_type = "", $entity_subtype = "", $owner_guid = 0, $container_guid = 0,
  804. $limit = 10, $offset = 0, $order_by = "", $site_guid = 0, $filter = false,
  805. $count = false, $region='-', $meta_max = '', $annotation_name = '')
  806. {
  807. global $CONFIG;
  808. // This should not be possible, but a sanity check just in case
  809. if (!is_numeric($meta_start_value) || !is_numeric($meta_end_value)) {
  810. return FALSE;
  811. }
  812. $meta_start_n = get_metastring_id($meta_start_name);
  813. $meta_end_n = get_metastring_id($meta_end_name);
  814. if ($region && $region != '-') {
  815. $region_n = get_metastring_id('region');
  816. $region_value_n = get_metastring_id($region);
  817. if (!$region_n || !$region_value_n) {
  818. if ($count) {
  819. return 0;
  820. } else {
  821. return false;
  822. }
  823. }
  824. }
  825. $entity_type = sanitise_string($entity_type);
  826. $entity_subtype = get_subtype_id($entity_type, $entity_subtype);
  827. $limit = (int)$limit;
  828. $offset = (int)$offset;
  829. //if ($order_by == "") $order_by = "e.time_created desc";
  830. if ($order_by == "") $order_by = "v.string asc";
  831. $order_by = sanitise_string($order_by);
  832. $site_guid = (int) $site_guid;
  833. if ((is_array($owner_guid) && (count($owner_guid)))) {
  834. foreach($owner_guid as $key => $guid) {
  835. $owner_guid[$key] = (int) $guid;
  836. }
  837. } else {
  838. $owner_guid = (int) $owner_guid;
  839. }
  840. if ((is_array($container_guid) && (count($container_guid)))) {
  841. foreach($container_guid as $key => $guid) {
  842. $container_guid[$key] = (int) $guid;
  843. }
  844. } else {
  845. $container_guid = (int) $container_guid;
  846. }
  847. if ($site_guid == 0)
  848. $site_guid = $CONFIG->site_guid;
  849. //$access = get_access_list();
  850. $where = array();
  851. if ($entity_type!="")
  852. $where[] = "e.type='$entity_type'";
  853. if ($entity_subtype)
  854. $where[] = "e.subtype=$entity_subtype";
  855. $where[] = "m.name_id='$meta_start_n'";
  856. $where[] = "m2.name_id='$meta_end_n'";
  857. $where[] = "((v.string >= $meta_start_value AND v.string <= $meta_end_value) OR ( v2.string >= $meta_start_value AND v2.string <= $meta_end_value) OR (v.string <= $meta_start_value AND v2.string >= $meta_start_value) OR ( v2.string <= $meta_end_value AND v2.string >= $meta_end_value))";
  858. if ($region && $region != '-') {
  859. $where[] = "m3.name_id='$region_n'";
  860. $where[] = "m3.value_id='$region_value_n'";
  861. }
  862. if ($site_guid > 0)
  863. $where[] = "e.site_guid = {$site_guid}";
  864. if ($filter) {
  865. if (is_array($owner_guid)) {
  866. $where[] = "ms2.string in (".implode(",",$owner_guid).")";
  867. } else if ($owner_guid > 0) {
  868. $where[] = "ms2.string = {$owner_guid}";
  869. }
  870. $where[] = "ms.string = 'personal_event'";
  871. } else {
  872. if (is_array($owner_guid)) {
  873. $where[] = "e.owner_guid in (".implode(",",$owner_guid).")";
  874. } else if ($owner_guid > 0) {
  875. $where[] = "e.owner_guid = {$owner_guid}";
  876. }
  877. }
  878. if (is_array($container_guid)) {
  879. $where[] = "e.container_guid in (".implode(",",$container_guid).")";
  880. } else if ($container_guid > 0)
  881. $where[] = "e.container_guid = {$container_guid}";
  882. if (!$count) {
  883. $query = "SELECT distinct e.* ";
  884. } else {
  885. $query = "SELECT count(distinct e.guid) as total ";
  886. }
  887. $query .= "FROM {$CONFIG->dbprefix}entities e JOIN {$CONFIG->dbprefix}metadata m on e.guid = m.entity_guid JOIN {$CONFIG->dbprefix}metadata m2 on e.guid = m2.entity_guid ";
  888. if ($filter) {
  889. $query .= "JOIN {$CONFIG->dbprefix}annotations a ON (a.entity_guid = e.guid) ";
  890. $query .= "JOIN {$CONFIG->dbprefix}metastrings ms ON (a.name_id = ms.id) ";
  891. $query .= "JOIN {$CONFIG->dbprefix}metastrings ms2 ON (a.value_id = ms2.id) ";
  892. }
  893. if ($region && $region != '-') {
  894. $query .= "JOIN {$CONFIG->dbprefix}metadata m3 ON (e.guid = m3.entity_guid) ";
  895. }
  896. if ($meta_max && $annotation_name) {
  897. // This groups events for which the meta max name is defined
  898. // perhaps this should be a left join and accept null values?
  899. // so it would return groups with no spots defined as well
  900. $meta_max_n = get_metastring_id($meta_max);
  901. $ann_n = get_metastring_id($annotation_name);
  902. if (!$meta_max_n || !$ann_n) {
  903. if ($count) {
  904. return 0;
  905. } else {
  906. return false;
  907. }
  908. }
  909. $query .= " LEFT JOIN {$CONFIG->dbprefix}metadata m4 ON (e.guid = m4.entity_guid AND m4.name_id=$meta_max_n) ";
  910. $query .= " LEFT JOIN {$CONFIG->dbprefix}metastrings ms4 ON (m4.value_id = ms4.id) ";
  911. $where[] = "((ms4.string is null) OR (ms4.string = \"\") OR (CONVERT(ms4.string,SIGNED) > (SELECT count(id) FROM {$CONFIG->dbprefix}annotations ann WHERE ann.entity_guid = e.guid AND name_id = $ann_n GROUP BY entity_guid)))";
  912. }
  913. $query .= "JOIN {$CONFIG->dbprefix}metastrings v on v.id = m.value_id JOIN {$CONFIG->dbprefix}metastrings v2 on v2.id = m2.value_id where";
  914. foreach ($where as $w)
  915. $query .= " $w AND ";
  916. $query .= get_access_sql_suffix("e"); // Add access controls
  917. $query .= ' AND ' . get_access_sql_suffix("m"); // Add access controls
  918. $query .= ' AND ' . get_access_sql_suffix("m2"); // Add access controls
  919. if (!$count) {
  920. $query .= " order by $order_by";
  921. if ($limit) {
  922. $query .= " limit $offset, $limit"; // Add order and limit
  923. }
  924. $entities = get_data($query, "entity_row_to_elggstar");
  925. if (elgg_get_plugin_setting('add_to_group_calendar', 'event_calendar') == 'yes') {
  926. if (get_entity($container_guid) instanceOf ElggGroup) {
  927. $entities = event_calendar_get_entities_from_metadata_between_related($meta_start_name, $meta_end_name,
  928. $meta_start_value, $meta_end_value, $entity_type,
  929. $entity_subtype, $owner_guid, $container_guid,
  930. 0, 0, "", 0,
  931. false, false, '-',$entities);
  932. }
  933. }
  934. return $entities;
  935. } else {
  936. if ($row = get_data_row($query))
  937. return $row->total;
  938. }
  939. return false;
  940. }
  941. function event_calendar_has_personal_event($event_guid,$user_guid) {
  942. // check legacy implementation and new one
  943. if (check_entity_relationship($user_guid,'personal_event',$event_guid)) {
  944. return TRUE;
  945. } else {
  946. // use old method for now
  947. $options = array('guid'=>$event_guid,'annotation_name' => 'personal_event', 'annotation_value' => $user_guid,'count'=>TRUE);
  948. //$annotations = get_annotations($event_guid, "object", "event_calendar", "personal_event", (int) $user_guid, $user_guid);
  949. if (elgg_get_annotations($options)) {
  950. return TRUE;
  951. } else {
  952. return FALSE;
  953. }
  954. }
  955. }
  956. function event_calendar_add_personal_event($event_guid,$user_guid) {
  957. if ($event_guid && $user_guid) {
  958. if (!event_calendar_has_personal_event($event_guid,$user_guid)
  959. && !event_calendar_has_collision($event_guid,$user_guid)) {
  960. if (!event_calendar_is_full($event_guid)) {
  961. add_entity_relationship($user_guid,'personal_event',$event_guid);
  962. return TRUE;
  963. }
  964. }
  965. }
  966. return FALSE;
  967. }
  968. function event_calendar_add_personal_events_from_group($event_guid,$group_guid) {
  969. $members = get_group_members($group_guid, 100000);
  970. foreach($members as $member) {
  971. $member_id = $member->getGUID();
  972. event_calendar_add_personal_event($event_guid,$member_id);
  973. }
  974. }
  975. function event_calendar_remove_personal_event($event_guid,$user_guid) {
  976. remove_entity_relationship($user_guid,'personal_event',$event_guid);
  977. // also use old method for now
  978. $annotations = get_annotations($event_guid, "object", "event_calendar", "personal_event", (int) $user_guid, $user_guid);
  979. if ($annotations) {
  980. foreach ($annotations as $annotation) {
  981. $annotation->delete();
  982. }
  983. }
  984. }
  985. function event_calendar_get_personal_events_for_user($user_guid,$limit) {
  986. $events_old_way = elgg_get_entities_from_annotations(array(
  987. 'type' => 'object',
  988. 'subtype' => 'event_calendar',
  989. 'annotation_names' => 'personal_event',
  990. 'annotation_value' => $user_guid,
  991. 'limit' => 0,
  992. ));
  993. $events_new_way = elgg_get_entities_from_relationship(array(
  994. 'type' => 'object',
  995. 'subtype' => 'event_calendar',
  996. 'relationship' => 'personal_event',
  997. 'relationship_guid' => $user_guid,
  998. 'limit' => 0,
  999. ));
  1000. $events = array_merge($events_old_way,$events_new_way);
  1001. $final_events = array();
  1002. if ($events) {
  1003. $now = time();
  1004. $one_day = 60*60*24;
  1005. // don't show events that have been over for more than a day
  1006. foreach($events as $event) {
  1007. if (($event->start_date > $now-$one_day) || ($event->end_date && ($event->end_date > $now-$one_day))) {
  1008. $final_events[] = $event;
  1009. }
  1010. }
  1011. }
  1012. $sorted = event_calendar_vsort($final_events,'start_date');
  1013. return array_slice($sorted,0,$limit);
  1014. }
  1015. // the old way used annotations, and the new Elgg 1.8 way uses relationships
  1016. // for now this version attempts to bridge the gap by using both methods for older sites
  1017. function event_calendar_get_users_for_event($event_guid,$limit,$offset=0,$is_count=FALSE) {
  1018. $options = array(
  1019. 'type' => 'user',
  1020. 'relationship' => 'personal_event',
  1021. 'relationship_guid' => $event_guid,
  1022. 'inverse_relationship' => TRUE,
  1023. 'limit' => 0,
  1024. );
  1025. if ($is_count) {
  1026. //$count_old_way = count_annotations($event_guid, "object", "event_calendar", "personal_event");
  1027. $count_old_way = elgg_get_annotations(array(
  1028. 'guid'=>$event_guid,
  1029. 'type'=>"object",
  1030. 'subtype'=>"event_calendar",
  1031. 'annotation_name' => "personal_event",
  1032. 'count'=>TRUE)
  1033. );
  1034. $options ['count'] = TRUE;
  1035. $count_new_way = elgg_get_entities_from_relationship($options);
  1036. return $count_old_way + $count_new_way;
  1037. } else {
  1038. $users_old_way = array();
  1039. //$annotations = get_annotations($event_id, "object", "event_calendar", "personal_event", "", 0, $limit, $offset);
  1040. $annotations = elgg_get_annotations(array(
  1041. 'guid'=>$event_guid,
  1042. 'type'=>"object",
  1043. 'subtype'=>"event_calendar",
  1044. 'annotation_name' => "personal_event",
  1045. 'limit' => $limit,
  1046. 'offset' => $offset)
  1047. );
  1048. if ($annotations) {
  1049. foreach($annotations as $annotation) {
  1050. if (($user = get_entity($annotation->value)) && ($user instanceOf ElggUser)) {
  1051. $users_old_way[] = $user;
  1052. }
  1053. }
  1054. }
  1055. $users_new_way = elgg_get_entities_from_relationship($options);
  1056. return array_merge($users_old_way,$users_new_way);
  1057. }
  1058. }
  1059. function event_calendar_security_fields() {
  1060. $ts = time();
  1061. $token = generate_action_token($ts);
  1062. return "__elgg_token=$token&__elgg_ts=$ts";
  1063. }
  1064. function event_calendar_get_events_for_group($group_guid, $limit = 0) {
  1065. $options = array(
  1066. 'type' => 'object',
  1067. 'subtype' => 'event_calendar',
  1068. 'container_guid' => $group_guid,
  1069. 'limit' => $limit,
  1070. );
  1071. return elgg_get_entities($options);
  1072. }
  1073. function event_calendar_convert_time($time) {
  1074. $event_calendar_time_format = elgg_get_plugin_setting('timeformat','event_calendar');
  1075. if ($event_calendar_time_format == '12') {
  1076. $hour = floor($time/60);
  1077. $minute = sprintf("%02d",$time-60*$hour);
  1078. if ($hour < 12) {
  1079. return "$hour:$minute am";
  1080. } else {
  1081. $hour -= 12;
  1082. return "$hour:$minute pm";
  1083. }
  1084. } else {
  1085. $hour = floor($time/60);
  1086. $minute = sprintf("%02d",$time-60*$hour);
  1087. return "$hour:$minute";
  1088. }
  1089. }
  1090. function event_calendar_format_time($date,$time1,$time2='') {
  1091. if (is_numeric($time1)) {
  1092. $t = event_calendar_convert_time($time1);
  1093. if (is_numeric($time2)) {
  1094. $t .= " - ".event_calendar_convert_time($time2);
  1095. }
  1096. return "$t, $date";
  1097. } else {
  1098. return $date;
  1099. }
  1100. }
  1101. function event_calender_get_gmt_from_server_time($server_time) {
  1102. $gmtime = $server_time - (int)substr(date('O'),0,3)*60*60;
  1103. }
  1104. function event_calendar_activated_for_group($group) {
  1105. $group_calendar = elgg_get_plugin_setting('group_calendar', 'event_calendar');
  1106. $group_default = elgg_get_plugin_setting('group_default', 'event_calendar');
  1107. if ($group && ($group_calendar != 'no')) {
  1108. if ( ($group->event_calendar_enable == 'yes') || ((!$group->event_calendar_enable && (!$group_default || $group_default == 'yes')))) {
  1109. return true;
  1110. }
  1111. }
  1112. return false;
  1113. }
  1114. function event_calendar_get_region($event) {
  1115. $event_calendar_region_list_handles = elgg_get_plugin_setting('region_list_handles', 'event_calendar');
  1116. $region = trim($event->region);
  1117. if ($event_calendar_region_list_handles == 'yes') {
  1118. $region = elgg_echo('event_calendar:region:'.$region);
  1119. }
  1120. return htmlspecialchars($region);
  1121. }
  1122. function event_calendar_get_type($event) {
  1123. $event_calendar_type_list_handles = elgg_get_plugin_setting('type_list_handles', 'event_calendar');
  1124. $type = trim($event->event_type);
  1125. if ($type) {
  1126. if ($event_calendar_type_list_handles == 'yes') {
  1127. $type = elgg_echo('event_calendar:type:'.$type);
  1128. }
  1129. return htmlspecialchars($type);
  1130. } else {
  1131. return $type;
  1132. }
  1133. }
  1134. function event_calendar_get_formatted_full_items($event) {
  1135. $time_bit = event_calendar_get_formatted_time($event);
  1136. $event_calendar_region_display = elgg_get_plugin_setting('region_display', 'event_calendar');
  1137. $event_calendar_type_display = elgg_get_plugin_setting('type_display', 'event_calendar');
  1138. $event_items = array();
  1139. if ($time_bit) {
  1140. $item = new stdClass();
  1141. $item->title = elgg_echo('event_calendar:when_label');
  1142. $item->value = $time_bit;
  1143. $event_items[] = $item;
  1144. }
  1145. $item = new stdClass();
  1146. $item->title = elgg_echo('event_calendar:venue_label');
  1147. $item->value = htmlspecialchars($event->venue);
  1148. $event_items[] = $item;
  1149. if ($event_calendar_region_display == 'yes') {
  1150. $item = new stdClass();
  1151. $item->title = elgg_echo('event_calendar:region_label');
  1152. $item->value = event_calendar_get_region($event);
  1153. $event_items[] = $item;
  1154. }
  1155. if ($event_calendar_type_display == 'yes') {
  1156. $event_type = event_calendar_get_type($event);
  1157. if ($event_type) {
  1158. $item = new stdClass();
  1159. $item->title = elgg_echo('event_calendar:type_label');
  1160. $item->value = event_calendar_get_type($event);
  1161. $event_items[] = $item;
  1162. }
  1163. }
  1164. $item = new stdClass();
  1165. $item->title = elgg_echo('event_calendar:fees_label');
  1166. $item->value = htmlspecialchars($event->fees);
  1167. $event_items[] = $item;
  1168. $item = new stdClass();
  1169. $item->title = elgg_echo('event_calendar:organiser_label');
  1170. $item->value = htmlspecialchars($event->organiser);
  1171. $event_items[] = $item;
  1172. $item = new stdClass();
  1173. $item->title = elgg_echo('event_calendar:contact_label');
  1174. $item->value = htmlspecialchars($event->contact);
  1175. $event_items[] = $item;
  1176. return $event_items;
  1177. }
  1178. function event_calendar_get_formatted_time($event) {
  1179. if (!$event->start_date) {
  1180. return '';
  1181. }
  1182. $date_format = 'j M Y';
  1183. $event_calendar_times = elgg_get_plugin_setting('times', 'event_calendar') != 'no';
  1184. $start_date = date($date_format,$event->start_date);
  1185. if ($event->end_date) {
  1186. $end_date = date($date_format,$event->end_date);
  1187. }
  1188. if ((!$event->end_date) || ($end_date == $start_date)) {
  1189. if (!$event->all_day && $event_calendar_times) {
  1190. $start_date = event_calendar_format_time($start_date,$event->start_time,$event->end_time);
  1191. }
  1192. $time_bit = $start_date;
  1193. } else {
  1194. if (!$event->all_day && $event_calendar_times) {
  1195. $start_date = event_calendar_format_time($start_date,$event->start_time);
  1196. $end_date = event_calendar_format_time($end_date,$event->end_time);
  1197. }
  1198. $time_bit = "$start_date - $end_date";
  1199. }
  1200. if ($event->repeats == 'yes') {
  1201. $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
  1202. $r = array();
  1203. foreach ($dow as $w) {
  1204. $fn = 'event-calendar-repeating-'.$w.'-value';
  1205. if ($event->$fn) {
  1206. $r[] = elgg_echo('event_calendar:dow:full:'.$w);
  1207. }
  1208. }
  1209. $week_bit = implode(", ",$r);
  1210. if ($event->repeat_interval > 1) {
  1211. $week_bit .= ' '.elgg_echo('event_calendar:repeated_event:week_interval',array($event->repeat_interval));
  1212. } else {
  1213. $week_bit .= ' '.elgg_echo('event_calendar:repeated_event:week_single');
  1214. }
  1215. $time_bit = elgg_echo('event_calendar:repeated_event:format',array($time_bit, $week_bit));
  1216. }
  1217. return $time_bit;
  1218. }
  1219. function event_calendar_get_formatted_date($ts) {
  1220. // TODO: make the date format configurable
  1221. return date('j/n/Y',$ts);
  1222. }
  1223. function event_calendar_is_full($event_id) {
  1224. $event_calendar_spots_display = elgg_get_plugin_setting('spots_display', 'event_calendar');
  1225. if ($event_calendar_spots_display == 'yes') {
  1226. $count = event_calendar_get_users_for_event($event_id,0,0,TRUE);
  1227. $event = get_entity($event_id);
  1228. if ($event) {
  1229. $spots = $event->spots;
  1230. if (is_numeric($spots)) {
  1231. if ($count >= $spots) {
  1232. return TRUE;
  1233. }
  1234. }
  1235. }
  1236. }
  1237. return FALSE;
  1238. }
  1239. function event_calendar_has_collision($event_id, $user_id) {
  1240. $no_collisions = elgg_get_plugin_setting('no_collisions', 'event_calendar');
  1241. if ($no_collisions == 'yes') {
  1242. $event = get_entity($event_id);
  1243. if ($event) {
  1244. $start_time = $event->start_date;
  1245. $end_time = event_calendar_get_end_time($event);
  1246. // look to see if the user already has events within this period
  1247. $count = event_calendar_get_events_for_user_between2($start_time,$end_time,TRUE,10,0,$user_id);
  1248. if ($count > 0) {
  1249. return TRUE;
  1250. } else {
  1251. return FALSE;
  1252. }
  1253. }
  1254. }
  1255. return FALSE;
  1256. }
  1257. // this complicated bit of code determines the event end time
  1258. function event_calendar_get_end_time($event) {
  1259. $default_length = elgg_get_plugin_setting('collision_length', 'event_calendar');
  1260. $start_time = $event->start_date;
  1261. $end_time = $event->end_time;
  1262. $end_date = $event->end_date;
  1263. if($end_date) {
  1264. if ($end_time) {
  1265. $end_time = $end_date+$end_time*60;
  1266. } else if ($start_time == $end_date) {
  1267. if (is_numeric($default_length)) {
  1268. $end_time = $end_date + $default_length;
  1269. } else {
  1270. // default to an hour length
  1271. $end_time = $start_time + 3600;
  1272. }
  1273. } else {
  1274. $end_time = $end_date;
  1275. }
  1276. } else {
  1277. if ($end_time) {
  1278. if ($event->start_time) {
  1279. $end_time = $start_time + ($end_time*60 - $event->start_time*60);
  1280. } else {
  1281. $end_time = $start_time + $end_time*60;
  1282. }
  1283. } else {
  1284. if (is_numeric($default_length)) {
  1285. $end_time = $start_time + $default_length;
  1286. } else {
  1287. // default to an hour length
  1288. $end_time = $start_time + 3600;
  1289. }
  1290. }
  1291. }
  1292. return $end_time;
  1293. }
  1294. // a version to allow for some customised options
  1295. function event_calendar_view_entity_list($entities, $count, $offset, $limit, $fullview = true, $viewtypetoggle = true, $pagination = true) {
  1296. $count = (int) $count;
  1297. $limit = (int) $limit;
  1298. // do not require views to explicitly pass in the offset
  1299. if (!$offset = (int) $offset) {
  1300. $offset = sanitise_int(get_input('offset', 0));
  1301. }
  1302. $context = elgg_get_context();
  1303. $html = elgg_view('event_calendar/entities/entity_list',array(
  1304. 'entities' => $entities,
  1305. 'count' => $count,
  1306. 'offset' => $offset,
  1307. 'limit' => $limit,
  1308. 'baseurl' => $_SERVER['REQUEST_URI'],
  1309. 'fullview' => $fullview,
  1310. 'context' => $context,
  1311. 'viewtypetoggle' => $viewtypetoggle,
  1312. 'viewtype' => get_input('search_viewtype','list'),
  1313. 'pagination' => $pagination
  1314. ));
  1315. return $html;
  1316. }
  1317. // returns open, closed or private for the given event and user
  1318. function event_calendar_personal_can_manage($event,$user_id) {
  1319. $status = 'private';
  1320. $event_calendar_personal_manage = elgg_get_plugin_setting('personal_manage', 'event_calendar');
  1321. if (!$event_calendar_personal_manage
  1322. || $event_calendar_personal_manage == 'open'
  1323. || $event_calendar_personal_manage == 'yes'
  1324. || (($event_calendar_personal_manage == 'by_event' && (!$event->personal_manage || ($event->personal_manage == 'open'))))) {
  1325. $status = 'open';
  1326. } else {
  1327. // in this case only admins or event owners can manage events on their personal calendars
  1328. if(elgg_is_admin_logged_in()) {
  1329. $status = 'open';
  1330. } else if ($event && ($event->owner_guid == $user_id)) {
  1331. $status = 'open';
  1332. } else if (($event_calendar_personal_manage == 'closed')
  1333. || ($event_calendar_personal_manage == 'no')
  1334. || (($event_calendar_personal_manage == 'by_event') && ($event->personal_manage == 'closed'))) {
  1335. $status = 'closed';
  1336. }
  1337. }
  1338. return $status;
  1339. }
  1340. function event_calendar_send_event_request($event,$user_guid) {
  1341. $result = FALSE;
  1342. if(add_entity_relationship($user_guid, 'event_calendar_request', $event->guid)) {
  1343. $subject = elgg_echo('event_calendar:request_subject');
  1344. $name = get_entity($user_guid)->name;
  1345. $title = $event->title;
  1346. $url = $event->getUrl();
  1347. $link = elgg_get_site_url().'event_calendar/review_requests/'.$event->guid;
  1348. $message = sprintf(elgg_echo('event_calendar:request_message'),$name,$title,$url,$link);
  1349. notify_user($event->owner_guid,elgg_get_site_entity()->guid,$subject,$message);
  1350. $result = TRUE;
  1351. }
  1352. return $result;
  1353. }
  1354. // pages
  1355. function event_calendar_get_page_content_list($page_type,$container_guid,$start_date,$display_mode,$filter,$region='-') {
  1356. elgg_load_js('elgg.event_calendar');
  1357. global $autofeed;
  1358. $autofeed = true;
  1359. if ($page_type == 'group') {
  1360. if (!event_calendar_activated_for_group($container_guid)) {
  1361. forward();
  1362. }
  1363. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'));
  1364. elgg_push_context('groups');
  1365. elgg_set_page_owner_guid($container_guid);
  1366. $user_guid = elgg_get_logged_in_user_guid();
  1367. if(event_calendar_can_add($container_guid)) {
  1368. elgg_register_menu_item('title', array(
  1369. 'name' => 'add',
  1370. 'href' => "event_calendar/add/".$container_guid,
  1371. 'text' => elgg_echo('event_calendar:add'),
  1372. 'class' => 'elgg-button elgg-button-action event-calendar-button-add',
  1373. ));
  1374. }
  1375. } else {
  1376. elgg_push_breadcrumb(elgg_echo('item:object:event_calendar'));
  1377. $user_guid = elgg_get_logged_in_user_guid();
  1378. if(event_calendar_can_add($container_guid)) {
  1379. elgg_register_menu_item('title', array(
  1380. 'name' => 'add',
  1381. 'href' => "event_calendar/add",
  1382. 'text' => elgg_echo('event_calendar:add'),
  1383. 'class' => 'elgg-button elgg-button-action event-calendar-button-add',
  1384. ));
  1385. }
  1386. }
  1387. $params = event_calendar_generate_listing_params($page_type,$container_guid,$start_date,$display_mode,$filter,$region);
  1388. $url = current_page_url();
  1389. if (substr_count($url, '?')) {
  1390. $url .= "&view=ical";
  1391. } else {
  1392. $url .= "?view=ical";
  1393. }
  1394. $url = elgg_format_url($url);
  1395. $menu_options = array(
  1396. 'name' => 'ical',
  1397. 'id' => 'event-calendar-ical-link',
  1398. 'text' => elgg_view_icon('calendar'),
  1399. 'href' => $url,
  1400. 'title' => elgg_echo('feed:ical'),
  1401. 'priority' => 800,
  1402. 'rel' => 'popup',
  1403. );
  1404. $menu_item = ElggMenuItem::factory($menu_options);
  1405. elgg_register_menu_item('extras', $menu_item);
  1406. $body = elgg_view_layout("content", $params);
  1407. $body .= elgg_view_module('popup', elgg_echo('feed:ical'), elgg_echo('event_calendar:ical_popup_message') . elgg_view('output/url', array(
  1408. 'href' => $url,
  1409. 'text' => elgg_echo('export'),
  1410. 'class' => 'elgg-button elgg-button-action',
  1411. )), array(
  1412. 'id' => 'event-calendar-ical',
  1413. 'class' => 'event-calendar-ical hidden',
  1414. ));
  1415. return elgg_view_page($title, $body);
  1416. }
  1417. function event_calendar_get_page_content_edit($page_type,$guid,$start_date='') {
  1418. elgg_load_js('elgg.event_calendar');
  1419. $vars = array();
  1420. $vars['id'] = 'event-calendar-edit';
  1421. // just in case a feature adds an image upload
  1422. $vars['enctype'] = 'multipart/form-data';
  1423. $body_vars = array();
  1424. if ($page_type == 'edit') {
  1425. $title = elgg_echo('event_calendar:manage_event_title');
  1426. $event = get_entity((int)$guid);
  1427. if (elgg_instanceof($event, 'object', 'event_calendar') && $event->canEdit()) {
  1428. $body_vars['event'] = $event;
  1429. $body_vars['form_data'] = event_calendar_prepare_edit_form_vars($event,$page_type);
  1430. $event_container = get_entity($event->container_guid);
  1431. if (elgg_instanceof($event_container, 'group')) {
  1432. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
  1433. $body_vars['group_guid'] = $event_container->guid;
  1434. } else {
  1435. elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
  1436. $body_vars['group_guid'] = 0;
  1437. }
  1438. elgg_push_breadcrumb($event->title,$event->getURL());
  1439. elgg_push_breadcrumb(elgg_echo('event_calendar:manage_event_title'));
  1440. $content = elgg_view_form('event_calendar/edit', $vars,$body_vars);
  1441. } else {
  1442. $content = elgg_echo('event_calendar:error_event_edit');
  1443. }
  1444. } else {
  1445. $title = elgg_echo('event_calendar:add_event_title');
  1446. if ($guid) {
  1447. // add to group
  1448. $group = get_entity($guid);
  1449. if (elgg_instanceof($group, 'group')) {
  1450. $body_vars['group_guid'] = $guid;
  1451. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$guid);
  1452. elgg_push_breadcrumb(elgg_echo('event_calendar:add_event_title'));
  1453. $body_vars['form_data'] = event_calendar_prepare_edit_form_vars(NULL,$page_type,$start_date);
  1454. $content = elgg_view_form('event_calendar/edit', $vars, $body_vars);
  1455. } else {
  1456. $content = elgg_echo('event_calendar:no_group');
  1457. }
  1458. } else {
  1459. $body_vars['group_guid'] = 0;
  1460. elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
  1461. elgg_push_breadcrumb(elgg_echo('event_calendar:add_event_title'));
  1462. $body_vars['form_data'] = event_calendar_prepare_edit_form_vars(NULL,$page_type,$start_date);
  1463. $content = elgg_view_form('event_calendar/edit', $vars, $body_vars);
  1464. }
  1465. }
  1466. $params = array('title' => $title, 'content' => $content,'filter' => '');
  1467. $body = elgg_view_layout("content", $params);
  1468. return elgg_view_page($title,$body);
  1469. }
  1470. /**
  1471. * Pull together variables for the edit form
  1472. *
  1473. * @param ElggObject $event
  1474. * @return array
  1475. */
  1476. function event_calendar_prepare_edit_form_vars($event = NULL, $page_type = '', $start_date = '') {
  1477. // input names => defaults
  1478. $now = time();
  1479. $iso_date = date('Y-m-d',$now);
  1480. $now_midnight = strtotime($iso_date);
  1481. if ($start_date) {
  1482. $start_date = strtotime($start_date);
  1483. } else {
  1484. $start_date = $now+60*60;
  1485. }
  1486. $start_time = floor(($now-$now_midnight)/60) + 60;
  1487. $start_time = floor($start_time/5)*5;
  1488. $values = array(
  1489. 'title' => NULL,
  1490. 'description' => NULL,
  1491. 'venue' => NULL,
  1492. 'start_date' => $start_date,
  1493. 'end_date' => $start_date+60*60,
  1494. 'start_time' => $start_time,
  1495. 'end_time' => $start_time + 60,
  1496. 'spots' => NULL,
  1497. 'region' => '-',
  1498. 'event_type' => '-',
  1499. 'fees' => NULL,
  1500. 'contact' => NULL,
  1501. 'organiser' => NULL,
  1502. 'tags' => NULL,
  1503. 'send_reminder' => NULL,
  1504. 'reminder_number' => 1,
  1505. 'reminder_interval' => 60,
  1506. 'repeats' => NULL,
  1507. 'repeat_interval' => 1,
  1508. 'event-calendar-repeating-monday-value' => 0,
  1509. 'event-calendar-repeating-tuesday-value' => 0,
  1510. 'event-calendar-repeating-wednesday-value' => 0,
  1511. 'event-calendar-repeating-thursday-value' => 0,
  1512. 'event-calendar-repeating-friday-value' => 0,
  1513. 'event-calendar-repeating-saturday-value' => 0,
  1514. 'event-calendar-repeating-sunday-value' => 0,
  1515. 'personal_manage' => 'open',
  1516. 'web_conference' => NULL,
  1517. 'long_description' => NULL,
  1518. 'access_id' => ACCESS_DEFAULT,
  1519. 'group_guid' => NULL,
  1520. );
  1521. if ($page_type == 'schedule') {
  1522. $values['schedule_type'] = 'poll';
  1523. } else {
  1524. $values['schedule_type'] = 'all_day';
  1525. }
  1526. if ($event) {
  1527. foreach (array_keys($values) as $field) {
  1528. if (isset($event->$field)) {
  1529. $values[$field] = $event->$field;
  1530. }
  1531. }
  1532. }
  1533. if (elgg_is_sticky_form('event_calendar')) {
  1534. $sticky_values = elgg_get_sticky_values('event_calendar');
  1535. foreach ($sticky_values as $key => $value) {
  1536. $values[$key] = $value;
  1537. }
  1538. }
  1539. elgg_clear_sticky_form('event_calendar');
  1540. return $values;
  1541. }
  1542. function event_calendar_generate_listing_params($page_type,$container_guid,$original_start_date,$display_mode,$filter,$region='-') {
  1543. $event_calendar_listing_format = elgg_get_plugin_setting('listing_format', 'event_calendar');
  1544. $event_calendar_spots_display = trim(elgg_get_plugin_setting('spots_display', 'event_calendar'));
  1545. $event_calendar_first_date = trim(elgg_get_plugin_setting('first_date', 'event_calendar'));
  1546. $event_calendar_last_date = trim(elgg_get_plugin_setting('last_date', 'event_calendar'));
  1547. if (!$original_start_date) {
  1548. $original_start_date = date('Y-m-d');
  1549. }
  1550. if ( $event_calendar_first_date && ($original_start_date < $event_calendar_first_date) ) {
  1551. $original_start_date = $event_calendar_first_date;
  1552. }
  1553. if ( $event_calendar_last_date && ($original_start_date > $event_calendar_last_date) ) {
  1554. $original_start_date = $event_calendar_first_date;
  1555. }
  1556. if ($event_calendar_listing_format == 'paged') {
  1557. $start_ts = strtotime($original_start_date);
  1558. $start_date = $original_start_date;
  1559. if ($event_calendar_last_date) {
  1560. $end_ts = strtotime($event_calendar_last_date);
  1561. } else {
  1562. // set to a large number
  1563. $end_ts = 2000000000;
  1564. }
  1565. $mode = 'paged';
  1566. } else {
  1567. // the default interval is one month
  1568. $day = 60*60*24;
  1569. $week = 7*$day;
  1570. $month = 31*$day;
  1571. $mode = trim($display_mode);
  1572. if (!$mode) {
  1573. $mode = 'month';
  1574. }
  1575. if ($mode == "day") {
  1576. $start_date = $original_start_date;
  1577. $end_date = $start_date;
  1578. $start_ts = strtotime($start_date);
  1579. $end_ts = strtotime($end_date)+$day-1;
  1580. } else if ($mode == "week") {
  1581. // need to adjust start_date to be the beginning of the week
  1582. $start_ts = strtotime($original_start_date);
  1583. $start_ts -= date("w",$start_ts)*$day;
  1584. $end_ts = $start_ts + 6*$day;
  1585. $start_date = date('Y-m-d',$start_ts);
  1586. $end_date = date('Y-m-d',$end_ts);
  1587. } else {
  1588. $start_ts = strtotime($original_start_date);
  1589. $month = date('m',$start_ts);
  1590. $year = date('Y',$start_ts);
  1591. $start_date = $year.'-'.$month.'-1';
  1592. $end_date = $year.'-'.$month.'-'.getLastDayOfMonth($month,$year);
  1593. }
  1594. if ($event_calendar_first_date && ($start_date < $event_calendar_first_date)) {
  1595. $start_date = $event_calendar_first_date;
  1596. }
  1597. if ($event_calendar_last_date && ($end_date > $event_calendar_last_date)) {
  1598. $end_date = $event_calendar_last_date;
  1599. }
  1600. $start_ts = strtotime($start_date);
  1601. if ($mode == "day") {
  1602. $end_ts = strtotime($end_date)+$day-1;
  1603. $subtitle = elgg_echo('event_calendar:day_label').': '.date('j F Y',strtotime($start_date));
  1604. } else if ($mode == "week") {
  1605. // KJ - fix for end date bug
  1606. //$end_ts = $start_ts + 6*$day;
  1607. $end_ts = $start_ts + 7*$day;
  1608. $subtitle = elgg_echo('event_calendar:week_label').': '.date('j F',$start_ts) . ' - '.date('j F Y',$end_ts);
  1609. } else {
  1610. // KJ - fix for end date bug
  1611. //$end_ts = strtotime($end_date);
  1612. $end_ts = strtotime($end_date)+24*60*60-1;
  1613. $subtitle = date('F Y',$start_ts);
  1614. }
  1615. }
  1616. $current_user_guid = elgg_get_logged_in_user_guid();
  1617. $access_status = elgg_get_ignore_access();
  1618. if ($page_type == 'owner') {
  1619. $container = get_entity($container_guid);
  1620. if (elgg_instanceof($container, 'user')) {
  1621. $auth_token = get_input('auth_token');
  1622. if ($auth_token) {
  1623. $secret_key = event_calendar_get_secret_key();
  1624. if ($secret_key && ($auth_token === sha1($container->username . $secret_key))) {
  1625. elgg_set_ignore_access(TRUE);
  1626. }
  1627. }
  1628. if ($container->canEdit()) {
  1629. $user_guid = $container_guid;
  1630. $group_guid = 0;
  1631. } else {
  1632. register_error('event_calendar:owner:permissions_error');
  1633. forward();
  1634. exit;
  1635. }
  1636. } else {
  1637. register_error('event_calendar:owner:permissions_error');
  1638. forward();
  1639. exit;
  1640. }
  1641. } else {
  1642. $user_guid = $current_user_guid;
  1643. $group_guid = $container_guid;
  1644. }
  1645. $offset = get_input('offset');
  1646. $limit = get_input('limit',15);
  1647. if ($event_calendar_spots_display == 'yes') {
  1648. if (!$filter) {
  1649. $filter = 'open';
  1650. }
  1651. } else {
  1652. if (!$filter) {
  1653. $filter = 'all';
  1654. }
  1655. }
  1656. if ($filter == 'all') {
  1657. $count = event_calendar_get_events_between($start_ts,$end_ts,true,$limit,$offset,$container_guid,$region);
  1658. $events = event_calendar_get_events_between($start_ts,$end_ts,false,$limit,$offset,$container_guid,$region);
  1659. } else if ($filter == 'open') {
  1660. $count = event_calendar_get_open_events_between($start_ts,$end_ts,true,$limit,$offset,$container_guid,$region);
  1661. $events = event_calendar_get_open_events_between($start_ts,$end_ts,false,$limit,$offset,$container_guid,$region);
  1662. } else if ($filter == 'friends') {
  1663. $count = event_calendar_get_events_for_friends_between($start_ts,$end_ts,true,$limit,$offset,$user_guid,$container_guid,$region);
  1664. $events = event_calendar_get_events_for_friends_between($start_ts,$end_ts,false,$limit,$offset,$user_guid,$container_guid,$region);
  1665. } else if ($filter == 'mine') {
  1666. $count = event_calendar_get_events_for_user_between2($start_ts,$end_ts,true,$limit,$offset,$user_guid,$container_guid,$region);
  1667. $events = event_calendar_get_events_for_user_between2($start_ts,$end_ts,false,$limit,$offset,$user_guid,$container_guid,$region);
  1668. }
  1669. $vars = array(
  1670. 'original_start_date' => $original_start_date,
  1671. 'start_date' => $start_date,
  1672. 'end_date' => $end_date,
  1673. 'first_date' => $event_calendar_first_date,
  1674. 'last_date' => $event_calendar_last_date,
  1675. 'mode' => $mode,
  1676. 'events' => $events,
  1677. 'count' => $count,
  1678. 'offset' => $offset,
  1679. 'limit' => $limit,
  1680. 'group_guid' => $group_guid,
  1681. 'filter' => $filter,
  1682. 'region' => $region,
  1683. 'listing_format' => $event_calendar_listing_format,
  1684. );
  1685. $content = elgg_view('event_calendar/show_events', $vars);
  1686. if ($page_type == 'group') {
  1687. $filter_override = '';
  1688. } else {
  1689. $filter_override = elgg_view('event_calendar/filter_menu',$vars);
  1690. }
  1691. if ($event_calendar_listing_format == 'paged') {
  1692. $title = elgg_echo('event_calendar:upcoming_events_title');
  1693. } else if ($event_calendar_listing_format == 'full') {
  1694. $title = elgg_echo('event_calendar:show_events_title');
  1695. } else if ($page_type == 'group') {
  1696. $title = elgg_echo('event_calendar:group'). ' ('.$subtitle.')';
  1697. } else {
  1698. $title = elgg_echo('event_calendar:listing_title:'.$filter). ' ('.$subtitle.')';
  1699. }
  1700. $params = array('title' => $title, 'content' => $content, 'filter_override'=>$filter_override);
  1701. elgg_set_ignore_access($access_status);
  1702. return $params;
  1703. }
  1704. function event_calendar_get_page_content_view($event_guid,$light_box = FALSE) {
  1705. // add personal calendar button and links
  1706. elgg_push_context('event_calendar:view');
  1707. $event = get_entity($event_guid);
  1708. if (!elgg_instanceof($event, 'object', 'event_calendar')) {
  1709. $content = elgg_echo('event_calendar:error_nosuchevent');
  1710. $title = elgg_echo('event_calendar:generic_error_title');
  1711. } else {
  1712. $title = htmlspecialchars($event->title);
  1713. $event_container = get_entity($event->container_guid);
  1714. if (elgg_instanceof($event_container, 'group')) {
  1715. if ($event_container->canEdit()) {
  1716. event_calendar_handle_menu($event_guid);
  1717. }
  1718. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
  1719. } else {
  1720. if ($event->canEdit()) {
  1721. event_calendar_handle_menu($event_guid);
  1722. }
  1723. elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
  1724. }
  1725. elgg_push_breadcrumb($event->title);
  1726. $content = elgg_view_entity($event, array('full_view' => true,'light_box'=>$light_box));
  1727. //check to see if comment are on - TODO - add this feature to all events
  1728. if ($event->comments_on != 'Off') {
  1729. $content .= elgg_view_comments($event);
  1730. }
  1731. }
  1732. if ($light_box) {
  1733. return '<div class="event-calendar-lightbox">'.elgg_view_title($title).$content.'</div>';
  1734. } else {
  1735. $params = array('title' => $title, 'content' => $content,'filter' => '');
  1736. $body = elgg_view_layout("content", $params);
  1737. return elgg_view_page($title,$body);
  1738. }
  1739. }
  1740. function event_calendar_get_page_content_display_users($event_guid) {
  1741. elgg_load_js('elgg.event_calendar');
  1742. $event = get_entity($event_guid);
  1743. if (!elgg_instanceof($event, 'object', 'event_calendar')) {
  1744. $content = elgg_echo('event_calendar:error_nosuchevent');
  1745. $title = elgg_echo('event_calendar:generic_error_title');
  1746. } else {
  1747. event_calendar_handle_menu($event_guid);
  1748. $title = elgg_echo('event_calendar:users_for_event_title',array(htmlspecialchars($event->title)));
  1749. $event_container = get_entity($event->container_guid);
  1750. if (elgg_instanceof($event_container, 'group')) {
  1751. elgg_push_context('groups');
  1752. elgg_set_page_owner_guid($event->container_guid);
  1753. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
  1754. } else {
  1755. elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
  1756. }
  1757. elgg_push_breadcrumb($event->title,$event->getURL());
  1758. elgg_push_breadcrumb(elgg_echo('event_calendar:users_for_event_breadcrumb'));
  1759. $limit = 12;
  1760. $offset = get_input('offset', 0);
  1761. $users = event_calendar_get_users_for_event($event_guid,$limit,$offset,false);
  1762. $options = array(
  1763. 'full_view' => FALSE,
  1764. 'list_type_toggle' => FALSE,
  1765. 'limit'=>$limit,
  1766. 'event_calendar_event'=>$event,
  1767. );
  1768. elgg_extend_view('user/default','event_calendar/calendar_toggle');
  1769. $content = elgg_view_entity_list($users,$options);
  1770. }
  1771. $params = array('title' => $title, 'content' => $content,'filter' => '');
  1772. $body = elgg_view_layout("content", $params);
  1773. return elgg_view_page($title,$body);
  1774. }
  1775. // display a list of all the members of the container of $event_guid and allowing
  1776. // adding or removing them
  1777. function event_calendar_get_page_content_manage_users($event_guid) {
  1778. // TODO: make this an optional feature, toggled off
  1779. elgg_load_js('elgg.event_calendar');
  1780. $event = get_entity($event_guid);
  1781. $limit = 10;
  1782. $offset = get_input('offset', 0);
  1783. $event_calendar_add_users = elgg_get_plugin_setting('add_users', 'event_calendar');
  1784. if ($event_calendar_add_users != 'yes') {
  1785. register_error(elgg_echo('event_calendar:feature_not_activated'));
  1786. forward();
  1787. exit;
  1788. }
  1789. if (!elgg_instanceof($event, 'object', 'event_calendar')) {
  1790. $content = elgg_echo('event_calendar:error_nosuchevent');
  1791. $title = elgg_echo('event_calendar:generic_error_title');
  1792. } else {
  1793. event_calendar_handle_menu($event_guid);
  1794. $title = elgg_echo('event_calendar:manage_users:title',array($event->title));
  1795. $event_container = get_entity($event->container_guid);
  1796. if ($event_container->canEdit()) {
  1797. if (elgg_instanceof($event_container, 'group')) {
  1798. elgg_push_context('groups');
  1799. elgg_set_page_owner_guid($event->container_guid);
  1800. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
  1801. elgg_register_menu_item('title', array(
  1802. 'name' => 'remove_from_group_members',
  1803. 'href' => elgg_add_action_tokens_to_url('action/event_calendar/remove_from_group_members?event_guid='.$event_guid),
  1804. 'text' => elgg_echo('event_calendar:remove_from_group_members:button'),
  1805. 'class' => 'elgg-button elgg-button-action',
  1806. ));
  1807. elgg_register_menu_item('title', array(
  1808. 'name' => 'add_to_group_members',
  1809. 'href' => elgg_add_action_tokens_to_url('action/event_calendar/add_to_group_members?event_guid='.$event_guid),
  1810. 'text' => elgg_echo('event_calendar:add_to_group_members:button'),
  1811. 'class' => 'elgg-button elgg-button-action',
  1812. ));
  1813. $users = $event_container->getMembers($limit,$offset);
  1814. $count = $event_container->getMembers($limit,$offset,TRUE);
  1815. elgg_extend_view('user/default','event_calendar/calendar_toggle');
  1816. $options = array(
  1817. 'full_view' => FALSE,
  1818. 'list_type_toggle' => FALSE,
  1819. 'limit'=>$limit,
  1820. 'event_calendar_event'=>$event,
  1821. 'pagination' => TRUE,
  1822. 'count'=>$count,
  1823. );
  1824. $content .= elgg_view_entity_list($users,$options,$offset,$limit);
  1825. } else {
  1826. elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
  1827. $content = '<p>'.elgg_echo('event_calendar:manage_users:description').'</p>';
  1828. $content .= elgg_view_form('event_calendar/manage_subscribers',array(),array('event'=>$event));
  1829. }
  1830. elgg_push_breadcrumb($event->title,$event->getURL());
  1831. elgg_push_breadcrumb(elgg_echo('event_calendar:manage_users:breadcrumb'));
  1832. } else {
  1833. $content = elgg_echo('event_calendar:manage_users:unauthorized');
  1834. }
  1835. }
  1836. $params = array('title' => $title, 'content' => $content,'filter' => '');
  1837. $body = elgg_view_layout("content", $params);
  1838. return elgg_view_page($title,$body);
  1839. }
  1840. function event_calendar_get_page_content_review_requests($event_guid) {
  1841. $event = get_entity($event_guid);
  1842. if (!elgg_instanceof($event, 'object', 'event_calendar')) {
  1843. $content = elgg_echo('event_calendar:error_nosuchevent');
  1844. $title = elgg_echo('event_calendar:generic_error_title');
  1845. } else {
  1846. event_calendar_handle_menu($event_guid);
  1847. $title = elgg_echo('event_calendar:review_requests_title',array(htmlspecialchars($event->title)));
  1848. $event_container = get_entity($event->container_guid);
  1849. if (elgg_instanceof($event_container, 'group')) {
  1850. elgg_push_context('groups');
  1851. elgg_set_page_owner_guid($event->container_guid);
  1852. elgg_push_breadcrumb(elgg_echo('event_calendar:group_breadcrumb'), 'event_calendar/group/'.$event->container_guid);
  1853. } else {
  1854. elgg_push_breadcrumb(elgg_echo('event_calendar:show_events_title'),'event_calendar/list');
  1855. }
  1856. elgg_push_breadcrumb($event->title,$event->getURL());
  1857. elgg_push_breadcrumb(elgg_echo('event_calendar:review_requests_menu_title'));
  1858. if ($event->canEdit()) {
  1859. $requests = elgg_get_entities_from_relationship(
  1860. array(
  1861. 'relationship' => 'event_calendar_request',
  1862. 'relationship_guid' => $event_guid,
  1863. 'inverse_relationship' => TRUE,
  1864. 'limit' => 0)
  1865. );
  1866. if ($requests) {
  1867. $content = elgg_view('event_calendar/review_requests',array('requests' => $requests, 'entity' => $event));
  1868. } else {
  1869. $content = elgg_echo('event_calendar:review_requests_request_none');
  1870. }
  1871. } else {
  1872. $content = elgg_echo('event_calendar:review_requests_error');
  1873. }
  1874. }
  1875. $params = array('title' => $title, 'content' => $content,'filter' => '');
  1876. $body = elgg_view_layout("content", $params);
  1877. return elgg_view_page($title,$body);
  1878. }
  1879. function event_calendar_handle_menu($event_guid) {
  1880. $event = get_entity($event_guid);
  1881. $event_calendar_personal_manage = elgg_get_plugin_setting('personal_manage', 'event_calendar');
  1882. if ((($event_calendar_personal_manage == 'by_event') && ($event->personal_manage == 'closed'))
  1883. || (($event_calendar_personal_manage == 'closed') || ($event_calendar_personal_manage == 'no'))) {
  1884. $url = "event_calendar/review_requests/$event_guid";
  1885. $item = new ElggMenuItem('event-calendar-0review_requests', elgg_echo('event_calendar:review_requests_menu_title'), $url);
  1886. $item->setSection('event_calendar');
  1887. elgg_register_menu_item('page', $item);
  1888. //elgg_register_menu_item(elgg_echo('event_calendar:review_requests_title'), $CONFIG->wwwroot . "pg/event_calendar/review_requests/".$event_id, '0eventcalendaradmin');
  1889. }
  1890. $event_calendar_add_users = elgg_get_plugin_setting('add_users', 'event_calendar');
  1891. if ($event_calendar_add_users == 'yes') {
  1892. $url = "event_calendar/manage_users/$event_guid";
  1893. $item = new ElggMenuItem('event-calendar-1manage_users', elgg_echo('event_calendar:manage_users:breadcrumb'), $url);
  1894. $item->setSection('event_calendar');
  1895. elgg_register_menu_item('page', $item);
  1896. }
  1897. }
  1898. function event_calendar_get_secret_key() {
  1899. $key_file_name = elgg_get_plugin_setting('ical_auth_file_name','event_calendar');
  1900. if ($key_file_name && file_exists($key_file_name)) {
  1901. $key = (require($key_file_name));
  1902. return $key['tokenSecretKey'];
  1903. } else {
  1904. return FALSE;
  1905. }
  1906. }
  1907. function getLastDayOfMonth($month,$year) {
  1908. return idate('d', mktime(0, 0, 0, ($month + 1), 0, $year));
  1909. }
  1910. function event_calendar_modify_full_calendar($event_guid,$day_delta,$minute_delta,$start_time,$resend,$minutes,$iso_date) {
  1911. $event = get_entity($event_guid);
  1912. if (elgg_instanceof($event,'object','event_calendar') && $event->canEdit()) {
  1913. if ($event->is_event_poll) {
  1914. if (elgg_is_active_plugin('event_poll')) {
  1915. elgg_load_library('elgg:event_poll');
  1916. return event_poll_change($event_guid,$day_delta,$minute_delta,$start_time,$resend,$minutes,$iso_date);
  1917. } else {
  1918. return FALSE;
  1919. }
  1920. } else {
  1921. $event->start_date = strtotime("$day_delta days",$event->start_date)+60*$minute_delta;
  1922. if ($event->end_date) {
  1923. $event->end_date = strtotime("$day_delta days",$event->end_date);
  1924. }
  1925. $times = elgg_get_plugin_setting('times','event_calendar');
  1926. //$inc = 24*60*60*$day_delta+60*$minute_delta;
  1927. //$event->real_end_time += $inc;
  1928. $event->real_end_time = strtotime("$day_delta days",$event->real_end_time)+60*$minute_delta;
  1929. if ($times != 'no') {
  1930. $event->start_time += $minute_delta;
  1931. if ($event->end_time) {
  1932. $event->end_time += $minute_delta;
  1933. }
  1934. }
  1935. $dow = array('monday','tuesday','wednesday','thursday','friday','saturday','sunday');
  1936. $week_repeats = array();
  1937. $weekday_delta = (($day_delta % 7) + 7) % 7; // Imagine delta is -12: ((-12 % 7) + 7 % 7) = +2 (thursdays are saturdays)
  1938. foreach ($dow as $i => $w) {
  1939. $v = 'event-calendar-repeating-'.$w.'-value';
  1940. $new_day = $dow[($i+$weekday_delta)%7];
  1941. $week_repeats[$new_day] = $event->$v;
  1942. }
  1943. foreach ($week_repeats as $w => $value) {
  1944. $v = 'event-calendar-repeating-'.$w.'-value';
  1945. $event->$v = $value;
  1946. }
  1947. return TRUE;
  1948. }
  1949. }
  1950. return FALSE;
  1951. }
  1952. function event_calendar_get_page_content_fullcalendar_events($start_date,$end_date,$filter='all',$container_guid=0,$region='-') {
  1953. //print "$start_date - $end_date";
  1954. $start_ts = strtotime($start_date);
  1955. $end_ts = strtotime($end_date);
  1956. if ($filter == 'all') {
  1957. $events = event_calendar_get_events_between($start_ts,$end_ts,false,0,0,$container_guid,$region);
  1958. } else if ($filter == 'open') {
  1959. $events = event_calendar_get_open_events_between($start_ts,$end_ts,false,0,0,$container_guid,$region);
  1960. } else if ($filter == 'friends') {
  1961. $user_guid = elgg_get_logged_in_user_guid();
  1962. $events = event_calendar_get_events_for_friends_between($start_ts,$end_ts,false,0,0,$user_guid,$container_guid,$region);
  1963. } else if ($filter == 'mine') {
  1964. $user_guid = elgg_get_logged_in_user_guid();
  1965. $events = event_calendar_get_events_for_user_between2($start_ts,$end_ts,false,0,0,$user_guid,$container_guid,$region);
  1966. }
  1967. $event_array = array();
  1968. $times_supported = elgg_get_plugin_setting('times','event_calendar') != 'no';
  1969. $polls_supported = elgg_is_active_plugin('event_poll');
  1970. foreach($events as $e) {
  1971. $event = $e['event'];
  1972. $event_data = $e['data'];
  1973. $c = count($event_data);
  1974. foreach($event_data as $ed) {
  1975. $event_item = array(
  1976. 'guid' => $event->guid,
  1977. 'title' => $event->title,
  1978. 'start' => date('c',$ed['start_time']),
  1979. 'end' => date('c',$ed['end_time']),
  1980. );
  1981. if (!$times_supported || ($event->schedule_type == 'all_day')) {
  1982. $event_item['allDay'] = TRUE;
  1983. } else {
  1984. $event_item['allDay'] = FALSE;
  1985. }
  1986. if ($polls_supported && isset($e['is_event_poll']) && $e['is_event_poll']) {
  1987. $event_item['className'] = 'event-poll-class';
  1988. $event_item['title'] .= ' '.elgg_echo('event_calendar:poll_suffix');
  1989. $event_item['is_event_poll'] = TRUE;
  1990. $event_item['url'] = elgg_get_site_url().'event_poll/vote/'.$event->guid;
  1991. $event_item['minutes'] = $ed['minutes'];
  1992. $event_item['iso_date'] = $ed['iso_date'];
  1993. } else {
  1994. $event_item['id'] = $event->guid;
  1995. $event_item['is_event_poll'] = FALSE;
  1996. $event_item['url'] = elgg_get_site_url().'event_calendar/view/'.$event->guid;
  1997. }
  1998. $event_array[] = $event_item;
  1999. }
  2000. }
  2001. $json_events_string = json_encode($event_array);
  2002. return $json_events_string;
  2003. }
  2004. // right now this does not return repeated events in sorted order, so repeated events only really work properly for the full calendar
  2005. // TODO: find another solution for displaying repeated events
  2006. function event_calendar_flatten_event_structure($events) {
  2007. $flattened = array();
  2008. $guids = array();
  2009. foreach($events as $e) {
  2010. $this_event = $e['event'];
  2011. $guid = $this_event->guid;
  2012. if (!in_array($guid,$guids)) {
  2013. $guids[] = $guid;
  2014. $flattened[] = $this_event;
  2015. }
  2016. }
  2017. return $flattened;
  2018. }
  2019. function event_calendar_queue_reminders() {
  2020. // game plan - get all events up to 60 days ahead
  2021. // with no reminder sent
  2022. // compute reminder period
  2023. // if <= current time, set reminder_queued flag and queue the
  2024. // notification message using the message_queue plugin
  2025. if (elgg_plugin_exists('message_queue')) {
  2026. $now = time();
  2027. // oops - this does not work for repeated events
  2028. // need extra stuff for that
  2029. /*$options = array(
  2030. 'type' => 'object',
  2031. 'subtype' => 'event_calendar',
  2032. 'metadata_name_value_pairs' => array(
  2033. array('name' => 'reminder_queued', 'value' => 'no'),
  2034. array('name' => 'send_reminder', 'value' => 1),
  2035. array('name' => 'start_date', 'value' => $now + 60*24*60*60, 'operand' => '>='),
  2036. ),
  2037. 'limit' => 0,
  2038. );
  2039. $events = elgg_get_entities_from_metadata($options);
  2040. */
  2041. $event_list = event_calendar_get_events_between($now,$now + 60*24*60*60,FALSE,0);
  2042. foreach($event_list as $es) {
  2043. $e = $es['event'];
  2044. if ($e->send_reminder) {
  2045. $reminder_period = 60*$e->reminder_interval*$e->reminder_number;
  2046. if ($e->repeats) {
  2047. // repeated events require more complex handing
  2048. foreach($es['data'] as $d) {
  2049. // if event falls in the reminder period
  2050. if ($d->start_time - $reminder_period >= $now) {
  2051. // and the reminder has not already been queued
  2052. if (!event_calendar_repeat_reminder_logged($e,$d->start_time)) {
  2053. // set the reminder queued flag
  2054. event_calendar_repeat_reminder_log($e,$d->start_time);
  2055. // queue the reminder for sending
  2056. event_calendar_queue_reminder($e);
  2057. }
  2058. break;
  2059. }
  2060. }
  2061. } else {
  2062. // if this is just a normal non-repeated event, then we just need to set a flag and queue the reminder
  2063. if (($e->reminder_queued != 'yes') && ($e->start_date - $now <= $reminder_period)) {
  2064. $e->reminder_queued = 'yes';
  2065. event_calendar_queue_reminder($e);
  2066. }
  2067. }
  2068. }
  2069. }
  2070. }
  2071. }
  2072. function event_calendar_repeat_reminder_log($e,$start) {
  2073. // this simple log just uses annotations on the event
  2074. // TODO - remove log entries for past events
  2075. create_annotation($e->guid, 'repeat_reminder_log_item', $start, '',0,ACCESS_PUBLIC);
  2076. }
  2077. function event_calendar_repeat_reminder_logged($e,$start) {
  2078. $options = array(
  2079. 'guid' => $e->guid,
  2080. 'annotation_name' => 'repeat_reminder_log_item',
  2081. 'annotation_value' => $start,
  2082. 'limit' => 1
  2083. );
  2084. if (elgg_get_annotations($options)) {
  2085. return TRUE;
  2086. } else {
  2087. return FALSE;
  2088. }
  2089. }
  2090. function event_calendar_queue_reminder($e) {
  2091. elgg_load_library('elgg:message_queue');
  2092. $subject = elgg_echo('event_calendar:reminder:subject',array($e->title));
  2093. $time_string = event_calendar_get_formatted_time($e);
  2094. $body = elgg_echo('event_calendar:reminder:body',array($e->title,$time_string,$e->getURL()));
  2095. $m = message_queue_create_message($subject,$body);
  2096. if ($m) {
  2097. $users = event_calendar_get_users_for_event($e->guid,0);
  2098. foreach($users as $u) {
  2099. message_queue_add($m->guid,$u->guid);
  2100. }
  2101. message_queue_set_for_sending($m->guid);
  2102. }
  2103. }
  2104. /*function event_calendar_create_bbb_conf($event) {
  2105. $bbb_security_salt = elgg_get_plugin_setting('bbb_security_salt','event_calendar');
  2106. $bbb_server_url = rtrim(elgg_get_plugin_setting('bbb_server_url','event_calendar'), '/') . '/';
  2107. if ($bbb_security_salt) {
  2108. $day_in_minutes = 60*24;
  2109. $now = time();
  2110. // fix duration bug
  2111. # $duration = (int)(($event->real_end_time-$event->start_date)/60)+$day_in_minutes;
  2112. $duration = (int)(($event->real_end_time-$now)/60)+$day_in_minutes;
  2113. $title = urlencode($event->title);
  2114. $params = "name=$title&meetingID={$event->guid}&duration=$duration";
  2115. $checksum = sha1('create'.$params.$bbb_security_salt);
  2116. $params .= "&checksum=$checksum";
  2117. // create curl resource
  2118. $ch = curl_init();
  2119. // set url
  2120. curl_setopt($ch, CURLOPT_URL, $bbb_server_url.'api/create?'.$params);
  2121. //return the transfer as a string
  2122. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2123. // $output contains the output string
  2124. $output = curl_exec($ch);
  2125. // close curl resource to free up system resources
  2126. curl_close($ch);
  2127. #error_log("BBB create request:");
  2128. #error_log($bbb_server_url.'api/create?'.$params);
  2129. #error_log("BBB create response:");
  2130. #error_log($output);
  2131. $xml = new SimpleXMLElement($output);
  2132. if ($xml->returncode == 'SUCCESS') {
  2133. $event->bbb_attendee_password = (string) $xml->attendeePW;
  2134. $event->bbb_moderator_password = (string) $xml->moderatorPW;
  2135. } else {
  2136. register_error(elgg_echo('event_calendar:bbb_create_error',array($xml->message)));
  2137. }
  2138. } else {
  2139. register_error(elgg_echo('event_calendar:bbb_settings_error'));
  2140. }
  2141. }*/
  2142. // utility function for BBB api calls
  2143. function event_calendar_bbb_api($api_function,$params=NULL) {
  2144. $bbb_security_salt = elgg_get_plugin_setting('bbb_security_salt','event_calendar');
  2145. $bbb_server_url = rtrim(elgg_get_plugin_setting('bbb_server_url','event_calendar'), '/') . '/';
  2146. if ($bbb_security_salt) {
  2147. if (isset($params) && is_array($params) && count($params) > 0) {
  2148. $query = array();
  2149. foreach($params as $k => $v) {
  2150. $query[] = $k.'='.rawurlencode($v);
  2151. }
  2152. $qs = implode('&',$query);
  2153. } else {
  2154. $qs = '';
  2155. }
  2156. $checksum = sha1($api_function.$qs.$bbb_security_salt);
  2157. if ($qs) {
  2158. $qs .= "&checksum=$checksum";
  2159. }
  2160. // create curl resource
  2161. $ch = curl_init();
  2162. // set url
  2163. curl_setopt($ch, CURLOPT_URL, $bbb_server_url.'api/'.$api_function.'?'.$qs);
  2164. //return the transfer as a string
  2165. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  2166. // $output contains the output string
  2167. $output = curl_exec($ch);
  2168. // close curl resource to free up system resources
  2169. curl_close($ch);
  2170. error_log("BBB api call: ".$api_function);
  2171. error_log(print_r($params,TRUE));
  2172. error_log("BBB response: \n".$output);
  2173. return $output;
  2174. } else {
  2175. return FALSE;
  2176. }
  2177. }
  2178. function event_calendar_create_bbb_conf($event) {
  2179. $day_in_minutes = 60*24;
  2180. $now = time();
  2181. // fix duration bug
  2182. # $duration = (int)(($event->real_end_time-$event->start_date)/60)+$day_in_minutes;
  2183. $duration = (int)(($event->real_end_time-$now)/60)+$day_in_minutes;
  2184. if ($duration > 0) {
  2185. $title = urlencode($event->title);
  2186. $output = event_calendar_bbb_api('create',array('meetingID'=>$event->guid,'name'=>$title,'duration'=>$duration));
  2187. if ($output) {
  2188. $xml = new SimpleXMLElement($output);
  2189. if ($xml->returncode == 'SUCCESS') {
  2190. $event->bbb_attendee_password = (string) $xml->attendeePW;
  2191. $event->bbb_moderator_password = (string) $xml->moderatorPW;
  2192. return TRUE;
  2193. } else {
  2194. return FALSE;
  2195. }
  2196. } else {
  2197. return FALSE;
  2198. }
  2199. } else {
  2200. return FALSE;
  2201. }
  2202. }
  2203. // checks to see if a BBB conference is actually running
  2204. function event_calendar_is_conference_running($event) {
  2205. $output = event_calendar_bbb_api('isMeetingRunning',array('meetingID'=>$event->guid));
  2206. if (!$output) {
  2207. return FALSE;
  2208. } else {
  2209. $xml = new SimpleXMLElement($output);
  2210. if ($xml->returncode == 'SUCCESS' && $xml->running == 'true') {
  2211. return TRUE;
  2212. } else {
  2213. return FALSE;
  2214. }
  2215. }
  2216. }
  2217. // checks to see if a BBB conference exists
  2218. function event_calendar_conference_exists($event) {
  2219. $output = event_calendar_bbb_api('getMeetingInfo',array('meetingID'=>$event->guid,'password'=>$event->bbb_moderator_password));
  2220. if (!$output) {
  2221. return FALSE;
  2222. } else {
  2223. $xml = new SimpleXMLElement($output);
  2224. if ($xml->returncode == 'SUCCESS' && $xml->meetingID == $event->guid) {
  2225. return TRUE;
  2226. } else {
  2227. return FALSE;
  2228. }
  2229. }
  2230. }
  2231. // forwards to the join link
  2232. // this function assumes that the conference is running
  2233. function event_calendar_join_conference($event) {
  2234. forward(event_calendar_get_join_bbb_url($event));
  2235. }
  2236. function event_calendar_get_join_bbb_url($event) {
  2237. $bbb_security_salt = elgg_get_plugin_setting('bbb_security_salt','event_calendar');
  2238. $bbb_server_url = rtrim(elgg_get_plugin_setting('bbb_server_url','event_calendar'), '/') . '/';
  2239. $user = elgg_get_logged_in_user_entity();
  2240. $full_name = urlencode($user->name);
  2241. if ($event->canEdit()) {
  2242. $password = urlencode($event->bbb_moderator_password);
  2243. } else {
  2244. $password = urlencode($event->bbb_attendee_password);
  2245. }
  2246. $params = "fullName=$full_name&meetingID={$event->guid}&userID={$user->username}&password=$password";
  2247. $checksum = sha1('join'.$params.$bbb_security_salt);
  2248. $params .= "&checksum=$checksum";
  2249. $url = $bbb_server_url.'api/join?'.$params;
  2250. return $url;
  2251. }
  2252. // returns TRUE if the given user can add an event to the given calendar
  2253. // if group_guid is 0, this is assumed to be the site calendar
  2254. function event_calendar_can_add($group_guid=0,$user_guid=0) {
  2255. if (!$user_guid) {
  2256. if (elgg_is_logged_in()) {
  2257. $user_guid = elgg_get_logged_in_user_guid();
  2258. } else {
  2259. return FALSE;
  2260. }
  2261. }
  2262. if ($group_guid) {
  2263. if (!event_calendar_activated_for_group($group_guid)) {
  2264. return FALSE;
  2265. }
  2266. $group = get_entity($group_guid);
  2267. if (elgg_instanceof($group,'group')) {
  2268. $group_calendar = elgg_get_plugin_setting('group_calendar', 'event_calendar');
  2269. if (!$group_calendar || $group_calendar == 'members') {
  2270. return $group->canWriteToContainer($user_guid);
  2271. } else if ($group_calendar == 'admin') {
  2272. if ($group->canEdit($user_guid)) {
  2273. return TRUE;
  2274. } else {
  2275. return FALSE;
  2276. }
  2277. }
  2278. } else {
  2279. return FALSE;
  2280. }
  2281. } else {
  2282. $site_calendar = elgg_get_plugin_setting('site_calendar', 'event_calendar');
  2283. if (!$site_calendar || $site_calendar == 'admin') {
  2284. // only admins can post directly to the site-wide calendar
  2285. return elgg_is_admin_user($user_guid);
  2286. } else if ($site_calendar == 'loggedin') {
  2287. // any logged-in user can post to the site calendar
  2288. return TRUE;
  2289. }
  2290. }
  2291. return FALSE;
  2292. }