We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10555
    • 32 Posts
    I am having trouble converting a php calendar and getting it to functions properly within modx. This is the code. I understand you have to return the output not sure how I build that output from this file

    Modx: Revolution 2.2 pl02

    Here is the original file

    <?php 
    $first_day_of_week = 'Monday'; // or 'Sunday'
    
    $type = CAL_GREGORIAN;
    
    if(isset($_GET['month'])) $month = trim(stripslashes(strip_tags($_GET['month'])));
    if(isset($_GET['year'])) $year = trim(stripslashes(strip_tags($_GET['year'])));
    
    if(!isset($_GET['month']) || !is_numeric($_GET['month'])) $month = date('n'); // Month ID, 1 through to 12.
    if(!isset($_GET['year']) || !is_numeric($_GET['year'])) $year = date('Y'); // Year in 4 digit 2009 format.
    
    $today = date('Y/n/d');
    
    $day_count = cal_days_in_month($type, $month, $year); // Get the amount of days in the chosen month to give to our function.
    
    $output = "<div id='calendar'>";
    $output .= "<div id='calendar_wrap'>";
    
    // Function for year change. //
    
    $last_month = $month - 1;
    $next_month = $month + 1;
    
    $last_year = $year - 1;
    $next_year = $year + 1;
    
    if($month == 12):
    	$change_year = $year;
    	$change_month  = $last_month;
    elseif($month == 1):
    	$change_year = $last_year;
    	$change_month  = '12';
    else:
    	$change_year = $year;
    	$change_month  = $last_month;
    endif;
    
    if($month == 1):
    	$change_year_next = $year;
    	$change_month_next  = $next_month;
    elseif($month == 12):
    	$change_year_next = $next_year;
    	$change_month_next  = '1';
    else:
    	$change_year_next = $year;
    	$change_month_next  = $next_month;
    endif;
    
    $output .= "<div class='title_bar'>";
    
    $output .= "<a href='".$_SERVER['PHP_SELF']."?month=". $change_month ."&year=". $change_year ."'><div class='previous'></div></a>";
    $output .= "<a href='".$_SERVER['PHP_SELF']."?month=". $change_month_next ."&year=". $change_year_next ."'><div class='next'></div></a>";
    $output .= "<h2 class='month'>" . date('F',  mktime(0,0,0,$month,1)) . " " . $year . "</h2>";
    
    
    $output .= "</div>";
    
    /* Previous Month */
    
    $first_day = date('N', mktime(0,0,0,$month,1,$year));
    
    if ( ($first_day_of_week=='Monday' && $first_day != 1) || ($first_day_of_week=='Sunday' && $first_day != 7) ) :
    
    	$last_month_day_count = cal_days_in_month($type, $change_month, $change_year);
    
    	if ($first_day_of_week=='Monday') :
    		if ( 'Monday' == date('l', mktime(0,0,0,$change_month,$last_month_day_count,$change_year)) ) :
    			$final_day = date('j', mktime(0,0,0,$change_month,$last_month_day_count,$change_year));
    		else :
    			$final_day = date('j', strtotime('last Monday', mktime(0,0,0,$change_month,$last_month_day_count,$change_year) ) );
    		endif;
    	else :
    		if ( 'Sunday' == date('l', mktime(0,0,0,$change_month,$last_month_day_count,$change_year)) ) :
    			$final_day = date('j', mktime(0,0,0,$change_month,$last_month_day_count,$change_year));
    		else :
    			$final_day = date('j', strtotime('last Sunday', mktime(0,0,0,$change_month,$last_month_day_count,$change_year) ) );
    		endif;
    	endif;
    
    	// Not a monday so fill the gap
    	for($i=$final_day; $i<=$last_month_day_count; $i++):
    
    		$date = $change_year.'/'.$change_month.'/'.$i;
    
    		$get_name = date('l', strtotime($date));
    		$month_name = date('F', strtotime($date));
    		$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
    
    		$count = count_events($i,$change_month,$change_year);
    		if($count >= 1)
    		$output = "<a href='day_view.php?day=$i&month=$change_month&year=$change_year' title='$i $month_name' rel='day_view'>";
    		$output .= "<div class='cal_day last_month'>"; // Calendar Day
    
    			$output.= "<div class='day_heading'>" . $day_name . "</div>";
    
    			if($count >= 1) $output.= "<div class='day_count'><span class='event'>" . $count . "</span></div>";
    
    			if($today == $date):
    				$output.= "<div class='day_number today'>" . $i . "</div>";
    			else:
    				$output.= "<div class='day_number'>" . $i . "</div>";
    			endif;
    
    		$output.= "</div>";
    		$output.= "</a>";
    	endfor;
    
    endif;
    
    /* Current Month */
    
    for($i=1; $i<= $day_count; $i++): // Start of for $i
    
    	$date = $year.'/'.$month.'/'.$i;
    
    	$get_name = date('l', strtotime($date));
    	$month_name = date('F', strtotime($date));
    	$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
    
    	$count = count_events($i,$month,$year);
    	if($count >= 1)
    	$output = "<a href='day_view.php?day=$i&month=$month&year=$year' title='$i $month_name' rel='day_view'>";
    	$output .= "<div class='cal_day'>"; // Calendar Day
    
    		$output .= "<div class='day_heading'>" . $day_name . "</div>";
    
    		if($count >= 1) echo "<div class='day_count'><span class='event'>" . $count . "</span></div>";
    
    		if($today == $date):
    			$output .= "<div class='day_number today'>" . $i . "</div>";
    		else:
    			$output .= "<div class='day_number'>" . $i . "</div>";
    		endif;
    
    	$output .= "</div>";
    	$output .= "</a>";
    
    endfor; // EOF for $i
    
    /* Next Month */
    
    $last_day = date('N', mktime(0,0,0,$month,$day_count,$year));
    
    if ( ($first_day_of_week=='Monday' && $last_day != 7) || ($first_day_of_week=='Sunday' && $last_day != 1) ) :
    
    	if ($first_day_of_week=='Monday') :
    		if ( 'Sunday' == date('l', mktime(0,0,0,$change_month_next,1,$change_year_next)) ) :
    			$first_day = date('j', mktime(0,0,0,$change_month_next,1,$change_year_next));
    		else :
    			$first_day = date('j', strtotime('first Sunday', mktime(0,0,0,$change_month_next,1,$change_year_next) ) );
    		endif;
    	else :
    		if ( 'Saturday' == date('l', mktime(0,0,0,$change_month_next,1,$change_year_next)) ) :
    			$first_day = date('j', mktime(0,0,0,$change_month_next,1,$change_year_next));
    		else :
    			$first_day = date('j', strtotime('first Saturday', mktime(0,0,0,$change_month_next,1,$change_year_next) ) );
    		endif;
    	endif;
    
    
    	// Not a monday so fill the gap
    	for($i=1; $i<=$first_day; $i++):
    
    		$date = $change_year_next.'/'.$change_month_next.'/'.$i;
    
    		$get_name = date('l', strtotime($date));
    		$month_name = date('F', strtotime($date));
    		$day_name = substr($get_name, 0, 3); // Trim day name to 3 chars
    
    		$count = count_events($i,$change_month_next,$change_year_next);
    		if($count >= 1)
    		$output = "<a href='day_view.php?date=$i&month=$change_month_next&year=$change_year_next' title='$i $month_name' rel='day_view'>";
    		$output .= "<div class='cal_day next_month'>"; // Calendar Day
    
    			$output .= "<div class='day_heading'>" . $day_name . "</div>";
    
    			if($count >= 1) $output .= "<div class='day_count'><span class='event'>" . $count . "</span></div>";
    
    			if($today == $date):
    				$output .= "<div class='day_number today'>" . $i . "</div>";
    			else:
    				$output .= "<div class='day_number'>" . $i . "</div>";
    			endif;
    
    		$output .= "</div>";
    		$output .= "</a>";
    
    	endfor;
    endif;
    
    
    ?>
      • 1778
      • 659 Posts
      Hi,

      First you have to return the $output at the end of your php.

      return $output;
      


      Then create a snippet (call it as you want, "myCalendar" for example) copy paste your php code in it. Now you have a snippet ready to be called in the page (Modx Resource) you want your calendar to be displayed. Call it
      [[myCalendar]]
      // or if you want to call it uncached
      [[!myCalendar]]
      


      Hope it is what you're looking for...
      Cheers
        • 10555
        • 32 Posts
        I have done this and I get a blank page making me think there is an error
          • 3749
          • 24,544 Posts
          Here are my guesses on what might be wrong: wink


          * day_view.php file is not being found (click on the link in "view source" or try something like this).

          $day_view_path = MODX_ASSETS_PATH . /whatever/directory/day_view.php';


          * count_events function is not defined (could also be a path issue)

          * $day, $month, and/or $year are not getting set (all are set in if statements).


          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/modx.html


          [ed. note: BobRay last edited this post 14 years, 8 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 32316
            • 387 Posts
            Edit... BobRay beat me to the punch...

            Are there any files that go with this?
            I tried it and got this error:

            Fatal error: Call to undefined function count_events() in...

            and indeed count_events() is used in the code posted and is not defined in it.

            I'm guessing you're missing a file (or two) [ed. note: whistlemaker last edited this post 14 years, 8 months ago.]
              • 10555
              • 32 Posts
              Thanks Bob. that was it.

              I forgot to include my files now that I have that figured out when i try to change months its not changing. Its just bringing back the ssame page I am assuming I am missing some placeholders or something here is the whats broken. thanks again for helping

              $output .= "<a href='members-area.html?month=". $change_month ."&year=". $change_year ."'><div class='previous'></div></a>";
              $output .= "<a href='".$_SERVER['PHP_SELF']."?month=". $change_month_next ."&year=". $change_year_next ."'><div class='next'></div></a>";
              $output .= "<h2 class='month'>" . date('F',  mktime(0,0,0,$month,1)) . " " . $year . "</h2>";



                • 10555
                • 32 Posts
                Hi Guys never mind I had [[calendar]] instead of [[!calendar]] to make it cache. Thanks again