CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • Code Contribution - An event listing page
Pages: [1] 2

Author Topic: Code Contribution - An event listing page  (Read 5450 times)

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Code Contribution - An event listing page
August 12, 2008, 03:12:38 pm
I'd like to make a code contribution (snippit) that we use to show events listed (see http://jicny.com/events).  I'd appreciate a code review because occasionally an event shows up even after it has been deleted.  I think this could be useful - especially as an email template:
---------------------------------------------------------------------------------------
<div id="eventspage" class="all2008">
<a name="top"></a> <?php

function cmp_date2($a,$b) {
  if ($a['start_date'] > $b['start_date']) return 1;
  if ($a['start_date'] < $b['start_date']) return -1;
  return 0;
}

if (module_exists('civicrm')) {
  civicrm_initialize(TRUE);
  require_once 'api/v2/Event.php';
  // This example limits listing to public and active events
  $params = array ('is_public'     => 1, 'is_active'     => 1);
  $myEvents = civicrm_event_search( $params );
  if ($myEvents) {
    $count = 0;
    $last = '';
    usort($myEvents,'cmp_date2');


print "<ul>\n";
  for ($i=0;$i < sizeof($myEvents); $i++) {
$event = $myEvents[$i];
 $now = date('Y-m-d H:i:s');
      if ($now > $event['start_date']) continue;
 list($title_place, $title_desc) = split(":",$event['title'],2);
print "<li><a href=\"#event$i\">$title_place</a></li> \n";
}
print "</ul>\n";


   for ($j=0;$j < sizeof($myEvents); $j++) {


    $event = $myEvents[$j];
      $now = date('Y-m-d H:i:s');
      if ($now > $event['start_date']) continue;
      $startdate = date('D M j Y',strtotime($event['start_date']));
      $enddate   = date('D M j Y',strtotime($event['end_date']));
      $starttime = date('g:i  A',strtotime($event['start_date']));
     

     $eventid = $event['id'];

      list($title_place, $title_desc) = split(":",$event['title'],2);
     
      $display = l($title_place.' '.$title_desc, 'civicrm/event/info', array(), 'reset=1&id='.$event['id']);
   echo '<div>';
echo '<a name="event' . $j . '"></a>';
 echo  '<h3>'.$display.'</h3>';

 $discr = $event['description'];
 echo  '<p>'.$discr.'</p>';

if ($last != $startdate) {
        $start_date = '<b> WHEN: </b>'.$startdate;
      }
print $start_date.'<br/>';

print '<b>TIME: </b>'.$starttime ;
echo '<br>';
require_once 'CRM/Core/DAO/LocBlock.php';
$addId = CRM_Core_DAO::getfieldvalue('CRM_Core_DAO_LocBlock',$event['loc_block_id'],'address_id');
if($addId){
$query = "SELECT  street_address , supplemental_address_1 , city , sp.abbreviation ,postal_code, cc.name FROM civicrm_address LEFT JOIN civicrm_state_province sp ON (civicrm_address.state_province_id =sp.id )LEFT JOIN civicrm_country cc ON (civicrm_address.country_id =cc.id ) WHERE civicrm_address.id=$addId";
$params = array();
$dao =& CRM_Core_DAO::executeQuery( $query, $params );
 $dao->fetch( );
$location = '<b>WHERE: </b>' ;
if($dao->street_address){
$location.= $dao->street_address.'<br />';}
if($dao->supplemental_address_1){
$location.= $dao->supplemental_address_1.'<br />';}
if($dao->city){
$location.= $dao->city.','.$dao->abbreviation.' '.$dao->postal_code.'<br />';}
print $location;
}

require_once 'CRM/Event/DAO/EventPage.php';
        $pageGroup= new CRM_Event_DAO_EventPage($eventid);
        $pageID = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_EventPage', $eventid, 'id', 'event_id');

 require_once 'CRM/Core/DAO/OptionGroup.php';
        $optionGroupDAO = new CRM_Core_DAO_OptionGroup();
        $optionGroupDAO->name = 'civicrm_event_page.amount.'.$pageID;
        $ss = array();
        $i = 0;
        if ($optionGroupDAO->find(true) ) {
            $optionGroupId = $optionGroupDAO->id;
            require_once 'CRM/Core/DAO/OptionValue.php';
            $optionValueDAO = new CRM_Core_DAO_OptionValue();
            $optionValueDAO->option_group_id = $optionGroupDAO->id;
            $optionValueDAO->find();
            while ( $optionValueDAO->fetch( ) ) {

                $ss['optionLable'][$i] = $optionValueDAO->label;
                $ss['optionValue'][$i] = $optionValueDAO->value;
                $i++;
            }
        }
 require_once 'CRM/Core/BAO/PriceSet.php';
 $priceSetId = CRM_Core_BAO_PriceSet::getFor( 'civicrm_event_page', $eventid );
if($ss['optionValue'] && !$priceSetId){
       print '<b>COST: </b>'.'$'.$ss['optionValue'][0].' per person' ;
}
if ($event['is_online_registration'] ) {

        echo "<p class=\"regbutton\"><a href=\"http://jicny.com/civicrm/event/register?reset=1&id=" . $event['id'] . "\"><img src = \"/files/images/register_online_button.gif\" /></a></p>";
}

echo '<p class="totop"><a href="#top" class="totop">top</a></p>';
echo '</div>';
      $count++;
      $last = $startdate;
 //     if ($count > 8) break;
// this limits the number of events to 8 - put in whatever number suits you
    }
    if ($count > 0) {

    } else {
      echo 'No events found.';
    }
  } else {
    echo 'No events found.';
  }
}
?></div>
<p>&nbsp;</p>


