#0 | implode
/var/www/studyforyou.info/apps/modules/frontend/controllers/CourseProfileController.php (116) <?php
namespace Modules\Frontend\Controllers;
class CourseProfileController extends ControllerBase
{
/**
* @param $course
*/
public function indexAction($course)
{
$lang_code = $this->language;
$languagesModel = new \Languages();
$lang = $languagesModel::findFirst("code='$lang_code'")->id;
//second param is filters you want use
$translates = $this->setViewTranslates($lang, ['menu', 'header', 'filters', 'footer', 'content', 'tabs']);
$this->assets->addCss('frontend/dist/css/course-profile.css' . $this->disableCache());
$this->assets->addJs('frontend/js/course-profile.js' . $this->disableCache());
$this->assets->addJs('frontend/js/rating.js' . $this->disableCache());
$courseRecord = $this->modelsManager->createBuilder()
->addFrom('Courses', 'c')
->columns("
c.id id,
ctr.title title,
c.country_id country_id,
c.city_id city_id,
cot.title as country,
cit.title as city,
ctr.type_id type_id,
c.university_id university_id,
ctr.category_id category_id,
ctr.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,
utr.title as university,
ctr.url_suffix url_suffix,
ctr.description description,
ctr.seo_title seo_title,
ctr.seo_description seo_description,
ctr.seo_keywords seo_keywords,
ctr.url_cannonical url_cannonical
")
->leftjoin('UniversityTranslates', 'utr.university_id = c.university_id AND utr.language_id = :language_id:', 'utr')
->leftjoin('CourseTranslates', 'ctr.course_id = c.id AND ctr.language_id = :language_id:', 'ctr')
->leftjoin('CountriesTranslates', 'cot.country_id = c.country_id AND cot.language_id = :language_id:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = c.city_id AND cit.language_id = :language_id:', 'cit')
->andWhere("ctr.url_suffix = :course:")
->getQuery()
->getSingleResult(['course' => $course, 'language_id' => $lang]);
if ($courseRecord) {
$courseRecord = $courseRecord->toArray();
$courseRecord['registration_end'] = date("d.m.Y", strtotime($courseRecord['registration_end']));
$courseRecord['study_start'] = date('d.m.Y', strtotime($courseRecord['study_start']));
$course_langs = \CourseLanguages::find(["course_id = :course_id:", "bind" => ["course_id" => $courseRecord['id']]])->toArray();
$langs_ids = $this->getField($course_langs, 'language_id');
if (count($langs_ids))
$langs = \FilterTranslates::find(["id IN ({langs_ids:array}) AND language_id = :language_id:", "bind" => ["langs_ids" => $langs_ids, "language_id" => $lang]])->toArray();
$course_profiles = \CourseProfile::find(["course_id = :course_id:", "bind" => ["course_id" => $courseRecord['id']]])->toArray();
$profiles_ids = $this->getField($course_profiles, 'profile_id');
if (count($profiles_ids))
$profiles = \FilterTranslates::find(["id IN ({profiles_ids:array}) AND language_id = :language_id:", "bind" => ["profiles_ids" => $profiles_ids, "language_id" => $lang]])->toArray();
$type = \FilterTranslates::findFirst(["id = :type_id: AND language_id = :language_id:", "bind" => ["type_id" => $courseRecord['type_id'], "language_id" => $lang]])->title;
$duration = \FilterTranslates::findFirst(["id = :duration_id: AND language_id = :language_id:", "bind" => ["duration_id" => $courseRecord['duration'], "language_id" => $lang]])->title;
$univer_record = \Universities::findFirstById($courseRecord['university_id']);
if ($univer_record) {
if ($univer_record->university_logo_id) {
$univerLogo = \Medias::findFirst($univer_record->university_logo_id)->large_url;
$logoAlt = \MediaSeo::findFirst(["media_id = :media_id: AND language_id = :language_id:", "bind" => ["media_id" => $univer_record->university_logo_id, "language_id" => $lang]])->alt;
}
if ($univer_record->banner_image_id)
$univerBanner = \Medias::findFirst($univer_record->banner_image_id)->original_url;
$univerUrlSuffix = \UniversityTranslates::findFirst(["university_id = ?0 AND language_id = ?1", "bind" => [0 => $univer_record->id, 1 => $lang]])->url_suffix;
$country = \CountriesTranslates::findFirst(["country_id = :country_id: AND language_id = :language_id:", "bind" => ["country_id" => $univer_record->country_id, "language_id" => $lang]])->title;
$city = \CitiesTranslates::findFirst(["city_id = :city_id: AND language_id = :language_id:", "bind" => ["city_id" => $univer_record->city_id, "language_id" => $lang]])->title;
}
$courseMedia = $this->modelsManager->createBuilder()
->addFrom('CourseMedias', 'cm')
->columns("m.id id, m.large_url as url, ms.alt alt, ms.title title")
->leftjoin('Medias', 'm.id = cm.media_id', 'm')
->leftjoin('MediaSeo', 'ms.media_id = cm.media_id AND ms.language_id = :lang_id:', 'ms')
->where('cm.course_id = :course_id:')
->getQuery()
->execute(['course_id' => $courseRecord['id'], 'lang_id' => $lang])->toArray();
$courseArticles = \CourseArticles::findByCourseId($courseRecord['id'])->toArray();
$course_articles_ids = $this->getField($courseArticles, 'article_id');
if ($course_articles_ids && count($course_articles_ids)) {
$course_articles = \ArticleTranslates::find(["article_id IN ({course_articles_ids:array}) AND language_id = :language_id:", "bind" => ["course_articles_ids" => $course_articles_ids, "language_id" => $lang]]);
}
$courseForCompare = $courseRecord;
$courseForCompare['langs'] = $langs_ids;
$courseRecord['univer_country'] = $country;
$courseRecord['univer_city'] = $city;
$courseRecord['univer_url'] = $univerUrlSuffix;
$courseRecord['langs'] = implode(', ', $this->getField($langs, 'title'));
$courseRecord['type'] = $type;
$courseRecord['duration'] = $duration;
$courseRecord['profile'] = implode(', ', $this->getField($profiles, 'title'));
$courseRecord['univer_logo'] = $univerLogo ?? false;
$courseRecord['univer_banner'] = $univerBanner ?? false;
$courseRecord['univer_logo_alt'] = $logoAlt;
}
$this->view->setVars([
'course' => $courseRecord,
'popularUnivers' => $this->getPopularUniversities($lang),
'relatedUnivers' => $this->getRelatedUniversities($courseRecord['country_id'], $courseRecord['city_id'], $courseRecord['university_id'], $lang),
'currentCity' => $courseRecord['city'],
'courseMedia' => $courseMedia,
'articles' => $course_articles,
'meta' => [
'title' => $courseRecord['seo_title'],
'description' => $courseRecord['seo_description'],
'keywords' => $courseRecord['seo_keywords'],
'cannonical' => $courseRecord['url_cannonical'],
],
'relatedCourses' => $this->getRelatedCourses($courseForCompare, $lang),
]);
//language titles
$course_translate = \CourseTranslates::findByCourseId($courseRecord['id']);
$this->changeLangViewParams($course_translate);
$breadcrumbs = $this->getBaseBreadCrumbs($translates, $lang_code);
$breadcrumbs[] = [
'pos' => '2',
'title' => $translates['t_content']->accomodation_catalog,
'url' => '/' . $lang_code . '/accomodation'
];
$breadcrumbs[] = [
'pos' => '3',
'title' => $courseRecord['title'],
'url' => '/' . $lang_code . '/courses/' . $courseRecord['url_suffix'],
'last' => true
];
$this->view->breadcrumbs = $breadcrumbs;
}
/**
* @param $course
* @param $lang
* @param int $limit
* @return array
*/
public function getRelatedCourses($course, $lang, $limit = 5)
{
$course_langs = \CourseLanguages::find(["language_id IN ({langs_ids:array})", "bind" => ["langs_ids" => $course['langs']]])->toArray();
$courses_ids = $this->getField($course_langs, 'course_id');
$bind = [
'first' => [
"lang" => $lang,
"exclude_id" => $course['id'],
"title" => $course['title'],
"courses_ids" => $courses_ids,
],
'second' => [
"lang" => $lang,
"exclude_id" => $course['id'],
"title" => $course['title'],
],
'third' => [
"lang" => $lang,
"exclude_id" => $course['id'],
"courses_ids" => $courses_ids,
]
];
$andWhere = [
'first' => ["ctr.title = :title:", "c.id IN ({courses_ids:array})"],
'second' => ["ctr.title = :title:"],
'third' => ["c.id IN ({courses_ids:array})"]
];
/* get courses with with equal title and languages */
$relatedCourses['first'] = $this->getCoursesByQuery($bind['first'], $andWhere['first'], $limit);
/* get courses with equal title and different languages */
if (!isset($relatedCourses['first']) || count($relatedCourses['first']) < $limit) {
$secondLimit = isset($relatedCourses['first']) ? ($limit - count($relatedCourses['first'])) : $limit;
$excludeIds = isset($relatedCourses['first']) ? $this->getField($relatedCourses['first'], 'id') : false;
if ($excludeIds) {
$bind['second']['exclude_items'] = $excludeIds;
$andWhere['second'][] = "c.id NOT IN ({exclude_items:array})";
}
$relatedCourses['second'] = $this->getCoursesByQuery($bind['second'], $andWhere['second'], $secondLimit);
}
/* get courses with equal languages */
if (!isset($relatedCourses['second']) || count($relatedCourses['second']) < $secondLimit) {
$thirdLimit = (isset($relatedCourses['second'])) ? ($secondLimit - count($relatedCourses['second'])) : $secondLimit;
if ($excludeIds) {
if (isset($relatedCourses['second']) && count($relatedCourses['second'])) {
$excludeIds = array_merge($excludeIds, $this->getField($relatedCourses['second'], 'id'));
}
$bind['third']['exclude_items'] = $excludeIds;
$andWhere['third'][] = "c.id NOT IN ({exclude_items:array})";
} else {
if (isset($relatedCourses['second']) && count($relatedCourses['second'])) {
$excludeIds = $this->getField($relatedCourses['second'], 'id');
if ($excludeIds) {
$bind['third']['exclude_items'] = $excludeIds;
$andWhere['third'][] = "c.id NOT IN ({exclude_items:array})";
}
}
}
$relatedCourses['third'] = $this->getCoursesByQuery($bind['third'], $andWhere['third'], $thirdLimit);
}
$relatedCourses = array_merge($relatedCourses['first'], $relatedCourses['second'], $relatedCourses['third']);
return $relatedCourses;
}
public function getCoursesByQuery($bind, $andWhere, $limit)
{
$relatedCourses = $this->modelsManager->createBuilder()
->addFrom('Courses', 'c')
->columns("
c.id id, c.cost cost, ctr.title title, ctr.url_suffix as url, utr.title as university, utr.url_suffix as univer_url,
cot.title as country, cit.title as city, m.large_url as image, ms.alt as image_alt, ms.title as image_title, ctr.duration duration")
->leftjoin('CourseTranslates', 'ctr.course_id = c.id AND ctr.language_id = :lang:', 'ctr')
->leftjoin('UniversityTranslates', 'utr.university_id = c.university_id AND utr.language_id = :lang:', 'utr')
->leftjoin('CountriesTranslates', 'cot.country_id = c.country_id AND cot.language_id = :lang:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = c.city_id AND cit.language_id = :lang:', 'cit')
->leftjoin('Universities', 'u.id = c.university_id', 'u')
->leftjoin('Medias', 'm.id = u.banner_image_id', 'm')
->leftjoin('MediaSeo', 'ms.media_id = m.id AND ms.language_id = :lang:', 'ms')
->where("c.id <> :exclude_id:")
->andWhere("c.is_published = 1");
foreach ($andWhere as $value) {
if (strpos($value, ':array}') === false) {
$relatedCourses = $relatedCourses->andWhere($value);
} else {
preg_match('/({(.*?):array})/', $value, $match);
$key = trim(str_replace(":array}", "", $match[1]), "{");
$relatedCourses = $relatedCourses->andWhere($value, [$key => $bind[$key]]);
unset($bind[$key]);
}
}
$relatedCourses = $relatedCourses->limit($limit)
->getQuery()
->execute($bind)
->toArray();
foreach ($relatedCourses as $key => $item) {
$course_langs = \CourseLanguages::findByCourseId($item['id'])->toArray();
$langs_ids = $this->getField($course_langs, 'language_id');
if (count($langs_ids)) {
$langs = \FilterTranslates::find([
"id IN ({langs_ids:array}) AND language_id = :lang:",
"bind" => ["langs_ids" => $langs_ids, "lang" => $bind['lang']]])->toArray();
}
$relatedCourses[$key]['study_langs'] = implode(', ', $this->getField($langs, 'title'));
}
return $relatedCourses;
}
}
|