#0 | implode
/var/www/studyforyou.info/apps/modules/frontend/controllers/UniversityProfileController.php (178) <?php
namespace Modules\Frontend\Controllers;
use Phalcon\Mvc\View;
class UniversityProfileController extends ControllerBase
{
public function indexAction($univer)
{
$this->assets->addCss('frontend/dist/css/university-profile.css' . $this->disableCache());
$this->assets->addJs('frontend/js/university-profile.js' . $this->disableCache());
$this->assets->addJs('frontend/js/rating.js' . $this->disableCache());
$lang_code = $this->language;
$languagesModel = new \Languages();
$lang = $languagesModel::findFirstByCode($lang_code)->id;
//second param is filters you want use
$translates = $this->setViewTranslates($lang, ['menu', 'header', 'filters', 'footer', 'content', 'tabs']);
$university = $this->modelsManager->createBuilder()
->addFrom('Universities', 'u')
->columns("
u.id id,
u.original_title original_title,
u.university_logo_id university_logo_id,
utr.title title,
u.is_apply_free is_apply_free,
utr.status status,
u.cost_per_year as cost,
u.is_hostel_available as has_hostel,
u.country_id country_id,
u.city_id city_id,
utr.url_suffix url_suffix,
u.university_url as url,
utr.ratings ratings,
utr.internationality internationality,
utr.for_students for_students,
utr.hostel hostel,
u.founded_at founded_at,
u.students_count students_count,
m.original_url as image_url,
utr.seo_title seo_title,
utr.seo_description seo_description,
utr.seo_keywords seo_keywords,
utr.url_cannonical url_cannonical,
utr.presentation_url presentation_url,
utr.courses_description courses_description,
utr.speciality_description speciality_description
")
->leftjoin('UniversityTranslates', 'utr.university_id = u.id AND utr.language_id = :language:', 'utr')
->leftjoin('Medias', 'm.id = u.banner_image_id', 'm')
->where("utr.url_suffix = :url_suffix:")
->andWhere("u.is_published = 1")
->getQuery()
->getSingleResult(['url_suffix' => $univer, 'language' => $lang]);
$univer_study_levels = \UniversityStudyLevels::findByUniversityId($university->id)->toArray();
$univer_sciences = \UniversityScinces::findByUniversityId($university->id)->toArray();
$univer_langs = \UniversityLanguages::findByUniversityId($university->id)->toArray();
$study_levels_ids = $this->getField($univer_study_levels, 'study_level_id');
$sciences_ids = $this->getField($univer_sciences, 'scince_id');
$langs_ids = $this->getField($univer_langs, 'language_id');
if ($study_levels_ids && count($study_levels_ids)) {
$study_levels = \FilterTranslates::find(["id IN ({study_levels_ids:array}) AND language_id = :language_id:", "bind" => ["study_levels_ids" => $study_levels_ids, "language_id" => $lang]])->toArray();
}
if ($sciences_ids && count($sciences_ids)) {
$sciences = \FilterTranslates::find(["id IN ({sciences_ids:array}) AND language_id = :language_id:", "bind" => ["sciences_ids" => $sciences_ids, "language_id" => $lang]])->toArray();
}
if ($langs_ids && count($langs_ids)) {
$study_langs = \FilterTranslates::find([
"id IN ({langs_ids:array}) AND language_id = :lang:",
"bind" => ["langs_ids" => $langs_ids, 'lang' => $lang]])->toArray();
}
$country = \CountriesTranslates::findFirst(["country_id = :country_id: AND language_id = :language_id:", "bind" => ["country_id" => $university->country_id, "language_id" => $lang]])->title;
$city = \CitiesTranslates::findFirst(["city_id = :city_id: AND language_id = :language_id:", "bind" => ["city_id" => $university->city_id, "language_id" => $lang]])->title;
$status = \FilterTranslates::findFirst(["id = :status: AND language_id = :language_id:", "bind" => ["status" => $university->status, "language_id" => $lang]])->title;
$universityMedia = [new \stdClass()];
$getUniversityMedia = \UniversityMedias::find(["university_id = :university_id: AND type = 'university'", "bind" => ["university_id" => $university->id]]);
if (count($getUniversityMedia) > 0) {
$universityMedia = $getUniversityMedia;
}
$universityHostelMedia = [new \stdClass()];
$getUniversityHostelMedia = \UniversityMedias::find(["university_id = :university_id: AND type = 'hostel'", "bind" => ["university_id" => $university->id]]);
if (count($getUniversityHostelMedia) > 0) {
$universityHostelMedia = $getUniversityHostelMedia;
}
$univerLogo = \Medias::findFirst($university->university_logo_id)->large_url;
$logoAlt = \MediaSeo::findFirst(["media_id = :media_id: AND language_id = :language_id:", "bind" => ["media_id" => $university->university_logo_id, "language_id" => $lang]])->alt;
$coursesList = $this->modelsManager->createBuilder()
->addFrom('Courses', 'c')
->columns("
c.id id, c.country_id country_id, c.city_id city_id, ct.type_id type_id, c.university_id university_id, ct.category_id category_id,
ct.duration duration, c.cost cost, c.registration_fee registration_fee, c.registration_end registration_end, c.study_start study_start,
c.study_hours study_hours, ct.title title, ct.url_suffix as url")
->leftjoin('CourseTranslates', 'c.id = ct.course_id AND ct.language_id = :lang_id:', 'ct')
->where("c.university_id = :university_id:")
->andWhere("c.is_published = 1")
->getQuery()
->execute(['university_id' => $university->id, 'lang_id' => $lang]);
foreach ($coursesList as $course) {
$courseCountry = \CountriesTranslates::findFirst(["country_id = :country_id: AND language_id = :language_id:", "bind" => ["country_id" => $course->country_id, "language_id" => $lang]])->title;
$courseCity = \CitiesTranslates::findFirst(["city_id = :city_id: AND language_id = :language_id:", "bind" => ["city_id" => $course->city_id, "language_id" => $lang]])->title;
$courseType = \FilterTranslates::findFirst(["id = :type_id: AND language_id = :language_id:", "bind" => ["type_id" => $course->type_id, "language_id" => $lang]])->title;
$courseCategory = \FilterTranslates::findFirst(["id = :category_id: AND language_id = :language_id:", "bind" => ["category_id" => $course->category_id, "language_id" => $lang]])->title;
$courseDuration = \FilterTranslates::findFirst(["id = :duration_id: AND language_id = :language_id:", "bind" => ["duration_id" => $course->duration, "language_id" => $lang]])->title;
$langs = \CourseLanguages::findByCourseId($course->id)->toArray();
$courseLangsIds = $this->getField($langs, 'language_id');
if (count($courseLangsIds))
$courseLangs = \FilterTranslates::find([
"id IN ({langs_ids:array}) AND language_id = :lang:",
"bind" => ["langs_ids" => $courseLangsIds, "lang" => $lang]
])->toArray();
$coursesInfo[$course->id] = [
'contry' => $courseCountry,
'city' => $courseCity,
'type' => $courseType,
'category' => $courseCategory,
'duration' => $courseDuration,
'langs' => $courseLangs ? implode(', ', $this->getField($courseLangs, 'title')) : '',
];
}
$univerAccomodation = \AccomodationUniversities::findByUniversityId($university->id)->toArray();
if (count($univerAccomodation)) {
$univer_accomodation_ids = $this->getField($univerAccomodation, 'accomodation_id');
$accomodations = $this->modelsManager->createBuilder()
->addFrom('Accomodation', 'a')
->columns("
a.id id, a.cost cost, at.title title, at.url_suffix as url, m.large_url as image, cot.title as country, cit.title as city,
ft.title as status, ms.title as image_title, ms.alt as image_alt")
->leftjoin('AccomodationTranslates', 'a.id = at.accommodation_id AND at.language_id = :lang_id:', 'at')
->leftjoin('Medias', 'm.id = a.image_id', 'm')
->leftjoin('MediaSeo', 'ms.media_id = m.id AND ms.language_id = :lang_id:', 'ms')
->leftjoin('CountriesTranslates', 'cot.country_id = a.country_id AND cot.language_id = :lang_id:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = a.city_id AND cit.language_id = :lang_id:', 'cit')
->leftjoin('FilterTranslates', 'ft.id = at.status_id AND ft.language_id = :lang_id:', 'ft')
->where("a.id IN ({acc_ids:array})", ['acc_ids' => $univer_accomodation_ids])
->andWhere("a.is_published = 1")
->getQuery()
->execute(['lang_id' => $lang]);
}
$univerArticles = \UniversityArticles::findByUniversityId($university->id)->toArray();
$univer_articles_ids = $this->getField($univerArticles, 'article_id');
if ($univer_articles_ids && count($univer_articles_ids)) {
$univer_articles = $this->modelsManager->createBuilder()
->addFrom('Articles', 'a')
->columns("
a.id id, at.url_suffix url_suffix, at.title title")
->leftjoin('ArticleTranslates', 'a.id = at.article_id AND at.language_id = :lang_id:', 'at')
->where("at.article_id IN ({univer_articles_ids:array})", ['univer_articles_ids' => $univer_articles_ids])
->andWhere("a.is_published = 1")
->getQuery()
->execute(['lang_id' => $lang]);
}
$univerInfo = [
'study_levels' => implode(', ', $this->getField($study_levels, 'title')),
'sciences' => implode(', ', $this->getField($sciences, 'title')),
'langs' => implode(', ', $this->getField($study_langs, 'title')),
'country' => $country,
'city' => $city,
'status' => $status,
'logo' => $univerLogo,
'logoAlt' => $logoAlt,
'rating' => $this->getUniverRating($university->id),
];
$this->view->setVars([
'univer' => $university,
'univerInfo' => $univerInfo,
'courses' => $coursesList,
'coursesInfo' => $coursesInfo,
'specialities' => \Specialities::find(["university_id = :univer_id: AND is_published = 1", "bind" => ["univer_id" => $university->id]]),
'univerMedias' => $universityMedia,
'hostelMedias' => $universityHostelMedia,
'meta' => [
'title' => $university->seo_title,
'description' => $university->seo_description,
'keywords' => $university->seo_keywords,
'cannonical' => $university->url_cannonical,
],
'study_levels' => \FilterTranslates::findManyByGroup('study_level', $lang),
'study_langs' => \FilterTranslates::findManyByGroup('study_lang', $lang),
'study_types' => \FilterTranslates::findManyByGroup('study_form', $lang),
'sorting' => \FilterTranslates::findManyByGroup('sorting', $lang),
'articles' => $univer_articles,
'popularUnivers' => $this->getPopularUniversities($lang),
'relatedUnivers' => $this->getRelatedUniversities($university->country_id, $university->city_id, $university->id, $lang),
'currentCity' => $city,
'accomodations' => $accomodations ? : false,
]);
//language titles
$university_translate = \UniversityTranslates::findByUniversityId($university->id);
$this->changeLangViewParams($university_translate);
$breadcrumbs = $this->getBaseBreadCrumbs($translates, $lang_code);
$breadcrumbs[] = [
'pos' => '2',
'title' => $translates['t_content']->university_catalog,
'url' => '/' . $lang_code . '/universities'
];
$breadcrumbs[] = [
'pos' => '3',
'title' => $university->title,
'url' => '/' . $lang_code . '/universities/' . $university->url_suffix,
'last' => true
];
$this->view->breadcrumbs = $breadcrumbs;
}
public function getSpecialitiesAction()
{
$univer = $this->request->get('univer_id');
//$lang = $this->request->get('lang');
$lang = $this->language;
$langId = \Languages::findFirstByCode($lang)->id;
$filters = $this->request->get('filters');
$sort = $this->request->get('sort');
$result = \SpecialityTranslates::getSpecialitiesByUniver($univer, $sort, $langId, $filters);
//second param is filters you want use
$this->setViewTranslates($langId, ['content']);
$this->view->setRenderLevel(
View::LEVEL_ACTION_VIEW
);
$this->view->partial('university-profile/specialities-list', $result);
}
public function getRatingAction()
{
$this->view->disable();
$univer_id = $this->request->get('id');
$rating = $this->getUniverRating($univer_id);
$this->response->setJsonContent(
$rating,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK
);
$this->response->send();
exit;
}
public function setRatingAction()
{
$this->view->disable();
$univer_id = $this->request->get('id');
$rating = $this->request->get('rating');
$rater = \UniversityRatings::findByRaterIp($_SERVER['REMOTE_ADDR']);
$univer = \UniversityRatings::findByUniversityId($univer_id);
if (!count($rater) || !count($univer)) {
$univerRating = new \UniversityRatings();
$univerRating->university_id = $univer_id;
$univerRating->rater_ip = $_SERVER['REMOTE_ADDR'];
$univerRating->rating = $rating;
$univerRating->save();
} else {
$univerRating = false;
}
$this->response->setJsonContent(
$univerRating,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK
);
$this->response->send();
exit;
}
public function getUniverRating($id)
{
$result = \UniversityRatings::sum(
[
'column' => 'rating',
'conditions' => "university_id = " . $id,
]
);
$count = \UniversityRatings::count('university_id = ' . $id);
if ($result && $count) {
$rating['star'] = round($result / $count);
$rating['value'] = round($result / $count, 2);
$rating['count'] = $count;
} else {
$rating['star'] = 5;
$rating['value'] = 5;
$rating['count'] = $count;
}
return $rating;
}
public function checkRatingAction()
{
$this->view->disable();
$univer_id = $this->request->get('id');
$rater = \UniversityRatings::findByRaterIp($_SERVER['REMOTE_ADDR']);
$univer = \UniversityRatings::findByUniversityId($univer_id);
if (!count($rater) || !count($univer)) {
$result = false;
} else {
$result = true;
}
$this->response->setJsonContent(
$result,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK
);
$this->response->send();
exit;
}
}
|