function autoFeature() { // look up FeaturedTemplate.t to see if more than interval $q = $this->db->queryC("SELECT id FROM FeaturedTemplate WHERE t<DATE_SUB(NOW(),INTERVAL " . AUTOFEATURE_INTERVAL . " HOUR);"); if ($q === false) { //return false; } // get admin user list require_once PATH_CORE . '/classes/user.class.php'; $userTable = new UserTable($this->db); $adminList = $userTable->listAdmins(); if ($adminList == '') { $this->db->log("AF Error: No admins listed"); return false; } $cnt = 0; $tempInterval = AUTOFEATURE_INTERVAL * 2; while ($cnt < 6) { // find 2 stories (posted by admin, in last n hours, not yet in log) - sort preference to images $qStr = "SELECT Content.siteContentId,title,ContentImages.url AS imageUrl FROM Content LEFT JOIN ContentImages ON Content.siteContentId=ContentImages.siteContentId LEFT JOIN Log ON Content.siteContentId=Log.itemid AND Log.action='storyFeatured' WHERE FIND_IN_SET(Content.userid,'{$adminList}') AND Content.date>DATE_SUB(NOW(),INTERVAL {$tempInterval} HOUR) ORDER BY (Log.id IS NULL) DESC,(imageUrl<>'') DESC, Content.siteContentId DESC LIMIT 2;"; $q1 = $this->db->queryC($qStr); if ($this->db->cnt == 2) { break; } $cnt += 1; $tempInterval *= 2; } if ($this->db->cnt == 0) { return false; } // not enough stories $numPrimaryStories = $this->db->cnt; $primaryStories = array(); $excludeStoryList = ''; // load array with results while ($d = $this->db->readQ($q1)) { $primaryStories[] = $d; $excludeStoryList .= $d->siteContentId . ','; } $excludeStoryList = trim($excludeStoryList, ','); // find secondary stories $cnt = 0; $tempInterval = AUTOFEATURE_INTERVAL * 4; while ($cnt < 3) { // 4 stories in last n hours e.g. 12/24 excluding above selections, posted by admins - no worries if featured before $qStr = "SELECT Content.siteContentId,title,ContentImages.url AS imageUrl FROM Content LEFT JOIN ContentImages ON Content.siteContentId=ContentImages.siteContentId WHERE NOT FIND_IN_SET(Content.siteContentId,'{$excludeStoryList}') AND FIND_IN_SET(Content.userid,'{$adminList}') AND Content.date>DATE_SUB(NOW(),INTERVAL {$tempInterval} HOUR) ORDER BY Content.siteContentId DESC LIMIT 4;"; $q2 = $this->db->queryC($qStr); if ($this->db->cnt == 4) { break; } $cnt += 1; $tempInterval *= 2; } $numSecondaryStories = $this->db->cnt; $secondaryStories = array(); // load array with results while ($d = $this->db->readQ($q2)) { $secondaryStories[] = $d; } $autoLog = PATH_SERVER_LOGS . 'auto.log'; // log changes to auto.log $this->db->log('Time: ' . date('H:i') . ' on ' . date('m-d'), $autoLog); $this->db->log('Template: ' . $template, $autoLog); // from story arrays, determine best template e.g. images // no images $story_1_id = 0; $story_2_id = 0; $story_3_id = 0; $story_4_id = 0; $story_5_id = 0; $story_6_id = 0; if ($primaryStories[0]->imageUrl == '') { // first always likeliest to have image due to sort $template = 'template_1'; $story_1_id = $primaryStories[0]->siteContentId; $this->db->log($primaryStories[0], $autoLog); } else { if ($primaryStories[0]->imageUrl != '' and $primaryStories[1]->imageUrl != '') { // two images // less than two secondary stories $x = rand(0, 1); if ($x == 0) { $template = 'template_3'; } else { $template = 'template_4'; } // two images version 2 $story_1_id = $primaryStories[0]->siteContentId; $story_4_id = $primaryStories[1]->siteContentId; $this->db->log($primaryStories[0], $autoLog); $this->db->log($primaryStories[1], $autoLog); /*if ($numSecondaryStories<4) { } else { // 4 secondary stories $template='template_5'; $story_1_id=$primaryStories[0]->siteContentId; $story_4_id=$primaryStories[1]->siteContentId; $story_2_id=$secondaryStories[0]->siteContentId; $story_3_id=$secondaryStories[1]->siteContentId; $story_5_id=$secondaryStories[2]->siteContentId; $story_6_id=$secondaryStories[3]->siteContentId; $this->db->log($primaryStories[0],$autoLog); $this->db->log($primaryStories[1],$autoLog); $this->db->log($secondaryStories,$autoLog); } */ } else { // one image if ($primaryStories[0]->imageUrl<>'') - first always likeliest to have image due to sort if ($numSecondaryStories >= 2) { $template = 'template_2'; // one image, two story links $story_1_id = $primaryStories[0]->siteContentId; $story_2_id = $secondaryStories[0]->siteContentId; $story_3_id = $secondaryStories[1]->siteContentId; $this->db->log($primaryStories[0], $autoLog); $this->db->log($secondaryStories[0], $autoLog); $this->db->log($secondaryStories[1], $autoLog); } else { $template = 'template_1'; // less than two secondaryStories $story_1_id = $primaryStories[0]->siteContentId; $this->db->log($primaryStories[0], $autoLog); } } } // add top two to the log as having been featured require_once PATH_CORE . '/classes/log.class.php'; $logObj = new log($this->db); for ($i = 1; $i <= 6; $i++) { $str = '$story_' . $i . '_id'; eval("\$storyid = \"{$str}\";"); if ($storyid > 0) { // only log new ones $q = $this->db->queryC("SELECT id FROM Log WHERE action='storyFeatured' AND itemid={$storyid} LIMIT 1;"); if ($q === false) { $logItem = $logObj->serialize(0, 0, 'storyFeatured', $storyid); $logObj->add($logItem); } } } // taken from save_template.php in console $this->db->query("UPDATE Content set isFeatured = 0 WHERE isFeatured = 1"); // set new features $sql = sprintf("REPLACE INTO FeaturedTemplate SET id = 1, template = '%s', story_1_id = %s, story_2_id = %s, story_3_id = %s, story_4_id = %s, story_5_id = %s, story_6_id = %s", $template, $story_1_id, $story_2_id, $story_3_id, $story_4_id, $story_5_id, $story_6_id); $this->db->query($sql); $this->db->query("UPDATE Content set isFeatured = 1 WHERE siteContentId IN ({$story_1_id}, {$story_2_id}, {$story_3_id}, {$story_4_id}, {$story_5_id}, {$story_6_id})"); // clear out the cache of the home top stories require_once PATH_CORE . '/classes/template.class.php'; $templateObj = new template($this->db); $templateObj->resetCache('home_feature'); // update twitter feed if (USE_TWITTER) { require_once PATH_CORE . 'classes/twitter.class.php'; $twitterObj = new twitter_old($this->db); $twitterObj->postFeaturedStories(); } return true; }