geilhufe

  • I post frequently
  • ***
  • Posts: 293
  • Karma: 33
    • Social Source Software
Re: Code Contribution - An event listing page
August 12, 2008, 06:28:55 pm
might also want to put that on the wiki:
http://wiki.civicrm.org/confluence/display/CRMDOC/Using+CiviCRM+APIs+-+Code+Snippets
Drupal and CiviCRM consulting, strategy and configuration
http://www.social-source.com/

Michał Mach

  • Ask me questions
  • ****
  • Posts: 748
  • Karma: 59
    • CiviCRM site
  • CiviCRM version: latest
  • CMS version: Drupal and Joomla latest
  • MySQL version: numerous
  • PHP version: 5.3 and 5.2
Re: Code Contribution - An event listing page
August 13, 2008, 05:11:59 am
Sonicthoughts,

Just to confirm, I tested your snippet with PHP code filter in Drupal 6.3 and CiviCRM 2.1 devel and it works nicely - thanks for the contribution!

Thx,
m
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

My absolute favourite: Wordpress Integration!.

Donate Now!

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: Code Contribution - An event listing page
August 13, 2008, 03:16:55 pm
great!  Please let me know if you come up with any enhancements!

You should know that i had a couple of issues with this:
1. I've seen an "undefined" briefly during the actual event.  I was never able to troubleshoot but it could be related to whether or not an end time is specified.  note there could be some situations where you want the event to be listed while it occurs (especially multi-day events, or so people can get the event info to show up) and others where it is not what you want.
2. I ran into some authentication issues.  i think they are ok if the role has access to the civievent module.  that may generally be how API access is controlled.   

Michał Mach

  • Ask me questions
  • ****
  • Posts: 748
  • Karma: 59
    • CiviCRM site
  • CiviCRM version: latest
  • CMS version: Drupal and Joomla latest
  • MySQL version: numerous
  • PHP version: 5.3 and 5.2
Re: Code Contribution - An event listing page
August 14, 2008, 03:47:44 am
Hey,

