1<?php 2require_once("classes/DBUtils.class.php"); 3/* 4 This class is used to contain values for meal planning. Different views and 5 styles can be set through this class. Most of the data handling and parsing should 6 be included in this class, including loading and saving 7*/ 8class MealPlan { 9 var $daysAbbr = array(); 10 var $daysFull = array(); 11 var $monthsAbbr = array(); 12 var $monthsFull = array(); 13 // Date that we are currently looking at 14 var $currentDate = NULL; 15 var $currentDay = NULL; 16 var $currentMonth = NULL; 17 var $currentYear = NULL; 18 // The real date 19 var $realDate = NULL; 20 var $realDay = NULL; 21 var $realMonth = NULL; 22 var $realYear = NULL; 23 // Save the meal plan info in memory 24 var $mealplanItems = array(); 25 var $mealList = array(); // names and ids of the meals 26 // The Start Day of the week 27 var $startWeekDay = NULL; 28 29 /** 30 Initializes some default values (With translation) 31 */ 32 function MealPlan($date) { 33 global $LangUI, $DB_LINK, $db_table_settings; 34 35 // Get the Start Day 36 $sql = "SELECT setting_mp_day FROM $db_table_settings"; 37 $result = $DB_LINK->Execute($sql); 38 DBUtils::checkResult($result, NULL, NULL, $sql); // Error check 39 $this->startWeekDay = $result->fields['setting_mp_day']; 40 41 /* Initialize the months and days */ 42 $this->daysAbbr = array( 43 $LangUI->_('Sun'), 44 $LangUI->_('Mon'), 45 $LangUI->_('Tue'), 46 $LangUI->_('Wed'), 47 $LangUI->_('Thu'), 48 $LangUI->_('Fri'), 49 $LangUI->_('Sat') 50 ); 51 $this->daysFull = array( 52 $LangUI->_('Sunday'), 53 $LangUI->_('Monday'), 54 $LangUI->_('Tuesday'), 55 $LangUI->_('Wednesday'), 56 $LangUI->_('Thursday'), 57 $LangUI->_('Friday'), 58 $LangUI->_('Saturday') 59 ); 60 $this->monthsAbbr = array($LangUI->_('Jan'), 61 $LangUI->_('Feb'), 62 $LangUI->_('Mar'), 63 $LangUI->_('Apr'), 64 $LangUI->_('May'), 65 $LangUI->_('Jun'), 66 $LangUI->_('Jul'), 67 $LangUI->_('Aug'), 68 $LangUI->_('Sep'), 69 $LangUI->_('Oct'), 70 $LangUI->_('Nov'), 71 $LangUI->_('Dec') 72 ); 73 $this->monthsFull = array($LangUI->_('January'), 74 $LangUI->_('February'), 75 $LangUI->_('March'), 76 $LangUI->_('April'), 77 $LangUI->_('May'), 78 $LangUI->_('June'), 79 $LangUI->_('July'), 80 $LangUI->_('August'), 81 $LangUI->_('September'), 82 $LangUI->_('October'), 83 $LangUI->_('November'), 84 $LangUI->_('December') 85 ); 86 $this->initDate($date); // initalize the date variables 87 } 88 89 /** 90 Initalizes the current date settings for use later on 91 */ 92 function initDate($date) { 93 $this->currentDate = $date; 94 list($this->currentMonth,$this->currentDay,$this->currentYear) = split("-",$date); 95 // Now set the real date 96 $date = date('m-d-Y'); 97 $this->realDate = $date; 98 list($this->realMonth,$this->realDay,$this->realYear) = split("-",$date); 99 } 100 101 /** 102 Loads the meal specified meal plan into memory 103 @param $start The date to start at 104 @param $end The date to end with 105 @return true, if sucessful, false if not (the results are set in a class var) 106 */ 107 function load($start,$end) { 108 global $SMObj, $DB_LINK, $db_table_mealplans, $db_table_recipes; 109 $sql = "SELECT mplan_date,mplan_meal,mplan_servings,mplan_recipe,recipe_name FROM recipe_mealplans 110 LEFT JOIN $db_table_recipes ON recipe_id = mplan_recipe 111 WHERE mplan_owner = '" . $SMObj->getUserLoginID() . "' AND mplan_date BETWEEN '".$DB_LINK->addq($start, get_magic_quotes_gpc())."' AND '".$DB_LINK->addq($end, get_magic_quotes_gpc())."'"; 112 $mplan = $DB_LINK->Execute($sql); 113 // Error check 114 DBUtils::checkResult($mplan, NULL, NULL, $sql); 115 //echo $sql; 116 while (!$mplan->EOF) { 117 $this->mealplanItems[($mplan->fields['mplan_date'])][] = array( 118 'meal' => $mplan->fields['mplan_meal'], 119 'name' => $mplan->fields['recipe_name'], 120 'id' => $mplan->fields['mplan_recipe'], 121 'servings' => $mplan->fields['mplan_servings']); 122 $mplan->MoveNext(); 123 } 124 } 125 126 /** 127 Deletes all of the meal planner items for the currently set date, this is mainly used so 128 that a fresh set of items can be set 129 @param $date the date to delete from (in ISO format) 130 */ 131 function delete($date) { 132 global $DB_LINK, $db_table_mealplans; 133 $sql = "DELETE FROM $db_table_mealplans WHERE mplan_date='".$DB_LINK->addq($date, get_magic_quotes_gpc())."'"; 134 $rc = $DB_LINK->Execute($sql); 135 DBUtils::checkResult($rc, NULL, NULL, $sql);// Error check 136 } 137 138 /** 139 Adds a meal plan item into the database 140 @param $date The ISO date to save it under 141 @param $meal The id of the meal (breakfast, lunch...) 142 @param $recipe The id of the recipe 143 @param $servings The serving size 144 @param $owner The owner of this item 145 */ 146 function insert($date, $meal, $recipe, $servings, $owner) { 147 global $DB_LINK, $db_table_mealplans, $LangUI; 148 149 $date = $DB_LINK->addq($date, get_magic_quotes_gpc()); 150 $meal = $DB_LINK->addq($meal, get_magic_quotes_gpc()); 151 $recipe = $DB_LINK->addq($recipe, get_magic_quotes_gpc()); 152 $servings = $DB_LINK->addq($servings, get_magic_quotes_gpc()); 153 $owner = $DB_LINK->addq($owner, get_magic_quotes_gpc()); 154 155 $sql = "INSERT INTO $db_table_mealplans (mplan_date, mplan_meal, mplan_recipe, mplan_servings, mplan_owner) 156 VALUES ('$date', $meal, $recipe, $servings, '$owner')"; 157 $rc = $DB_LINK->Execute($sql); 158 DBUtils::checkResult($rc, NULL, $LangUI->_('Recipe is already added to Meal Plan for date:'.$date), $sql);// Error check 159 } 160 161 /** 162 Removes all of the meals currently saved for a day so that meals will only be added if they are wanted. This 163 function can be used in combination with insert(...) in order to remove unwanted recipes 164 @param $date The date to clear in ISO format (use DBUtils) 165 */ 166 function clearDay($date) { 167 global $DB_LINK, $db_table_mealplans; 168 $date = $DB_LINK->addq($date, get_magic_quotes_gpc()); 169 $sql = "DELETE FROM $db_table_mealplans WHERE mplan_date='$date'"; 170 $rc = $DB_LINK->Execute($sql); 171 DBUtils::checkResult($rc,NULL,NULL,$sql); 172 } 173 174 /** 175 Gets the meals for a given day and puts it into html and returns it as a string 176 @param $date The date to get (ISO Format) 177 @param $format The format to put it in, maily big=1, small=2 178 @return String with HTML in it 179 */ 180 function getMeals($date, $format) { 181 global $DB_LINK, $db_table_meals; 182 $output = ""; 183 // If there is nothing to get, don't bother, just return 184 if (!isset($this->mealplanItems[$date])) return ""; 185 // Get the list of all the meal types (breakfast, lunch...) 186 if (count($this->mealList) == 0) { 187 // we have to load the list of meals now 188 $rc = DBUtils::fetchColumn( $db_table_meals, 'meal_name', 'meal_id', 'meal_id' ); 189 $this->mealList = DBUtils::createList($rc, 'meal_id', 'meal_name'); 190 } 191 // Now that we know there is something to get 192 foreach ($this->mealList as $k=>$v) { 193 $count = 0; 194 $str=""; 195 foreach ($this->mealplanItems[$date] as $item) { 196 if ($item['meal'] == $k) { 197 // add this one · 198 if ($format==1) $str .= '<li><a href="index.php?m=recipes&a=view&recipe_id=' . $item['id'] . '">' . $item['name'] . "</a>\n"; 199 else if ($format==2) $str .= '<tr><td class="nav" colspan=2 nowrap><a href="index.php?m=recipes&a=view&recipe_id=' . $item['id'] . '">' . 200 '· ' . $item['name'] . "</a></td></tr>\n"; 201 $count++; 202 } 203 } 204 if ($count) { 205 if ($format==1) $output .= "$v<ul>" . $str . "</ul>"; 206 else if ($format==2) { 207 $output .= '<tr><td colspan=2 class="nav">' . $v . ':</font></td></tr>'; 208 $output .= $str; 209 } 210 211 } 212 } 213 return $output; 214 } 215 216 /** 217 This function determines how many days are in a given month and year 218 @param $month The month 219 @param $year THe year 220 @return the number of days in the month/year combo 221 */ 222 function daysInMonth($month,$year) { 223 $dim = array(31,28,31,30,31,30,31,31,30,31,30,31); 224 $value = $dim[$month-1]; 225 if ( $month == 2 && $year %4 == 0 && $year % 100 != 0 ) { 226 $value++; 227 } 228 return $value; 229 } 230 231 /** 232 Creates an array with the days of the week in them. This will account for weeks 233 that wrap to the next month, or carry over from the previous month 234 @param $day The day 235 @param $month The month 236 @param $year the year 237 @return seven element array of arrays (day, month, year) 238 */ 239 function getWeekDaysList($day, $month, $year) { 240 $dayList = array(); 241 $count = 1; 242 // Figure out what day of the week this date is on 243 $weekDay = date('w',mktime(0,0,0,$month,$day,$year)); 244 // Set the date so that it is the given start of the week (any day) 245 if ($weekDay != $this->startWeekDay) { 246 if ($weekDay < $this->startWeekDay) { 247 $dec = 7 - $this->startWeekDay + $weekDay; 248 list($day, $month, $year) = $this->getPreviousDay($day, $month, $year, $dec); 249 } else if ($weekDay > $this->startWeekDay) { 250 $dec = $weekDay - $this->startWeekDay; 251 list($day, $month, $year) = $this->getPreviousDay($day, $month, $year, $dec); 252 } 253 } 254 // Save the start date 255 $dayList[] = array($day, $month, $year); 256 // Add days to the list until we reach 7 days (one week) 257 while ($count < 7) { 258 list($day, $month, $year) = $this->getNextDay($day, $month, $year, 1); 259 $dayList[] = array($day, $month, $year); 260 $count++; 261 } 262 return $dayList; 263 } 264 265 /** 266 This function gets the date of a day that is a given 267 number of days in the future. 268 @param $day The day 269 @param $month The month 270 @param $year The year 271 @param $num the number of days to forward 272 @return array of ($day, $month, $year) that is the new date 273 */ 274 function getNextDay($day, $month, $year, $num=1) { 275 $maxdays = $this->daysInMonth($month,$year); 276 while ($num > 0) { 277 if ($day == $maxdays) { 278 // We need to roll over to a new month 279 if ($month < 12) $month++; 280 else { 281 $year++; 282 $month=1; 283 } 284 $day = 1; 285 } else $day++; 286 $num--; 287 } 288 return array($day, $month, $year); 289 } 290 291 /** 292 This function gets the date of a day that is a given 293 number of days ago. 294 @param $day The day 295 @param $month The month 296 @param $year The year 297 @param $num the number of days to go back 298 @return array of ($day, $month, $year) that is the new date 299 */ 300 function getPreviousDay($day, $month, $year, $num) { 301 // Loop until we have gone $num days backwards 302 while ($num > 0) { 303 if ($day == 1) { 304 // we need to roll back to the previous month 305 if ($month > 1) { 306 $month--; 307 } else { 308 $year--; 309 $month=12; 310 } 311 // Set days to the max days of the new month 312 $day = $this->daysInMonth($month,$year); 313 } else $day--; 314 $num--; 315 } 316 return array($day, $month, $year); 317 } 318 319 /** 320 Gets the next week (Sunday) for a given date 321 */ 322 function getNextWeek($day, $month, $year) { 323 $weekList = $this->getWeekDaysList($day,$month,$year); 324 $day = $weekList[6][0]; 325 $month = $weekList[6][1]; 326 $year = $weekList[6][2]; 327 return ($this->getNextDay($day,$month,$year,1)); 328 } 329 330 /** 331 Gets the previous week (Sunday) for a given date 332 */ 333 function getPreviousWeek($day, $month, $year) { 334 $weekList = $this->getWeekDaysList($day,$month,$year); 335 $day = $weekList[0][0]; 336 $month = $weekList[0][1]; 337 $year = $weekList[0][2]; 338 return ($this->getPreviousDay($day,$month,$year,7)); 339 } 340 341 /** 342 Gets the next month 343 */ 344 function getNextMonth($day, $month, $year) { 345 if ($month < 12) $month++; 346 else { 347 $month=1; 348 $year++; 349 } 350 return array($day, $month, $year); 351 } 352 353 /** 354 Gets the previous month 355 */ 356 function getPreviousMonth($day, $month, $year) { 357 if ($month > 1) $month--; 358 else { 359 $month=12; 360 $year--; 361 } 362 return array($day, $month, $year); 363 } 364} 365?> 366