from('news')->where("valid = '1' and dt = (select max(dt) from news where type = 'new')")->fetchAll(); $img = ''; $res = $res[0]; $i = 240; //max count of characters $str = $res['txt']; $res['txt'] = $this->shortenText($str, $i, '...'); $imgInfo = $this->getImgPath($res->id); $res['img'] = $imgInfo; return $res; } private static function addQuotes($str) { $str = "'".$str."'"; return $str; } public function getLastX($page, $count, $type) { $typeStr = News::createInClause($type); $res = dibi::select('id,title,txt,type,DATE_FORMAT(dt,"%d.%c.%Y") dt,valid,enterDt')->from('news')->where("[valid] = 1 and [type] in ".$typeStr)->orderBy('news.dt DESC')->limit($count)->offset(($page-1)*$count)->fetchAll(); $rres = array(); foreach($res as $key => $val) { $rres[] = $val; $val['img'] = $this->getImgPath($val->id); $val['txt'] = $this->shortenText($val->txt, 500, '...'); $val['widthheigher'] = $this->isImgWidthHeigher($val->img); $val['link'] = String::webalize($val->id.'-'.$val->title); } return $res; } public function getById($id) { $res = dibi::select('id,title,txt,type,DATE_FORMAT(dt,"%d.%c.%Y") dt,valid,enterDt')->from('news')->where("valid = 1 and id = %i", $id)->fetchAll(); return $res; } public function getCount($page, $count, $type) { $typeStr = News::createInClause($type); $res = dibi::select('count(id) count')->from('news')->where("valid = 1 and [type] in ".$typeStr)->orderBy('dt DESC')->fetchAll(); //echo dibi::$sql; return $res; } private static function createInClause($type) { $typeArr = explode(',', $type); $typeArr = array_map(array('News','addQuotes'), $typeArr); $typeStr = implode(',', $typeArr); $typeStr = "(".$typeStr.")"; return $typeStr; } private function getImgPath($newId) { $img = ''; $imgData = $this->getImgById($newId, 1); $imgPath = isset($imgData[0])? $imgData[0]['path']: ''; $res = $imgPath; return $res; } private function shortenText($txt, $maxChars, $addToEndStr = '...') { if (strlen($txt) > $maxChars) { $str = $txt; $i = min($maxChars, (strlen($str) - 1)); while(($str[$i] != '.') and ($str[$i] != ' ') and ($str[$i] != ',') and ($str[$i] != '?') and ($str[$i] != '!') and ($str[$i-1] != ' ') and ($i>0)) { $i--; } } else { $addToEndStr = ''; $i = strlen($txt); } return substr($txt, 0, $i).$addToEndStr; } public function getImgById($id, $limit=999) { $img = ''; $imgArr = array(); $newId = $id; $newsImgPathDir = WWW_DIR.$this->pathToEventsGallery.$newId.'/'; $i = 0; if (file_exists($newsImgPathDir) === true) { $handle = opendir($newsImgPathDir); if ($handle) { while(($file = readdir($handle)) !== false) { if (file_exists($newsImgPathDir.$file) === true && ($file != '.' && $file != '..' )) { $i++; $img = $this->pathToEventsGallery; $img .= $newId.'/'.$file; $img = substr($img, 1); $imgArr[($i-1)]['path'] = $img; $imgArr[($i-1)]['widthheigher'] = $this->isImgWidthHeigher($img); if ($i >= $limit) { break; } } } } } return $imgArr; } private function isImgWidthHeigher($img) { $res = false; if ($img !== '') { //Debug::dump(file_exists(WWW_DIR.'/'.$img)); $imgInfo = getimagesize(WWW_DIR.'/'.$img); $res = $imgInfo[0] >= $imgInfo[1]; } return $res; } }