Somebody from the team (I didn't find out who yet, but will come back with credits as soon as I know  :) ) was playing with the script and came up with simplified version that's visible on sandbox. Here's the code in case somebody not keen in playing with PHP needed very simple version (date + event title).

Code: [Select]
<?php

function cmp_date($a,$b) {
  if (
$a['start_date'] > $b['start_date']) return 1;
  if (
$a['start_date'] < $b['start_date']) return -1;
  return 
0;
}

if (
module_exists('civicrm')) {
  
civicrm_initialize(TRUE);
  require_once 
'api/v2/Event.php';
  
// This example limits listing to public and active events
  
$params = array ('is_public'     => 1, 'is_active'     => 1);
  
$myEvents = civicrm_event_search( $params );
  if (
$myEvents) {
    
$count = 0;
    
$last = '';
    
usort($myEvents,'cmp_date');
    foreach (
$myEvents as $event) {
      
$now = date('Y-m-d H:i:s');
      if (
$now > $event['start_date']) continue;
      
$startdate = date('D M j Y',strtotime($event['start_date']));
      
$enddate = date('D M j Y',strtotime($event['end_date']));
             
      
$eventid = $event['id'];
      list(
$title_place, $title_desc) = split(":",$event['title'],2);
      if (
$last != $startdate) {
        
$display = '<br /><b>'.$startdate.'</b><br />';
      }
      
$display .= l($title_place.' '.$title_desc, 'civicrm/event/info', array( 'query' => 'reset=1&id='.$event['id'] )).'<br />';
      echo 
$display;
      
$count++;
      
$last = $startdate;
      if (
$count > 8) break;
      
// this limits the number of events to 8 - put in whatever number suits you
    
}
    if (
$count > 0) {

    } else {
      echo 
'No events found.';
    }
  } else {
    echo 
'No events found.';
  }
}
?>
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

My absolute favourite: Wordpress Integration!.

Donate Now!

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: Code Contribution - An event listing page
August 14, 2008, 04:43:04 am
yes - in fact this is the version that we "borrowed" to extend and we use both!  this we use as a block, and the one i provided as an event listing page.

S.

ericj

  • Guest
Re: Code Contribution - An event listing page
August 15, 2008, 03:43:00 pm
Hi,
So I'd like to implement this on my site.  I read the "Getting Started with Standalone Scripts" but am still a little confused on how to get this up and running.  Does anyone know where I can get some "do this, now do this - you dummy" instructions?

Thanks in advance for the help.

Eric

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Code Contribution - An event listing page
August 15, 2008, 07:21:29 pm
Not sure about in joomla but in Drupal you can just publish it as a content item - you have to set the content type to php though.
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Code Contribution - An event listing page
August 16, 2008, 11:33:10 am
Eric:

Check http://sandbox.civicrm.org/civicrm/, there is "Upcoming Events" block.

Kurund
Found this reply helpful? Support CiviCRM

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: Code Contribution - An event listing page
August 16, 2008, 08:23:33 pm
just to clarify, we used the upcoming event block as the base but made several enhancements.  i also have a scrolling event block if anyone is interested. 

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Code Contribution - An event listing page
August 17, 2008, 02:14:40 am
sonicthoughts:

Can you add it to http://wiki.civicrm.org/confluence/display/CRMDOC/Using+CiviCRM+APIs+-+Code+Snippets#UsingCiviCRMAPIs-CodeSnippets-CreateEventListingPage(basedonEventBlockcode)

kurund
Found this reply helpful? Support CiviCRM

ericj

  • Guest
Re: Code Contribution - An event listing page
August 17, 2008, 08:07:54 pm
Thank you for the help Eileen, Kurund and Sonicthoughts.  I like the code used for http://jicny.com/events.  I'd be interested in the scrolling page code if you're willing to share.  As I understand it, I simply publish new 'content' item.  I will give that a try and if I have any problems...I'll be back!  :)

ericj

  • Guest
Re: Code Contribution - An event listing page
August 18, 2008, 11:57:22 am
Ok, I'm back.  So, I must be overthinking this or something (or just extra newbish).  How do I publish this aforementioned code as a "content item?"  I have tried to create a node content template, but was unable to get it to work.  I am using Drupal 5.8 if that helps.  Thanks again for the help.

Eileen

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4195
  • Karma: 218
    • Fuzion
Re: Code Contribution - An event listing page
August 18, 2008, 12:03:17 pm
I created a static page & pasted it including php tags - but change the input format
Make today the day you step up to support CiviCRM and all the amazing organisations that are using it to improve our world - http://civicrm.org/contribute

ericj

  • Guest
Re: Code Contribution - An event listing page
August 18, 2008, 04:32:03 pm
Eileen, thank you, that worked great!  I was definitely over-thinking it. 

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • Code Contribution - An event listing page

This forum was archived on 2017-11-26.