Skip to content

Instantly share code, notes, and snippets.

@andku83
Last active February 5, 2018 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andku83/8fcfb48ba5e0b465220545a5082be1d3 to your computer and use it in GitHub Desktop.
Save andku83/8fcfb48ba5e0b465220545a5082be1d3 to your computer and use it in GitHub Desktop.
<?php
namespace Admin\Model;
use Rush\Mapper\AbstractMapper;
/**
* Class Dictionary
*
* @package Admin\Model
*/
class Dictionary extends AbstractMapper
{
/**
* @var string
*/
protected $tableName = 'translate_key';
/**
* @param $id
* @return array|\ArrayObject|null
*/
public function find($id)
{
$select = $this->getSelect()->where(array($this->tableName . '.id' => $id));
$result = $this->select($select)->current();
$select = $this->getSelect('translate_value')->where(array('kid' => $id))->columns(array('lang_id', 'value'));
$values = $this->select($select)->toArray();
foreach($values as $val) {
$result['value_' . $val['lang_id']] = $val['value'];
}
return $result;
}
/**
* @return array
*/
public function findAll()
{
$select = $this->getSelect()
->join('translate_value', $this->tableName . '.id = translate_value.kid', array('value'))
->join('lang', "translate_value.lang_id = lang.id", array('lang_key' => 'key', 'lang_value' => 'value'))
->group($this->tableName . '.key');
return $this->select($select)->toArray();
}
/**
* @return array
*/
public function values()
{
$values = array();
$result = $this->select($this->getSelect());
if ($result->count() > 0) {
foreach ($result as $value) {
$values[$value['id']] = $value['name'];
}
}
return $values;
}
/**
* @return int
*/
public function count()
{
return $this->select($this->getSelect())->count();
}
/**
* @param $post
* @return bool
*/
public function save($post)
{
$result = false;
$rowset = $this->_toArray($post);
$translate_key = $this->_beforeSave($rowset['keys']);
$translate_value = $rowset['values'];
$select = $this->getSelect($this->tableName)->where(array('id' => $translate_key['id']));
$entity = $this->select($select)->current();
if ($entity) {
$this->update($translate_key, array('id' => $entity->id) , $this->tableName);
$id = $entity->id;
} else {
$this->insert($translate_key, $this->tableName);
$id = $this->getLastInsertValue();
}
$this->delete(array('kid' => $id), 'translate_value');
foreach ($translate_value as $key => $item) {
$item['kid'] = (int)$id;
$result = $this->insert($item, 'translate_value');
}
return (bool)$result;
}
/**
* @param $id
* @return bool
*/
public function remove($id)
{
$result = false;
$entity = $this->find($id);
if ($entity) {
$result = $this->delete(array('id' => $id));
}
return (bool)$result;
}
/**
* @param $data
* @return mixed
*/
private function _toArray($data)
{
$keys = array(
'id' => empty($data['id']) ? null : $data['id'],
'name' => $data['name'],
'key' => $data['key'],
);
$value = array();
$lang = \Rush::alias('Admin\Model\Lang')->findAll();
foreach ($lang as $item) {
if (!empty($data['value_' . $item['id']])) {
$value[$item['id']] =
array(
'id' => NULL,
'lang_id' => $item['id'],
'value' => $data['value_' . $item['id']],
);
}
}
$result['keys'] = $keys;
$result['values'] = $value;
return $result;
}
/**
* @param array $rowset
* @return array
*/
private function _beforeSave(array $rowset = array())
{
$select = $this->getSelect()->where(array('key' => $rowset['key']));
if (!empty($rowset['id'])) {
$select->where("id != {$rowset['id']}");
}
if ($this->select($select)->current() !== false) {
$rowset['key'] = $rowset['key'] . '_' . rand(111, 999);
}
return $rowset;
}
}
<?php
namespace App\Model;
use Rush\Cache;
use Rush\Paginator;
use Zend\Db\Sql\Expression;
/**
* Class Football
*
* @package App\Model
*/
class Football extends TranslateModel
{
/**
* @var int
*/
private $loadCount = 9;
/**
* @var string
*/
protected $tableName = 'football_profile';
/**
* @var array
*/
protected $translate_column_names = array('name', 'biography');
/**
* @var array
*/
protected $keys = array('birthday', 'height', 'weight', 'country_id', 'football_club_id', 'foot', 'contract_from', 'contract_to');
/**
* @var array
*/
protected $relations = array('country', 'football_club');
/**
* @param string $url
* @return array
*/
public function findByUrl($url = '')
{
$cache_name = 'football-findByUrl-' . \Rush::get('lang') . '-' . $url;
if (Cache::has($cache_name)) {
$result = Cache::get($cache_name);
} else {
$country_name = $this->getSelect($this->translate)
->where(array('table_name' => 'country', 'column_name' => 'name'));
$country = $this->getSelect('country')
->join(array('name' => $country_name), 'country.id = name.kid', array('name' => 'value', 'lang_id'))
->join('lang', "name.lang_id = lang.id", array())
->where(array('lang.key' => \Rush::get('lang')))
->order('sorting DESC')
->order('name');
$club_name = $this->getSelect($this->translate)
->where(array('table_name' => 'football_club', 'column_name' => 'name', 'lang_id'));
$club = $this->getSelect('football_club')
->join(array('country' => $country),
"football_club.country_id = country.id",
array('country_image' => 'image', 'country_name' => 'name'))
->join(array('name' => $club_name), 'football_club.id = name.kid', array('name' => 'value', 'lang_id'))
->join('lang', "name.lang_id = lang.id", array())
->where(array('lang.key' => \Rush::get('lang')))
->order('sorting DESC')
->order('name');
$age = new Expression('floor(datediff(CURRENT_DATE, birthday)/365)');
parent::findByUrl($url);
$select = $this->loadSelect
->join('country', "{$this->tableName}.country_id = country.id", array('country_image' => 'image'))
->join(array('club' => $club),
"{$this->tableName}.football_club_id = club.id",
array('football_club_image' => 'image', 'football_club_country_image' => 'country_image'))
->columns(array('id', 'url', 'image', 'birthday', 'height', 'weight', 'foot', 'contract_from', 'contract_to', 'sorting', 'age' => $age));
$result = $this->select($select)->current();
//add positions
$select = $this->getSelect('football_profile_has_position')
->join($this->translate, "football_profile_has_position.position_id = {$this->translate}.kid", array('position_name' => 'value'))
->join('lang', "{$this->translate}.lang_id = lang.id", array())
->columns(array('position_id'))
->where(array('profile_id' => $result['id'], $this->translate.'.table_name' => 'football_position', 'lang.key' => \Rush::get('lang')))
->order('sorting DESC');
$positions = $this->select($select)->toArray();
foreach($positions as $position) {
$result['position'][$position['position_id']] = $position['position_name'];
}
//add social link
$select = $this->getSelect('football_social')->where(array('football_profile_id' => $result['id']))->columns(array('name', 'link'));
$socials = $this->select($select)->toArray();
foreach ($socials as $social){
$result['social'][$social['name']] = $social['link'];
}
//add trophy
$trophy_name = $this->getSelect($this->translate)->where(array('table_name' => 'football_trophy', 'column_name' => 'name'));
$select = $this->getSelect('football_trophy')
->join(array('name' => $trophy_name), "football_trophy.id = name.kid", array('name' => 'value'))
->join('football_profile_has_trophy', 'football_profile_has_trophy.football_trophy_id = football_trophy.id', array('number'))
->where(array('football_profile_has_trophy.profile_id' => $result['id'], 'name.lang_id' => $result['lang_id']))
->order('sorting DESC');
$result['trophies'] = $this->select($select)->toArray();
//add club history
$select = $this->getSelect('football_profile_history')
->join(array('left_club' => $club),
"football_profile_history.left_club = left_club.id",
array('left_club_image' => 'image', 'left_club_country_image' =>'country_image', 'left_club_name' => 'name'),
'LEFT'
)
->join(array('join_club' => $club),
"football_profile_history.join_club = join_club.id",
array('join_club_image' => 'image', 'join_club_country_image' =>'country_image', 'join_club_name' => 'name'),
'LEFT'
)
->where(array('profile_id' => $result['id']))
->columns(array('id', 'season', 'date', 'left_club_id' => 'left_club', 'join_club_id' => 'join_club', 'sorting'))
->order('sorting DESC')
->order('date DESC');
$result['history'] = $this->select($select)->toArray();
//add season
$select = $this->getSelect('football_profile_season')
->where(array('profile_id' => $result['id']))
->order('sorting DESC')
->order('games DESC');
$result['season'] = $this->select($select)->toArray();
//add career
$select = $this->getSelect('football_profile_career')
->join(array('team' => $country),
"football_profile_career.team = team.id",
array('country_image' => 'image', 'country_name' => 'name'),
'LEFT'
)
->where(array('profile_id' => $result['id']))
->order('sorting DESC')
->order('goals DESC');
$result['career'] = $this->select($select)->toArray();
Cache::add($cache_name, $result);
}
return $result;
}
/**
* @return array
*/
public function findAll()
{
parent::selectFindAll();
$age = new Expression('floor(datediff(CURRENT_DATE, birthday)/365)');
$select = $this->loadSelect
->order('sorting DESC')
->columns(array('id', 'url', 'image', 'birthday', 'age' => $age, 'sorting'));
$result = $this->select($select)->toArray();
$params = \Rush::app()->getRequest()->getUri()->getQueryAsArray();
foreach ($result as $key => &$profile){
$select = $this->getSelect('football_profile_has_position')
->join($this->translate, "football_profile_has_position.position_id = {$this->translate}.kid", array('position_name' => 'value'))
->join('lang', "{$this->translate}.lang_id = lang.id",array())
->columns(array('position_id'))
->where(array('profile_id' => $profile['id'], $this->translate.'.table_name' => 'football_position', 'lang.key' => \Rush::get('lang')))
->order('sorting DESC');
$rowset = $this->select($select)->toArray();
$position = array();
foreach($rowset as $item) {
$position[$item['position_id']] = $item['position_name'];
}
$profile['position'] = $position;
if (!empty($params['position'])){
$find_position = explode(',', $params['position']);
foreach ($find_position as $item){
if (array_key_exists($item, $position)){
continue 2;
}
}
unset($result[$key]);
}
}
unset($profile);
return $result;
}
/**
* @param int $page
*
* @return \Zend\Paginator\Paginator
*/
public function getAllWithPagination($page = 1)
{
return Paginator::paginate($this->findAll(), $page, $this->loadCount);
}
/**
* @return $this
*/
public function executeQuery()
{
$params = \Rush::app()->getRequest()->getUri()->getQueryAsArray();
if (isset($params['pagination'])) {
$this->loadCount = (int)$params['pagination'];
}
// if (isset($params['sorting']) and (string)$params['sorting'] !== '') {
// if ((string)$params['sorting'] == 'price') {
// $this->loadSelect->order("{$this->tableName}.price ASC");
// } elseif ((string)$params['sorting'] == 'name') {
// $this->loadSelect->order("{$this->tableName}.name ASC");
// }
// } else {
// $this->loadSelect->order("{$this->tableName}.name ASC");
// }
if (!empty($params['country'])) {
$this->loadSelect->where("country_id IN ({$params['country']})");
}
if (!empty($params['club'])) {
$this->loadSelect->where("football_club_id IN ({$params['club']})");
}
if (!empty($params['age_min'])) {
$start_birthday = date('Y-m-d', strtotime(date('Y-m-d').' -'.$params['age_min'].' year'));
$this->loadSelect->where("birthday < '".$start_birthday . "'");
}
if (!empty($params['age_max'])) {
$start_birthday = date('Y-m-d', strtotime(date('Y-m-d').' -'.$params['age_max'].' year'));
$this->loadSelect->where("birthday > '".$start_birthday . "'");
}
if (!empty($params['word'])) {
$this->filterBySearch($params['word']);
}
return $this;
}
/**
* @param $table
* @return \Zend\Db\Sql\Select
*/
public function getGlobalSelect($table)
{
$select = $this->getSelect($table)
->join($this->translate, "$table.id = {$this->translate}.kid", array('name' => 'value'))
->join('lang', "{$this->translate}.lang_id = lang.id", array())
->where(array($this->translate.'.table_name' => $table, 'lang.key' => \Rush::get('lang')))
->columns(array('id', 'image'))
->order("{$this->translate}.value");
return $select;
}
/**
* @return array
*/
public function getGlobalPosition()
{
$cache_name = 'football-position-filter-' . \Rush::get('lang');
if (Cache::has($cache_name)) {
$result = Cache::get($cache_name);
} else {
$select = $this->getGlobalSelect('football_position')
->columns(array('id'));
$result = $this->select($select)->toArray();
Cache::add($cache_name, $result);
}
return $result;
}
/**
* @return array
*/
public function getGlobalCountry()
{
$cache_name = 'football-country-filter-' . \Rush::get('lang');
if (Cache::has($cache_name)) {
$result = Cache::get($cache_name);
} else {
$select = $this->getGlobalSelect('country')
->columns(array('id'));
$result = $this->select($select)->toArray();
Cache::add($cache_name, $result);
}
return $result;
}
/**
* @return array
*/
public function getGlobalClub()
{
$cache_name = 'football-club-filter-' . \Rush::get('lang');
if (Cache::has($cache_name)) {
$result = Cache::get($cache_name);
} else {
$select = $this->getGlobalSelect('football_club')
->columns(array('id'));
$result = $this->select($select)->toArray();
Cache::add($cache_name, $result);
}
return $result;
}
/**
* @param string $search
*
* @return $this
*/
public function filterBySearch($search = '')
{
if (!empty($search)) {
$search = preg_replace('/\+/', ' ', $search);
$rowset = array('name.value', 'country.value', 'football_club.value', 'biography.value');
foreach ($rowset as $value) {
$this->getPredicate()->like( $value, '%' . $search . '%');
}
$this->loadSelect->where($this->getPredicate());
}
return $this;
}
/**
* @param $search
*
* @return array
*/
public function search($search)
{
$this->filterBySearch($search);
$age = new Expression('floor(datediff(CURRENT_DATE, birthday)/365)');
$select = $this->loadSelect
->where(array(
$this->tableName . '.is_active' => 1,
'lang.key' => \Rush::get('lang')
))
->columns(array('id', 'url', 'image', 'age' => $age));
$rowset = $this->select($select)->toArray();
$result = array();
if (!empty($rowset)) {
foreach ($rowset as $key => $value) {
$result[$key]['name'] = $value['name'];
$result[$key]['age'] = $value['age'];
$result[$key]['image'] = $value['image'];
$result[$key]['country'] = $value['country_name'];
$result[$key]['club'] = $value['football_club_name'];
$result[$key]['link'] = \Rush::url('football/view', array('url' => $value['url']));
}
}
return $result;
}
}
<?php
namespace AppAdmin\Model;
use Rush\Helper;
use Rush\Locale;
use Rush\Upload;
/**
* Class FootballProfile
*
* @package AppAdmin\Model
*/
class FootballProfile extends TranslateModel
{
/**
* @var string
*/
protected $tableName = 'football_profile';
/**
* @var array
*/
protected $translate_column_names = array('name', 'biography');
/**
* @var array
*/
protected $keys = array('birthday', 'height', 'weight', 'country_id', 'football_club_id', 'foot', 'contract_from', 'contract_to');
/**
* @var array
*/
protected $relations = array('country', 'football_club');
/**
* @const
*/
protected $IMAGE_WIDTH = 518;
/**
* @const
*/
protected $IMAGE_HEIGHT = 712;
/**
* @const
*/
protected $SLIDER_WIDTH = 844;
/**
* @const
*/
protected $SLIDER_HEIGHT = 563;
/**
* @param $id
*
* @return array|\ArrayObject|null
*/
public function find($id)
{
$result = parent::find($id);
$select = $this->getSelect('football_social')
->where(array('football_profile_id' => $id))
->columns(array('name', 'link'));
$socials = $this->select($select)->toArray();
foreach($socials as $social) {
$result['social'][$social['name']] = $social['link'];
}
// $upload = new Upload();
//
// $upload->deletePath(APPLICATION_ROOT . $this->appPath . '/' . $id);
return $result;
}
/**
* @param $post
* @param $file
* @return bool
*/
public function save($post, $file = null)
{
$result = false;
$rowset = $this->_beforeSave($this->_toArray($post));
$translate_key = $rowset['keys'];
$translate_value = $rowset['values'];
$select = $this->getSelect($this->tableName)->where(array('id' => $translate_key['id']));
$entity = $this->select($select)->current();
if ($entity) {
$result = $this->update($translate_key, array('id' => $entity->id) , $this->tableName);
$this->delete(array('kid' => $entity->id, 'table_name' => $this->tableName), $this->translate);
$this->delete(array('profile_id' => $entity->id), 'football_profile_has_position');
$this->delete(array('football_profile_id' => $entity->id), 'football_social');
$id = $entity->id;
} else {
$result = $this->insert($translate_key, $this->tableName);
$id = $this->getLastInsertValue();
}
foreach ($translate_value as $key => $item) {
$item['table_name'] = $this->tableName;
$item['kid'] = (int)$id;
$result = $this->insert($item, $this->translate);
}
if (!empty($file->image) /*and (bool)$result*/) {
$upload = new Upload();
$upload->path($this->appPath)
->save($file->image)
->resize($id . '-' . date('His'), $this->IMAGE_WIDTH, $this->IMAGE_HEIGHT)
->clean();
if ($upload->resized !== NULL) {
if (!empty($entity->image)) {
$upload->deleteFile($this->appPath . '/' . $entity->image);
}
$image['image'] = $upload->resized;
$result = $this->update($image, array('id' => $id), $this->tableName);
}
}
if (isset($post['position']) and is_array($post['position'])) {
foreach ($post['position'] as $position) {
$result = $this->insert(array('profile_id' => $id, 'position_id' => $position), 'football_profile_has_position');
}
}
if (isset($post['social']) and is_array($post['social'])) {
foreach ($post['social'] as $name => $social) {
if (!empty($social)) {
$result = $this->insert(
array(
'name' => $name,
'football_profile_id' => $id,
'link' => $social),
'football_social');
}
}
}
return (bool)$result;
}
/**
* @param $id
* @param $file
* @param $post
*
* @return \stdClass
*/
public function moveFile($id, $file, $post)
{
$upload = new Upload();
$upload->path($this->appPath . '/' . $id);
$answer = new \stdClass();
$moving = $file->mf_file_upload;
if (!empty($moving)) {
$time = explode('.', microtime(true));
$upload->save($moving)
->resize('img-' . date('dHis') . $time[1], $this->SLIDER_WIDTH, $this->SLIDER_HEIGHT)
->clean();
$answer->filename = $moving['name'];
$answer->size = sprintf("%.2fMB", $moving['size'] / 1048576);
}
if ($post->deleteFile) {
$upload->deleteFile($post->deleteFile);
}
return $answer;
}
/**
* @param $id
*
* @return array
*/
public function findFiles($id)
{
$result = array();
$folder = APPLICATION_ROOT . $this->appPath .'/'. $id . '/';
if (file_exists($folder) and (int)$id > 0) {
$files = array_diff(scandir($folder), array('.', '..', '.DS_Store'));
foreach ($files as $file):
$result[] = $this->appPath . '/' . $id . '/' . $file;
endforeach;
}
return $result;
}
/**
* @param $data
*
* @return array
*/
protected function _toArray($data)
{
$select = $this->getSelect('lang')->where(array('key' => Locale::findDefault()))->columns(array('id'));
$lang = $this->select($select)->current();
$keys = array(
'id' => empty($data['id']) ? null : $data['id'],
'url' => empty($data['url']) ? Helper::translit($data['name_' . $lang['id']]) : Helper::translit($data['url']),
'sorting' => $data['sorting'],
'is_active' => $data['is_active'],
);
foreach ($this->keys as $keyname){
$keys[$keyname] = $data[$keyname];
}
$keyname = array();
$langs = \Rush::alias('Admin\Model\Lang')->findAll();
foreach ($langs as $lang) {
foreach ($this->translate_column_names as $column_name) {
if (!empty($data[$column_name . '_' . $lang['id']])) {
$keyname[$column_name . '_' . $lang['id']] =
array(
'id' => NULL,
'lang_id' => $lang['id'],
'column_name' => $column_name,
'value' => $data[$column_name . '_' . $lang['id']],
);
}
}
}
$result['keys'] = $keys;
$result['values'] = $keyname;
return $result;
}
/**
* @param $rowset
*
* @return array
*/
private function _beforeSave(array $rowset = array())
{
$select = $this->getSelect();
if (isset($rowset['keys']['id'])) {
$select->where("id != {$rowset['keys']['id']}");
}
if ($this->select($select->where(array('url' => $rowset['keys']['url'])))->current() !== false) {
$rowset['keys']['url'] = $rowset['keys']['url'] . '-' . rand(111, 999);
}
return $rowset;
}
}
<?php
namespace App\Model;
use Rush\Mapper\AbstractMapper;
/**
* Class TranslateModel
*
* @package App\Model
*/
abstract class TranslateModel extends AbstractMapper
{
/**
* @var \Zend\Db\Sql\Select
*/
protected $loadSelect;
/**
* @var string
*/
protected $tableName;
/**
* @var string
*/
protected $translate = 'football_translate_value';
/**
* @var array
*/
protected $translate_column_names = array('name');
/**
* @var array
*/
protected $keys;
/**
* @var array
*/
protected $relations = array();
/**
* @var
*/
public $appPath;
/**
* @const
*/
protected $IMAGE_WIDTH = 50;
/**
* @const
*/
protected $IMAGE_HEIGHT = 50;
/**
*
*/
public function __construct()
{
$this->appPath = \Rush::config('directory.image') . '/' . $this->tableName;
$this->loadSelect = $this->getLoadSelect();
}
/**
* @param $id
*
* @return array|\ArrayObject|null
*/
public function find($id)
{
$select = $this->loadSelect->where(array($this->tableName . '.id' => $id, 'lang.key' => \Rush::get('lang')));
$result = $this->select($select)->current();
// $select = $this->getSelect($this->translate)->where(array('table_name' => $this->tableName,'kid' => $id))->columns(array('column_name', 'lang_id', 'value'));
// $values = $this->select($select)->toArray();
// foreach($values as $val) {
// $result[$val['column_name'] . '_' . $val['lang_id']] = $val['value'];
// }
return $result;
}
/**
* @return array
*/
public function findAll()
{
$select = $this->loadSelect
->where(array(
$this->tableName.'.is_active' => 1,
'lang.key' => \Rush::get('lang')));
return $this->select($select)->toArray();
}
/**
* @return \Zend\Db\Sql\Select
*/
public function selectFindAll()
{
$this->loadSelect
->where(array(
$this->tableName.'.is_active' => 1,
'lang.key' => \Rush::get('lang')));
return $this;
}
/**
* @param string $url
*
* @return array|\ArrayObject|null
*/
public function findByUrl($url = '')
{
$select = $this->loadSelect
->where(array(
$this->tableName . '.url' => (string)$url,
$this->tableName . '.is_active' => 1,
'lang.key' => \Rush::get('lang')
));
return $this->select($select)->current();
}
/**
* @return \Zend\Db\Sql\Select
*/
public function getLoadSelect()
{
$is_first = true;
$select = $this->getSelect();
foreach ($this->translate_column_names as $column_name){
$$column_name = $this->getSelect($this->translate)->where(array('table_name' => $this->tableName, 'column_name' => $column_name));
if ($is_first){
$select->join(array($column_name => $$column_name), "{$this->tableName}.id = {$column_name}.kid", array($column_name => 'value', 'lang_id'));
$first_column = $column_name;
$is_first = false;
} else {
$select->join(array($column_name => $$column_name), "{$this->tableName}.id = {$column_name}.kid AND {$first_column}.lang_id = {$column_name}.lang_id", array($column_name => 'value'));
}
}
if ($this->translate_column_names) {
$select->join('lang', "{$first_column}.lang_id = lang.id", array('lang_key' => 'key', 'lang_value' => 'value', 'lang_sorting' => 'sorting'));
}
foreach ($this->relations as $relation){
$$relation = $this->getSelect($this->translate)->where(array('table_name' => $relation))->columns(array('column_name', 'kid', 'lang_id', 'value'));
$select->join(array($relation => $$relation), "{$this->tableName}.{$relation}_id = {$relation}.kid AND lang.id = {$relation}.lang_id", array("{$relation}_name" => 'value'), 'left');
}
return $select;
}
/**
* @return array
*/
public function values()
{
$list = array();
$data = $this->findAll();
if (!empty($data)) {
foreach ($data as $item) {
$list[$item['id']] = $item['name'];
}
}
return $list;
}
/**
* @param $data
*
* @return array
*/
protected function _toArray($data)
{
$keys = array(
'id' => empty($data['id']) ? null : $data['id'],
'sorting' => $data['sorting'],
'is_active' => $data['is_active'],
);
foreach ($this->keys as $keyname){
$keys[$keyname] = $data[$keyname];
}
$keyname = array();
$langs = \Rush::alias('Admin\Model\Lang')->findAll();
foreach ($langs as $lang) {
foreach ($this->translate_column_names as $column_name) {
if (!empty($data[$column_name . '_' . $lang['id']])) {
$keyname[$column_name . '_' . $lang['id']] =
array(
'id' => NULL,
'lang_id' => $lang['id'],
'column_name' => $column_name,
'value' => $data[$column_name . '_' . $lang['id']],
);
}
}
}
$result['keys'] = $keys;
$result['values'] = $keyname;
return $result;
}
}
<?php
namespace AppAdmin\Model;
use Rush\Upload;
use Rush\Locale;
use Rush\Mapper\AbstractMapper;
/**
* Class TranslateModel
*
* @package AppAdmin\Model
*/
abstract class TranslateModel extends AbstractMapper
{
/**
* @var \Zend\Db\Sql\Select
*/
protected $loadSelect;
/**
* @var string
*/
protected $tableName;
/**
* @var string
*/
protected $translate = 'football_translate_value';
/**
* @var array
*/
protected $translate_column_names = array('name');
/**
* @var array
*/
protected $keys = array();
/**
* @var array
*/
protected $relations = array();
/**
* @var
*/
public $appPath;
/**
* @const
*/
protected $IMAGE_WIDTH = 50;
/**
* @const
*/
protected $IMAGE_HEIGHT = 50;
/**
*
*/
public function __construct()
{
$this->appPath = \Rush::config('directory.image') . '/' . $this->tableName;
$this->loadSelect = $this->getLoadSelect();
}
/**
* @param $id
*
* @return array|\ArrayObject|null
*/
public function find($id)
{
$select = $this->loadSelect->where(array($this->tableName . '.id' => $id, 'lang.key' => Locale::findDefault()));
$result = $this->select($select)->current();
$select = $this->getSelect($this->translate)->where(array('table_name' => $this->tableName,'kid' => $id))->columns(array('column_name', 'lang_id', 'value'));
$values = $this->select($select)->toArray();
foreach($values as $val) {
$result[$val['column_name'] . '_' . $val['lang_id']] = $val['value'];
}
return $result;
}
/**
* @return array
*/
public function findAll()
{
$select = $this->loadSelect->where(array('lang.key' => Locale::findDefault()));
return $this->select($select)->toArray();
}
/**
* @return \Zend\Db\Sql\Select
*/
public function selectFindAll()
{
$this->loadSelect
->where(array(
$this->tableName.'.is_active' => 1,
'lang.key' => \Rush::get('lang')));
return $this;
}
/**
* @return \Zend\Db\Sql\Select
*/
public function getLoadSelect()
{
$is_first = true;
$select = $this->getSelect();
foreach ($this->translate_column_names as $column_name){
$$column_name = $this->getSelect($this->translate)->where(array('table_name' => $this->tableName, 'column_name' => $column_name));
if ($is_first){
$select->join(array($column_name => $$column_name), "{$this->tableName}.id = {$column_name}.kid", array($column_name => 'value', 'lang_id'));
$first_column = $column_name;
$is_first = false;
} else {
$select->join(array($column_name => $$column_name), "{$this->tableName}.id = {$column_name}.kid AND {$first_column}.lang_id = {$column_name}.lang_id", array($column_name => 'value'));
}
}
if ($this->translate_column_names) {
$select->join('lang', "{$first_column}.lang_id = lang.id", array('lang_key' => 'key', 'lang_value' => 'value', 'lang_sorting' => 'sorting'));
} else {
// $select->join('lang', "{$this->tableName}.id = lang.id", array('lang_key' => 'key', 'lang_value' => 'value', 'lang_sorting' => 'sorting'), 'outer');
}
foreach ($this->relations as $relation){
$$relation = $this->getSelect($this->translate)->where(array('table_name' => $relation))->columns(array('column_name', 'kid', 'lang_id', 'value'));
$select->join(array($relation => $$relation), "{$this->tableName}.{$relation}_id = {$relation}.kid AND lang.id = {$relation}.lang_id", array("{$relation}_name" => 'value'), 'left');
}
return $select;
}
/**
* @return array
*/
public function values()
{
$list = array();
$data = $this->findAll();
if (!empty($data)) {
foreach ($data as $item) {
$list[$item['id']] = $item['name'];
}
}
return $list;
}
/**
* @param $post
* @param $file
* @return bool
*/
public function save($post, $file = null)
{
$result = false;
$rowset = $this->_toArray($post);
$translate_key = $rowset['keys'];
$translate_value = $rowset['values'];
$select = $this->getSelect($this->tableName)->where(array('id' => $translate_key['id']));
$entity = $this->select($select)->current();
if ($entity) {
$result = $this->update($translate_key, array('id' => $entity->id) , $this->tableName);
$this->delete(array('kid' => $entity->id, 'table_name' => $this->tableName), $this->translate);
$id = $entity->id;
} else {
$result = $this->insert($translate_key, $this->tableName);
$id = $this->getLastInsertValue();
}
foreach ($translate_value as $key => $item) {
$item['table_name'] = $this->tableName;
$item['kid'] = (int)$id;
$result = $this->insert($item, $this->translate);
}
if (!empty($file->image) /*and (bool)$result*/) {
$upload = new Upload();
if($this->IMAGE_WIDTH != 0 && $this->IMAGE_HEIGHT != 0) {
$upload->path($this->appPath)
->save($file->image)
->resize($id . '-' . date('His'), $this->IMAGE_WIDTH, $this->IMAGE_HEIGHT)
->clean();
if ($upload->resized !== NULL) {
if (!empty($entity->image)) {
$upload->deleteFile($this->appPath . '/' . $entity->image);
}
$image['image'] = $upload->resized;
$result = $this->update($image, array('id' => $id), $this->tableName);
}
} else {
if(!empty($file->image['name'])) {
$userfile_extn = explode(".", strtolower($file->image['name']));
$userfile_extn = end($userfile_extn);
$new_name = $id . '-' . date('His') . '.' . $userfile_extn;
$upload->path($this->appPath)->saveFile($file->image, $new_name);
if ($upload->saved !== NULL) {
if (!empty($entity->image)) {
$upload->deleteFile($this->appPath . '/' . $entity->image);
}
$image['image'] = $upload->saved;
$result = $this->update($image, array('id' => $id), $this->tableName);
}
}
}
}
return (bool)$result;
}
/**
* @param $id
*
* @return bool
*/
public function toggle($id)
{
$status = 1;
$entity = $this->find($id);
if ((int)$entity->is_active === 1) {
$status = 0;
}
return (bool)$this->update(array('is_active' => $status), array('id' => $id));
}
/**
* @param $id
*
* @return bool
*/
public function remove($id)
{
$result = false;
$entity = $this->find($id);
if ($entity !== false) {
$upload = new Upload();
if (!empty($entity->image)) {
$upload->deleteFile($this->appPath . '/' . $entity->image);
}
$upload->deletePath($this->appPath . '/' . $id);
$this->delete(array('kid' => $entity->id, 'table_name' => $this->tableName), $this->translate);
$result = $this->delete(array('id' => $id));
}
return (bool)$result;
}
/**
* @param $data
*
* @return array
*/
protected function _toArray($data)
{
$keys = array(
'id' => empty($data['id']) ? null : $data['id'],
'sorting' => $data['sorting'],
'is_active' => $data['is_active'],
);
foreach ($this->keys as $keyname){
$keys[$keyname] = $data[$keyname];
}
$keyname = array();
$langs = \Rush::alias('Admin\Model\Lang')->findAll();
foreach ($langs as $lang) {
foreach ($this->translate_column_names as $column_name) {
if (!empty($data[$column_name . '_' . $lang['id']])) {
$keyname[$column_name . '_' . $lang['id']] =
array(
'id' => NULL,
'lang_id' => $lang['id'],
'column_name' => $column_name,
'value' => $data[$column_name . '_' . $lang['id']],
);
}
}
}
$result['keys'] = $keys;
$result['values'] = $keyname;
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment