#0 | array_merge
/var/www/studyforyou.info/apps/modules/frontend/controllers/SpecialityProfileController.php (307) <?php
namespace Modules\Frontend\Controllers;
class SpecialityProfileController extends ControllerBase
{
public function indexAction($speciality)
{
$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']);
$this->assets->addCss('frontend/dist/css/speciality-profile.css' . $this->disableCache());
$this->assets->addJs('frontend/js/speciality-profile.js' . $this->disableCache());
$this->assets->addJs('frontend/js/rating.js' . $this->disableCache());
$specialityRecord = $this->modelsManager->createBuilder()
->addFrom('Specialities', 's')
->columns("
s.id id,
str.title title,
s.cost_per_year cost_per_year,
s.cost_per_sem cost_per_sem,
s.country_id country_id,
s.city_id city_id,
cot.title as country,
cit.title as city,
ft.title as study_level,
str.faculty_id faculty_id,
str.science_id science_id,
s.registration_fee registration_fee,
s.registration_end registration_end,
s.study_start study_start,
s.is_apply_free is_apply_free,
utr.title as university,
ssd.duration_months as study_duration,
str.url_suffix url_suffix,
str.seo_title seo_title,
str.seo_description seo_description,
str.seo_keywords seo_keywords,
str.url_cannonical url_cannonical,
s.university_id university_id,
str.study_level_id study_level_id,
sd.job_description job_description,
sd.study_description study_description
")
->leftjoin('UniversityTranslates', 'utr.university_id = s.university_id AND utr.language_id = :lang:', 'utr')
->leftjoin('SpecialityTranslates', 'str.speciality_id = s.id AND str.language_id = :lang:', 'str')
->leftjoin('SpecialityStudyDurations', 'ssd.speciality_id = s.id', 'ssd')
->leftjoin('FilterTranslates', 'ft.id = str.study_level_id AND ft.language_id = :lang:', 'ft')
->leftjoin('CountriesTranslates', 'cot.country_id = s.country_id AND cot.language_id = :lang:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = s.city_id AND cit.language_id = :lang:', 'cit')
->leftjoin('SpecialityDescription', 's.id = sd.speciality_id AND sd.language_id = :lang:', 'sd')
->where("str.url_suffix = :speciality:")
->andWhere("s.is_published = 1")
->getQuery()
->getSingleResult(['speciality' => $speciality, 'lang' => $lang]);
if ($specialityRecord) {
$specialityRecord = $specialityRecord->toArray();
$speciality_langs = \SpecialityLanguages::findBySpecialityId($specialityRecord['id'])->toArray();
$langs_ids = $this->getField($speciality_langs, 'language_id');
if ($langs_ids) {
if (count($langs_ids) > 1) {
$where = "id IN ({langs_ids:array}) AND language_id = :lang:";
} else {
$where = "id = :langs_ids: AND language_id = :lang:";
$langs_ids = $langs_ids[0];
}
$langs = \FilterTranslates::find([
$where,
"bind" => ["langs_ids" => $langs_ids, "lang" => $lang]])->toArray();
}
$faculty = \FilterTranslates::findFirst(["id = :faculty: AND language_id = :language_id:", "bind" => ["faculty" => $specialityRecord['faculty_id'], "language_id" => $lang]])->title;
$speciality_documents = \SpecialityRequiredDocuments::findBySpecialityId($specialityRecord['id'])->toArray();
$speciality_documents_ids = $this->getField($speciality_documents, 'document_id');
if ($speciality_documents_ids && count($speciality_documents_ids)) {
$documents = \FilterTranslates::find([
"id IN ({speciality_documents_ids:array}) AND language_id = :lang:",
"bind" => ["speciality_documents_ids" => $speciality_documents_ids, "lang" => $lang]])->toArray();
foreach ($speciality_documents as $key => $val) {
foreach ($documents as $item => $value) {
if ($value['id'] == $val['document_id'])
$document[] = $value['title'];
}
}
$documents = $document;
}
$speciality_jobs = \SpecialityJobs::findBySpecialityId($specialityRecord['id'])->toArray();
$speciality_jobs_ids = $this->getField($speciality_jobs, 'job_id');
if ($speciality_jobs_ids && count($speciality_jobs_ids))
$jobs = \FilterTranslates::find([
"id IN ({speciality_jobs_ids:array}) AND language_id = :lang:",
"bind" => ["speciality_jobs_ids" => $speciality_jobs_ids, "lang" => $lang]]);
$speciality_subjects = \SpecialitySubjects::findBySpecialityId($specialityRecord['id'])->toArray();
foreach ($speciality_subjects as $speciality_subject) {
$subject_id = $speciality_subject['subject_id'];
$subjects[] = (object)[
'title' => \FilterTranslates::findFirst([
'filter_id = :subject_id: AND language_id = :language_id:',
'bind' => ['subject_id' => $subject_id, 'language_id' => $lang]
])->title,
'duration_hr' => $speciality_subject['duration_hr'],
'checkboxes' => $speciality_subject['checkboxes'],
];
}
$specialityRecord['study_start'] = date("d.m.Y", strtotime($specialityRecord['study_start']));
$specialityRecord['registration_end'] = date("d.m.Y", strtotime($specialityRecord['registration_end']));
$univer_record = \Universities::findFirstById($specialityRecord['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;
}
$speciality_dependents = \SpecialityDependend::findBySpecialityId($specialityRecord['id'])->toArray();
$speciality_dependents_ids = $this->getField($speciality_dependents, 'speciality_depend_id');
if ($speciality_dependents_ids && count($speciality_dependents_ids)) {
$dependents = \FilterTranslates::find([
"id IN ({speciality_dependents_ids:array}) AND language_id = :lang:",
"bind" => ["speciality_dependents_ids" => $speciality_dependents_ids, "lang" => $lang]]);
}
$specialArticles = \SpecialtyArticles::findBySpecialtyId($specialityRecord['id'])->toArray();
$special_articles_ids = $this->getField($specialArticles, 'article_id');
if ($special_articles_ids && count($special_articles_ids)) {
$special_articles = \ArticleTranslates::find(["article_id IN ({special_articles_ids:array}) AND language_id = :language_id:", "bind" => ["special_articles_ids" => $special_articles_ids, "language_id" => $lang]]);
}
$specialityRecord['langs'] = implode(', ', $this->getField($langs, 'title'));
$specialityRecord['faculty'] = $faculty;
$specialityRecord['required_docs'] = $documents;
$specialityRecord['jobs'] = $jobs;
$specialityRecord['subjects'] = $subjects;
$specialityRecord['univer_logo'] = $univerLogo ?? false;
$specialityRecord['univer_banner'] = $univerBanner ?? false;
$specialityRecord['univer_url'] = $univerUrlSuffix;
$specialityRecord['description'] = \SpecialityDescription::findFirst(["speciality_id = :speciality_id: AND language_id = :language_id:", "bind" => ["speciality_id" => $specialityRecord['id'], "language_id" => $lang]]);
$specialityRecord['dependents'] = $dependents;
$specialityRecord['univer_logo_alt'] = $logoAlt;
$specialityRecord['univer_country'] = $country;
$specialityRecord['univer_city'] = $city;
$specialityForCompare = $specialityRecord;
$specialityForCompare['langs'] = $langs_ids;
}
$applyFreeServiceId = \ServiceTranslates::findFirstByUrlSuffix('bezkoshtobna-podacha-polsha-vnz')->service_id;
$applyFreeService = \ServiceTranslates::findFirst(["service_id = :service_id: AND language_id = :language_id:", "bind" => ["service_id" => $applyFreeServiceId, "language_id" => $lang]]);
$this->view->setVars([
'speciality' => $specialityRecord,
'meta' => [
'title' => $specialityRecord['seo_title'],
'description' => $specialityRecord['seo_description'],
'keywords' => $specialityRecord['seo_keywords'],
'cannonical' => $specialityRecord['url_cannonical'],
],
'popularUnivers' => $this->getPopularUniversities($lang),
'relatedUnivers' => $this->getRelatedUniversities($specialityRecord['country_id'], $specialityRecord['city_id'], $specialityRecord['university_id'], $lang),
'articles' => $special_articles,
//'univerGallery' => $univerPhotos,
'currentCity' => $specialityRecord['city'],
'relatedSpecials' => $this->getRelatedSpecialities($specialityForCompare, $lang),
'apply_free_service' => $applyFreeService->url_suffix,
]);
//language titles
$speciality_translate = \SpecialityTranslates::findBySpecialityId($specialityRecord['id']);
$this->changeLangViewParams($speciality_translate);
$breadcrumbs = $this->getBaseBreadCrumbs($translates, $lang_code);
$breadcrumbs[] = [
'pos' => '2',
'title' => $translates['t_content']->specialty_catalog,
'url' => '/' . $lang_code . '/specialities'
];
$breadcrumbs[] = [
'pos' => '2',
'title' => $specialityRecord['title'],
'url' => '/' . $lang_code . '/specialities/' . $specialityRecord['url_suffix'],
'last' => true
];
$this->view->breadcrumbs = $breadcrumbs;
}
public function getRelatedSpecialities($speciality, $lang, $limit = 5)
{
if (is_array($speciality['langs'])) {
$where = "language_id IN ({langs_ids:array})";
} else {
$where = "language_id = :langs_ids:";
}
$speciality_langs = \SpecialityLanguages::find([$where, "bind" => ["langs_ids" => $speciality['langs']]])->toArray();
$specs_ids = $this->getField($speciality_langs, 'speciality_id');
if ($specs_ids) {
if (is_array($specs_ids) && count($specs_ids) > 1) {
$specs_ids_where = "s.id IN ({specs_ids:array})";
} else {
$specs_ids_where = "s.id = :specs_ids:";
$specs_ids = $specs_ids[0];
}
}
$bind = [
'first' => [
"lang" => $lang,
"exclude_id" => $speciality['id'],
"title" => $speciality['title'],
"study_level" => $speciality['study_level_id'],
"specs_ids" => $specs_ids,
],
'second' => [
"lang" => $lang,
"exclude_id" => $speciality['id'],
"title" => $speciality['title'],
"study_level" => $speciality['study_level_id']
],
'third' => [
"lang" => $lang,
"exclude_id" => $speciality['id'],
"science_id" => $speciality['science_id'],
"study_level" => $speciality['study_level_id'],
"specs_ids" => $specs_ids,
]
];
$andWhere = [
'first' => ["str.title = :title:", "str.study_level_id = :study_level:", $specs_ids_where],
'second' => ["str.title = :title:", "str.study_level_id = :study_level:"],
'third' => ["str.science_id = :science_id:", "str.study_level_id = :study_level:", $specs_ids_where]
];
/* get specialities with equal title, study_level and languages */
$relatedSpecials['first'] = $this->getSpecialitiesByQuery($bind['first'], $andWhere['first'], $limit);
/* get specialities with equal title, study_level and different languages */
if (!isset($relatedSpecials['first']) || count($relatedSpecials['first']) < $limit) {
$secondLimit = isset($relatedSpecials['first']) ? ($limit - count($relatedSpecials['first'])) : $limit;
$excludeIds = isset($relatedSpecials['first']) ? $this->getField($relatedSpecials['first'], 'id') : false;
if ($excludeIds) {
$bind['second']['exclude_items'] = $excludeIds;
$andWhere['second'][] = "s.id NOT IN ({exclude_items:array})";
}
$relatedSpecials['second'] = $this->getSpecialitiesByQuery($bind['second'], $andWhere['second'], $secondLimit);
}
/* get specialities with equal science, study_level and languages */
if (!isset($relatedSpecials['second']) || count($relatedSpecials['second']) < $secondLimit) {
$thirdLimit = (isset($relatedSpecials['second'])) ? ($secondLimit - count($relatedSpecials['second'])) : $secondLimit;
if ($excludeIds) {
if (isset($relatedSpecials['second']) && count($relatedSpecials['second'])) {
$excludeIds = array_merge($excludeIds, $this->getField($relatedSpecials['second'], 'id'));
}
$bind['third']['exclude_items'] = $excludeIds;
$andWhere['third'][] = "s.id NOT IN ({exclude_items:array})";
} else {
if (isset($relatedSpecials['second']) && count($relatedSpecials['second'])) {
$excludeIds = $this->getField($relatedSpecials['second'], 'id');
if ($excludeIds) {
$bind['third']['exclude_items'] = $excludeIds;
$andWhere['third'][] = "s.id NOT IN ({exclude_items:array})";
}
}
}
$relatedSpecials['third'] = $this->getSpecialitiesByQuery($bind['third'], $andWhere['third'], $thirdLimit);
}
$relatedSpecials = array_merge($relatedSpecials['first'], $relatedSpecials['second'], $relatedSpecials['third']);
return $relatedSpecials;
}
public function getSpecialitiesByQuery($bind, $andWhere, $limit)
{
$relatedSpecials = $this->modelsManager->createBuilder()
->addFrom('Specialities', 's')
->columns("
s.id id, s.cost_per_year cost_per_year, str.title title, str.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, ft.title as study_level")
->leftjoin('SpecialityTranslates', 'str.speciality_id = s.id AND str.language_id = :lang:', 'str')
->leftjoin('UniversityTranslates', 'utr.university_id = s.university_id AND utr.language_id = :lang:', 'utr')
->leftjoin('CountriesTranslates', 'cot.country_id = s.country_id AND cot.language_id = :lang:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = s.city_id AND cit.language_id = :lang:', 'cit')
->leftjoin('Universities', 'u.id = s.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')
->leftjoin('FilterTranslates', 'ft.id = str.study_level_id AND ft.language_id = :lang:', 'ft')
->where("s.id <> :exclude_id:")
->andWhere("s.is_published = 1");
foreach ($andWhere as $value) {
if ($value) {
if (strpos($value, ':array}') === false) {
$relatedSpecials = $relatedSpecials->andWhere($value);
} else {
preg_match('/({(.*?):array})/', $value, $match);
$key = trim(str_replace(":array}", "", $match[1]), "{");
if ($key === 'specs_ids') {
$relatedSpecials = $relatedSpecials->andWhere("s.id IN (" . implode(',', $bind[$key]) . ")");
}
if ($key === 'exclude_items') {
$relatedSpecials = $relatedSpecials->andWhere("s.id NOT IN (" . implode(',', $bind[$key]) . ")");
}
//$relatedSpecials = $relatedSpecials->andWhere($value, [$key => $bind[$key]]);
unset($bind[$key]);
}
}
}
$limit = ($limit != null) ? $limit : 5;
$relatedSpecials = $relatedSpecials->limit($limit)
->getQuery()
->execute($bind)
->toArray();
if ($relatedSpecials) {
foreach ($relatedSpecials as $key => $item) {
$speciality_langs = \SpecialityLanguages::findBySpecialityId($item['id'])->toArray();
$langs_ids = $this->getField($speciality_langs, 'language_id');
if ($langs_ids) {
if (count($langs_ids) > 1) {
$where = "id IN ({langs_ids:array}) AND language_id = :lang:";
} else {
$where = "id = :langs_ids: AND language_id = :lang:";
}
$langs = \FilterTranslates::find([
$where,
"bind" => ["langs_ids" => $langs_ids, "lang" => $bind['lang']]])->toArray();
$relatedSpecials[$key]['study_langs'] = implode(', ', $this->getField($langs, 'title'));
}
}
}
return $relatedSpecials;
}
}
|
#1 | Modules\Frontend\Controllers\SpecialityProfileController->getRelatedSpecialities
/var/www/studyforyou.info/apps/modules/frontend/controllers/SpecialityProfileController.php (193) <?php
namespace Modules\Frontend\Controllers;
class SpecialityProfileController extends ControllerBase
{
public function indexAction($speciality)
{
$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']);
$this->assets->addCss('frontend/dist/css/speciality-profile.css' . $this->disableCache());
$this->assets->addJs('frontend/js/speciality-profile.js' . $this->disableCache());
$this->assets->addJs('frontend/js/rating.js' . $this->disableCache());
$specialityRecord = $this->modelsManager->createBuilder()
->addFrom('Specialities', 's')
->columns("
s.id id,
str.title title,
s.cost_per_year cost_per_year,
s.cost_per_sem cost_per_sem,
s.country_id country_id,
s.city_id city_id,
cot.title as country,
cit.title as city,
ft.title as study_level,
str.faculty_id faculty_id,
str.science_id science_id,
s.registration_fee registration_fee,
s.registration_end registration_end,
s.study_start study_start,
s.is_apply_free is_apply_free,
utr.title as university,
ssd.duration_months as study_duration,
str.url_suffix url_suffix,
str.seo_title seo_title,
str.seo_description seo_description,
str.seo_keywords seo_keywords,
str.url_cannonical url_cannonical,
s.university_id university_id,
str.study_level_id study_level_id,
sd.job_description job_description,
sd.study_description study_description
")
->leftjoin('UniversityTranslates', 'utr.university_id = s.university_id AND utr.language_id = :lang:', 'utr')
->leftjoin('SpecialityTranslates', 'str.speciality_id = s.id AND str.language_id = :lang:', 'str')
->leftjoin('SpecialityStudyDurations', 'ssd.speciality_id = s.id', 'ssd')
->leftjoin('FilterTranslates', 'ft.id = str.study_level_id AND ft.language_id = :lang:', 'ft')
->leftjoin('CountriesTranslates', 'cot.country_id = s.country_id AND cot.language_id = :lang:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = s.city_id AND cit.language_id = :lang:', 'cit')
->leftjoin('SpecialityDescription', 's.id = sd.speciality_id AND sd.language_id = :lang:', 'sd')
->where("str.url_suffix = :speciality:")
->andWhere("s.is_published = 1")
->getQuery()
->getSingleResult(['speciality' => $speciality, 'lang' => $lang]);
if ($specialityRecord) {
$specialityRecord = $specialityRecord->toArray();
$speciality_langs = \SpecialityLanguages::findBySpecialityId($specialityRecord['id'])->toArray();
$langs_ids = $this->getField($speciality_langs, 'language_id');
if ($langs_ids) {
if (count($langs_ids) > 1) {
$where = "id IN ({langs_ids:array}) AND language_id = :lang:";
} else {
$where = "id = :langs_ids: AND language_id = :lang:";
$langs_ids = $langs_ids[0];
}
$langs = \FilterTranslates::find([
$where,
"bind" => ["langs_ids" => $langs_ids, "lang" => $lang]])->toArray();
}
$faculty = \FilterTranslates::findFirst(["id = :faculty: AND language_id = :language_id:", "bind" => ["faculty" => $specialityRecord['faculty_id'], "language_id" => $lang]])->title;
$speciality_documents = \SpecialityRequiredDocuments::findBySpecialityId($specialityRecord['id'])->toArray();
$speciality_documents_ids = $this->getField($speciality_documents, 'document_id');
if ($speciality_documents_ids && count($speciality_documents_ids)) {
$documents = \FilterTranslates::find([
"id IN ({speciality_documents_ids:array}) AND language_id = :lang:",
"bind" => ["speciality_documents_ids" => $speciality_documents_ids, "lang" => $lang]])->toArray();
foreach ($speciality_documents as $key => $val) {
foreach ($documents as $item => $value) {
if ($value['id'] == $val['document_id'])
$document[] = $value['title'];
}
}
$documents = $document;
}
$speciality_jobs = \SpecialityJobs::findBySpecialityId($specialityRecord['id'])->toArray();
$speciality_jobs_ids = $this->getField($speciality_jobs, 'job_id');
if ($speciality_jobs_ids && count($speciality_jobs_ids))
$jobs = \FilterTranslates::find([
"id IN ({speciality_jobs_ids:array}) AND language_id = :lang:",
"bind" => ["speciality_jobs_ids" => $speciality_jobs_ids, "lang" => $lang]]);
$speciality_subjects = \SpecialitySubjects::findBySpecialityId($specialityRecord['id'])->toArray();
foreach ($speciality_subjects as $speciality_subject) {
$subject_id = $speciality_subject['subject_id'];
$subjects[] = (object)[
'title' => \FilterTranslates::findFirst([
'filter_id = :subject_id: AND language_id = :language_id:',
'bind' => ['subject_id' => $subject_id, 'language_id' => $lang]
])->title,
'duration_hr' => $speciality_subject['duration_hr'],
'checkboxes' => $speciality_subject['checkboxes'],
];
}
$specialityRecord['study_start'] = date("d.m.Y", strtotime($specialityRecord['study_start']));
$specialityRecord['registration_end'] = date("d.m.Y", strtotime($specialityRecord['registration_end']));
$univer_record = \Universities::findFirstById($specialityRecord['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;
}
$speciality_dependents = \SpecialityDependend::findBySpecialityId($specialityRecord['id'])->toArray();
$speciality_dependents_ids = $this->getField($speciality_dependents, 'speciality_depend_id');
if ($speciality_dependents_ids && count($speciality_dependents_ids)) {
$dependents = \FilterTranslates::find([
"id IN ({speciality_dependents_ids:array}) AND language_id = :lang:",
"bind" => ["speciality_dependents_ids" => $speciality_dependents_ids, "lang" => $lang]]);
}
$specialArticles = \SpecialtyArticles::findBySpecialtyId($specialityRecord['id'])->toArray();
$special_articles_ids = $this->getField($specialArticles, 'article_id');
if ($special_articles_ids && count($special_articles_ids)) {
$special_articles = \ArticleTranslates::find(["article_id IN ({special_articles_ids:array}) AND language_id = :language_id:", "bind" => ["special_articles_ids" => $special_articles_ids, "language_id" => $lang]]);
}
$specialityRecord['langs'] = implode(', ', $this->getField($langs, 'title'));
$specialityRecord['faculty'] = $faculty;
$specialityRecord['required_docs'] = $documents;
$specialityRecord['jobs'] = $jobs;
$specialityRecord['subjects'] = $subjects;
$specialityRecord['univer_logo'] = $univerLogo ?? false;
$specialityRecord['univer_banner'] = $univerBanner ?? false;
$specialityRecord['univer_url'] = $univerUrlSuffix;
$specialityRecord['description'] = \SpecialityDescription::findFirst(["speciality_id = :speciality_id: AND language_id = :language_id:", "bind" => ["speciality_id" => $specialityRecord['id'], "language_id" => $lang]]);
$specialityRecord['dependents'] = $dependents;
$specialityRecord['univer_logo_alt'] = $logoAlt;
$specialityRecord['univer_country'] = $country;
$specialityRecord['univer_city'] = $city;
$specialityForCompare = $specialityRecord;
$specialityForCompare['langs'] = $langs_ids;
}
$applyFreeServiceId = \ServiceTranslates::findFirstByUrlSuffix('bezkoshtobna-podacha-polsha-vnz')->service_id;
$applyFreeService = \ServiceTranslates::findFirst(["service_id = :service_id: AND language_id = :language_id:", "bind" => ["service_id" => $applyFreeServiceId, "language_id" => $lang]]);
$this->view->setVars([
'speciality' => $specialityRecord,
'meta' => [
'title' => $specialityRecord['seo_title'],
'description' => $specialityRecord['seo_description'],
'keywords' => $specialityRecord['seo_keywords'],
'cannonical' => $specialityRecord['url_cannonical'],
],
'popularUnivers' => $this->getPopularUniversities($lang),
'relatedUnivers' => $this->getRelatedUniversities($specialityRecord['country_id'], $specialityRecord['city_id'], $specialityRecord['university_id'], $lang),
'articles' => $special_articles,
//'univerGallery' => $univerPhotos,
'currentCity' => $specialityRecord['city'],
'relatedSpecials' => $this->getRelatedSpecialities($specialityForCompare, $lang),
'apply_free_service' => $applyFreeService->url_suffix,
]);
//language titles
$speciality_translate = \SpecialityTranslates::findBySpecialityId($specialityRecord['id']);
$this->changeLangViewParams($speciality_translate);
$breadcrumbs = $this->getBaseBreadCrumbs($translates, $lang_code);
$breadcrumbs[] = [
'pos' => '2',
'title' => $translates['t_content']->specialty_catalog,
'url' => '/' . $lang_code . '/specialities'
];
$breadcrumbs[] = [
'pos' => '2',
'title' => $specialityRecord['title'],
'url' => '/' . $lang_code . '/specialities/' . $specialityRecord['url_suffix'],
'last' => true
];
$this->view->breadcrumbs = $breadcrumbs;
}
public function getRelatedSpecialities($speciality, $lang, $limit = 5)
{
if (is_array($speciality['langs'])) {
$where = "language_id IN ({langs_ids:array})";
} else {
$where = "language_id = :langs_ids:";
}
$speciality_langs = \SpecialityLanguages::find([$where, "bind" => ["langs_ids" => $speciality['langs']]])->toArray();
$specs_ids = $this->getField($speciality_langs, 'speciality_id');
if ($specs_ids) {
if (is_array($specs_ids) && count($specs_ids) > 1) {
$specs_ids_where = "s.id IN ({specs_ids:array})";
} else {
$specs_ids_where = "s.id = :specs_ids:";
$specs_ids = $specs_ids[0];
}
}
$bind = [
'first' => [
"lang" => $lang,
"exclude_id" => $speciality['id'],
"title" => $speciality['title'],
"study_level" => $speciality['study_level_id'],
"specs_ids" => $specs_ids,
],
'second' => [
"lang" => $lang,
"exclude_id" => $speciality['id'],
"title" => $speciality['title'],
"study_level" => $speciality['study_level_id']
],
'third' => [
"lang" => $lang,
"exclude_id" => $speciality['id'],
"science_id" => $speciality['science_id'],
"study_level" => $speciality['study_level_id'],
"specs_ids" => $specs_ids,
]
];
$andWhere = [
'first' => ["str.title = :title:", "str.study_level_id = :study_level:", $specs_ids_where],
'second' => ["str.title = :title:", "str.study_level_id = :study_level:"],
'third' => ["str.science_id = :science_id:", "str.study_level_id = :study_level:", $specs_ids_where]
];
/* get specialities with equal title, study_level and languages */
$relatedSpecials['first'] = $this->getSpecialitiesByQuery($bind['first'], $andWhere['first'], $limit);
/* get specialities with equal title, study_level and different languages */
if (!isset($relatedSpecials['first']) || count($relatedSpecials['first']) < $limit) {
$secondLimit = isset($relatedSpecials['first']) ? ($limit - count($relatedSpecials['first'])) : $limit;
$excludeIds = isset($relatedSpecials['first']) ? $this->getField($relatedSpecials['first'], 'id') : false;
if ($excludeIds) {
$bind['second']['exclude_items'] = $excludeIds;
$andWhere['second'][] = "s.id NOT IN ({exclude_items:array})";
}
$relatedSpecials['second'] = $this->getSpecialitiesByQuery($bind['second'], $andWhere['second'], $secondLimit);
}
/* get specialities with equal science, study_level and languages */
if (!isset($relatedSpecials['second']) || count($relatedSpecials['second']) < $secondLimit) {
$thirdLimit = (isset($relatedSpecials['second'])) ? ($secondLimit - count($relatedSpecials['second'])) : $secondLimit;
if ($excludeIds) {
if (isset($relatedSpecials['second']) && count($relatedSpecials['second'])) {
$excludeIds = array_merge($excludeIds, $this->getField($relatedSpecials['second'], 'id'));
}
$bind['third']['exclude_items'] = $excludeIds;
$andWhere['third'][] = "s.id NOT IN ({exclude_items:array})";
} else {
if (isset($relatedSpecials['second']) && count($relatedSpecials['second'])) {
$excludeIds = $this->getField($relatedSpecials['second'], 'id');
if ($excludeIds) {
$bind['third']['exclude_items'] = $excludeIds;
$andWhere['third'][] = "s.id NOT IN ({exclude_items:array})";
}
}
}
$relatedSpecials['third'] = $this->getSpecialitiesByQuery($bind['third'], $andWhere['third'], $thirdLimit);
}
$relatedSpecials = array_merge($relatedSpecials['first'], $relatedSpecials['second'], $relatedSpecials['third']);
return $relatedSpecials;
}
public function getSpecialitiesByQuery($bind, $andWhere, $limit)
{
$relatedSpecials = $this->modelsManager->createBuilder()
->addFrom('Specialities', 's')
->columns("
s.id id, s.cost_per_year cost_per_year, str.title title, str.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, ft.title as study_level")
->leftjoin('SpecialityTranslates', 'str.speciality_id = s.id AND str.language_id = :lang:', 'str')
->leftjoin('UniversityTranslates', 'utr.university_id = s.university_id AND utr.language_id = :lang:', 'utr')
->leftjoin('CountriesTranslates', 'cot.country_id = s.country_id AND cot.language_id = :lang:', 'cot')
->leftjoin('CitiesTranslates', 'cit.city_id = s.city_id AND cit.language_id = :lang:', 'cit')
->leftjoin('Universities', 'u.id = s.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')
->leftjoin('FilterTranslates', 'ft.id = str.study_level_id AND ft.language_id = :lang:', 'ft')
->where("s.id <> :exclude_id:")
->andWhere("s.is_published = 1");
foreach ($andWhere as $value) {
if ($value) {
if (strpos($value, ':array}') === false) {
$relatedSpecials = $relatedSpecials->andWhere($value);
} else {
preg_match('/({(.*?):array})/', $value, $match);
$key = trim(str_replace(":array}", "", $match[1]), "{");
if ($key === 'specs_ids') {
$relatedSpecials = $relatedSpecials->andWhere("s.id IN (" . implode(',', $bind[$key]) . ")");
}
if ($key === 'exclude_items') {
$relatedSpecials = $relatedSpecials->andWhere("s.id NOT IN (" . implode(',', $bind[$key]) . ")");
}
//$relatedSpecials = $relatedSpecials->andWhere($value, [$key => $bind[$key]]);
unset($bind[$key]);
}
}
}
$limit = ($limit != null) ? $limit : 5;
$relatedSpecials = $relatedSpecials->limit($limit)
->getQuery()
->execute($bind)
->toArray();
if ($relatedSpecials) {
foreach ($relatedSpecials as $key => $item) {
$speciality_langs = \SpecialityLanguages::findBySpecialityId($item['id'])->toArray();
$langs_ids = $this->getField($speciality_langs, 'language_id');
if ($langs_ids) {
if (count($langs_ids) > 1) {
$where = "id IN ({langs_ids:array}) AND language_id = :lang:";
} else {
$where = "id = :langs_ids: AND language_id = :lang:";
}
$langs = \FilterTranslates::find([
$where,
"bind" => ["langs_ids" => $langs_ids, "lang" => $bind['lang']]])->toArray();
$relatedSpecials[$key]['study_langs'] = implode(', ', $this->getField($langs, 'title'));
}
}
}
return $relatedSpecials;
}
}
|