Inserted basic structure for clubs and models
This commit is contained in:
320
src/admin/mymodels/club.php
Normal file
320
src/admin/mymodels/club.php
Normal file
@@ -0,0 +1,320 @@
|
||||
<?php
|
||||
|
||||
// No direct access.
|
||||
use Joomla\CMS\Factory;
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
class ClubsClub
|
||||
{
|
||||
protected $id;
|
||||
protected $name;
|
||||
protected $address;
|
||||
protected $city;
|
||||
protected $homapge;
|
||||
protected $mail;
|
||||
protected $iban;
|
||||
protected $bic;
|
||||
protected $charitable;
|
||||
protected $presidentId;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getAddress()
|
||||
{
|
||||
return $this->address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getCity()
|
||||
{
|
||||
return $this->city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHomapge()
|
||||
{
|
||||
return $this->homapge;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getMail()
|
||||
{
|
||||
return $this->mail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIban()
|
||||
{
|
||||
return $this->iban;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBic()
|
||||
{
|
||||
return $this->bic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isCharitable()
|
||||
{
|
||||
return $this->charitable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getPresidentId()
|
||||
{
|
||||
return $this->presidentId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ClubsUser
|
||||
*/
|
||||
public function getPresident()
|
||||
{
|
||||
return ClubsUser::loadUser((int) $this->presidentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $address
|
||||
*/
|
||||
public function setAddress(string $address)
|
||||
{
|
||||
$this->address = $address;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $city
|
||||
*/
|
||||
public function setCity(string $city)
|
||||
{
|
||||
$this->city = $city;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $homapge
|
||||
*/
|
||||
public function setHomapge(string $homapge)
|
||||
{
|
||||
$this->homapge = $homapge;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mail
|
||||
*/
|
||||
public function setMail(string$mail)
|
||||
{
|
||||
$this->mail = $mail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iban
|
||||
*/
|
||||
public function setIban(string $iban)
|
||||
{
|
||||
$this->iban = $iban;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $bic
|
||||
*/
|
||||
public function setBic(string $bic)
|
||||
{
|
||||
$this->bic = $bic;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $charitable
|
||||
*/
|
||||
public function setCharitable(bool $charitable)
|
||||
{
|
||||
$this->charitable = $charitable;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $presidentId
|
||||
*/
|
||||
public function setPresidentId(int $presidentId)
|
||||
{
|
||||
$this->presidentId = $presidentId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ClubsUser $user
|
||||
*/
|
||||
public function setPresident(ClubsUser $user)
|
||||
{
|
||||
$this->presidentId = $user->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*/
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
protected function loadData(array $data)
|
||||
{
|
||||
$mapping = array('id', 'name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
|
||||
|
||||
foreach($mapping as $m)
|
||||
$this->$m = $data[$m];
|
||||
}
|
||||
|
||||
protected function __construct()
|
||||
{}
|
||||
|
||||
public static function loadClubs()
|
||||
{
|
||||
$dbo = Factory::getDbo();
|
||||
$q = $dbo->getQuery(true);
|
||||
$q->select('*')
|
||||
->from('#__club_clubs');
|
||||
$dbo->setQuery($q);
|
||||
$dbo->execute();
|
||||
$list = $dbo->loadAssocList('id');
|
||||
|
||||
$ret = array();
|
||||
foreach($list as $c)
|
||||
{
|
||||
$co = new ClubsClub();
|
||||
$co->loadData($c);
|
||||
|
||||
$ret[] = $co;
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @throws Exception
|
||||
* @return ClubsOffer
|
||||
*/
|
||||
public static function loadClub(int $id)
|
||||
{
|
||||
$dbo = Factory::getDbo();
|
||||
$q = $dbo->getQuery(true);
|
||||
$q->select('*')->from('#__club_clubs')->where('id=' . (int) $id);
|
||||
$dbo->setQuery($q);
|
||||
$dbo->execute();
|
||||
|
||||
$row = $dbo->loadAssoc();
|
||||
|
||||
if($row == null)
|
||||
{
|
||||
throw new Exception("No club found.");
|
||||
// TODO
|
||||
}
|
||||
|
||||
$club = new ClubsClub();
|
||||
$club->loadData($row);
|
||||
return $club;
|
||||
}
|
||||
|
||||
public static function createClub()
|
||||
{
|
||||
$club = new ClubsClub();
|
||||
$club->id = 'new';
|
||||
return $club;
|
||||
}
|
||||
|
||||
|
||||
public function save()
|
||||
{
|
||||
if($this->id === 'new')
|
||||
$this->insertClub();
|
||||
else
|
||||
$this->updateClub();
|
||||
}
|
||||
|
||||
private function insertClub()
|
||||
{
|
||||
$dbo = Factory::getDbo();
|
||||
$q = $dbo->getQuery(true);
|
||||
|
||||
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
|
||||
$values = array();
|
||||
foreach($mapping as $m)
|
||||
$values[$m] = $q->q($this->$m);
|
||||
|
||||
$q->insert('#__club_clubs')
|
||||
->columns(array('name'))
|
||||
->values(join(',', $values))
|
||||
;
|
||||
|
||||
$dbo->transactionStart();
|
||||
$dbo->setQuery($q);
|
||||
$dbo->execute();
|
||||
$this->id = $dbo->insertid();
|
||||
$dbo->transactionCommit();
|
||||
}
|
||||
|
||||
private function updateClub()
|
||||
{
|
||||
$dbo = Factory::getDbo();
|
||||
$q = $dbo->getQuery(true);
|
||||
|
||||
$mapping = array('name', 'address', 'city', 'homepage', 'mail', 'iban', 'bic', 'charitable', 'presidentId');
|
||||
$values = array();
|
||||
foreach($mapping as $m)
|
||||
$values[$m] = $q->q($this->$m);
|
||||
|
||||
$q->update('#__club_clubs');
|
||||
foreach($mapping as $m)
|
||||
$q->set($q->qn($m) . '=' . $values[$m]);
|
||||
$q->where("id=". (int) $this->id);
|
||||
|
||||
$dbo->setQuery($q);
|
||||
$dbo->execute();
|
||||
}
|
||||
|
||||
public function delete()
|
||||
{
|
||||
if($this->id === 'new')
|
||||
return;
|
||||
$dbo = Factory::getDbo();
|
||||
|
||||
$q = $dbo->getQuery(true);
|
||||
$q->delete('#__club_clubs')
|
||||
->where('id=' . (int) $this->id);
|
||||
|
||||
$dbo->setQuery($q);
|
||||
$dbo->execute();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user