Skip to content

Instantly share code, notes, and snippets.

@ssmusoke
Created March 29, 2012 18:39
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 ssmusoke/2241773 to your computer and use it in GitHub Desktop.
Save ssmusoke/2241773 to your computer and use it in GitHub Desktop.
Model for Family
<?php
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Profile for a person who will be included within a family tree
*
* @ORM\Entity
* @ORM\Table(name="person")
*
*/
class Person extends BaseEntity {
/**
* @ORM\Column(type="string")
* @Assert\NotBlank(message = "person_firstname_error")
*/
protected $firstname = '';
/**
* @ORM\Column(type="string")
*
* @Assert\NotBlank(message = "person_surname_error")
*/
protected $surname = '';
/**
* @ORM\Column(type="string")
*/
protected $clanname = '';
/**
* @ORM\Column(type="string")
*/
protected $othernames = '';
/**
* @ORM\Column(type="string")
*/
protected $alias = '';
/**
* @ORM\Column(type="integer")
*/
protected $gender;
/**
* @ORM\Column(type="integer")
*/
protected $lifestatus;
/**
* @ORM\Column(type="date")
*/
protected $dateofbirth;
/**
* The circa date column for the date of birth column above
*/
protected $cdateofbirth;
// relationships
/**
* @ORM\OneToOne(targetEntity="UserAccount")
* @ORM\JoinColumn(name="ownerid", referencedColumnName="id")
* @Assert\NotBlank(message = "person_owner_error")
*/
protected $owner;
/**
* @ORM\OneToOne(targetEntity="UserAccount", inversedBy="person")
* @ORM\JoinColumn(name="userid", referencedColumnName="id")
*/
protected $user;
/**
* @ORM\OneToOne(targetEntity="Clan")
* @ORM\JoinColumn(name="clanid", referencedColumnName="id")
* @Assert\NotBlank(message = "person_clan_error")
*/
protected $clan;
/**
* @ORM\OneToMany(targetEntity="Family", mappedBy="father")
* @ORM\JoinColumn(name="id", referencedColumnName="fatherid")
**/
protected $ffamilies;
/**
* @ORM\OneToMany(targetEntity="Family", mappedBy="mother")
* @ORM\JoinColumn(name="id", referencedColumnName="motherid")
**/
protected $mfamilies;
/**
* @ORM\ManyToOne(targetEntity="Family", inversedBy="children")
* @ORM\JoinColumn(name="familyid", referencedColumnName="id")
**/
protected $family;
// columns which are not mapped to the database
/**
* The id of the person to whom a relation is to be created
*/
protected $personid;
/**
* The type of relationship which will determine what type of family tree entry will be made
*/
protected $relationshiptype;
/**
* Initialization Method
*
* @ORM\PostLoad
*/
public function init() {
parent::init();
// initialize the parent and child arrays
$this->addForeignKeys(array('ownerid', 'clanid'));
// collections
$this->ffamilies = new ArrayCollection();
$this->mfamilies = new ArrayCollection();
}
/**
* Return the person's full names, which is a concatenation of the first and the surname
*
* @return String
*/
function getName() {
return $this->getFirstName()." ".$this->getSurname();
}
/**
* Set the owner of the person profile
* @param integer clanid The ID of the owner's subscriber profile
*/
function setOwnerId($ownerid) {
$this->owner = loadEntity('UserAccount', $ownerid);
}
/**
* Set the clan
* @param integer $clanid The ID of the owner's subscriber profile
*/
function setClanID($clanid) {
if (!isEmptyString($clanid)) {
$this->clan = loadEntity('Clan', $clanid);
}
}
/**
* Return the ID of the clan or FALSE if there is no clan
*
* @return integer|FALSE
*/
function getClanID() {
if (is_null($this->clan)) {
return false;
}
return $this->clan->getID();
}
/**
* Return the ID of the family or FALSE if there is no clan
*
* @return integer|FALSE
*/
function getFamilyID() {
if (is_null($this->family)) {
return false;
}
return $this->family->getID();
}
/* (non-PHPdoc)
* @see BaseRecord::afterProcessPost()
*/
public function afterProcessPost()
{
$result = parent::afterProcessPost();
if (!$result) {
return false;
}
// initialize the family tree records
// depending create a new family tree entry depending on the relationship type
switch ($this->relationshiptype) {
case RELATIONSHIP_FATHER:
// father being added
$rel = new Family();
// set the father
$rel->setFather($this);
// add the family to the person
// add the personid as one of the children
$rel->addChildByID($this->personid);
$this->family = $rel;
break;
case RELATIONSHIP_MOTHER:
// mother being added
$rel = new Family();
$rel->setMotherID($this->personid);
$rel->setType(RELATIONSHIP_MOTHER);
$rel->getChild()->setMother($this);
// add to the list of children
$rel->setParent($this);
$this->children[] = $rel;
break;
case RELATIONSHIP_SON:
// son being added
$rel = new Family();
$rel->setFatherID($this->personid);
// check the gender of the parent then make the necessary assignments
if ($rel->getParent()->getGender() == 1) {
$this->setFather($rel->getParent());
$rel->setType(RELATIONSHIP_FATHER);
} else if ($rel->getParent()->getGender() == 2) {
$this->setMother($rel->getParent());
$rel->setType(RELATIONSHIP_MOTHER);
}
// add to the list of parents
$rel->setChild($this);
$this->parents[] = $rel;
break;
case RELATIONSHIP_DAUGHTER:
// daughter being created
$rel = new Family();
$rel->setFatherID($this->personid);
// check the gender of the parent then make the necessary assignments
if ($rel->getParent()->getGender() == 1) {
$this->setFather($rel->getParent());
$rel->setType(RELATIONSHIP_FATHER);
} else if ($rel->getParent()->getGender() == 2) {
$this->setMother($rel->getParent());
$rel->setType(RELATIONSHIP_MOTHER);
}
// add to the list of parents
$rel->setChild($this);
$this->parents[] = $rel;
break;
case "":
// no relationship
break;
default:
$this->addError(sprintf(translateKey('person_relationshipttype_unknown'), $this->relationshiptype));
return false;
}
return true;
}
/**
* Get the date of birth, this is formatted to the selected page display format
*
* @return string
*/
function getDateofBirth() {
return changeMySQLDateToPageFormat($this->dateofbirth);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment