Skip to content

Instantly share code, notes, and snippets.

@dzhuryn
Created April 29, 2021 11:54
Show Gist options
  • Save dzhuryn/0e16cc866744fe84dbb25bda1aa0e888 to your computer and use it in GitHub Desktop.
Save dzhuryn/0e16cc866744fe84dbb25bda1aa0e888 to your computer and use it in GitHub Desktop.
Change Site_content
<?php
class SiteContent
{
public function scopeWithTVs($query, $tvList = array(), $sep = ':', $tree = false)
{
$main_table = 'site_content';
if ($tree) {
$main_table = 't2';
}
if (!empty($tvList)) {
/** @var \Illuminate\Database\Query\Builder $query */
$query->addSelect($main_table . '.*');
$tvList = array_unique($tvList);
$tvListWithDefaults = [];
foreach ($tvList as $v) {
$tmp = explode($sep, $v, 2);
$tvListWithDefaults[$tmp[0]] = !empty($tmp[1]) ? trim($tmp[1]) : '';
}
$tablePrefix = \DB::getTablePrefix();
$tvs = SiteTmplvar::whereIn('name', array_keys($tvListWithDefaults))->get()->pluck('id', 'name')->toArray();
foreach ($tvs as $tvname => $tvid) {
$query = $query->leftJoin('site_tmplvar_contentvalues as tv_' . $tvname, function ($join) use ($main_table, $tvid, $tvname) {
$join->on($main_table . '.id', '=', 'tv_' . $tvname . '.contentid')->where('tv_' . $tvname . '.tmplvarid', '=', $tvid);
});
if (!empty($tvListWithDefaults[$tvname]) && $tvListWithDefaults[$tvname] == 'd') {
$query = $query->leftJoin('site_tmplvars as tvd_' . $tvname, function ($join) use ($tvid, $tvname) {
$join->where('tvd_' . $tvname . '.id', '=', $tvid);
});
$query = $query->selectRaw('IF(' . $tablePrefix . 'tv_' . $tvname . '.value,' . $tablePrefix . 'tv_' . $tvname . '.value,' . $tablePrefix . 'tvd_' . $tvname . '.default_text) as tv_' . $tvname);
} else {
$query = $query->addSelect('tv_' . $tvname . '.value as ' . $tvname);
}
$query = $query->groupBy('tv_' . $tvname . '.value');
}
$query->groupBy($main_table . '.id');
}
return $query;
}
public function scopeWhereDefaultIn($query, $column, $values, $boolean = 'and')
{
$tablePrefix = \DB::getTablePrefix();
$query->whereRaw("
IFNULL(
`" . $tablePrefix . "tv_".$column."`.`value`, `" . $tablePrefix . "tvd_".$column."`.`default_text`
) in(" . str_repeat('?,', count($values) - 1) . "?)
", $values, $boolean);
}
public function scopeWhereDefault($query, $column, $value, $boolean = 'and')
{
$tablePrefix = \DB::getTablePrefix();
$query->whereRaw("
IFNULL(
`" . $tablePrefix . "tv_".$column."`.`value`, `" . $tablePrefix . "tvd_".$column."`.`default_text`
) = ?
", $value, $boolean);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment