Created
March 24, 2011 12:25
-
-
Save oaugustus/884965 to your computer and use it in GitHub Desktop.
ProposalController, ProposalRepository, Proposal Entity and AbstractRepository
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Neton\PropositalBundle\Model; | |
| use Doctrine\ORM\EntityRepository; | |
| /** | |
| * Abstract Base Repository. | |
| * | |
| * @author Otávio Fernandes <[email protected]> | |
| */ | |
| abstract class AbstractRepository extends EntityRepository | |
| { | |
| /** | |
| * Create a query with filter. | |
| * | |
| * @param QueryBuilder $qb | |
| * @param array $fields | |
| * @param array $filters | |
| * @return Query | |
| */ | |
| protected function createQueryFilter($qb, $fields, $filters = array()) | |
| { | |
| $apply = array(); | |
| foreach ($fields as $field) { | |
| $apply[] = $field." like :query"; | |
| } | |
| $qb->andWhere((implode(' OR ',$apply))); | |
| if (isset($filters['query'])){ | |
| $qb->setParameter('query', "%".$filters['query']."%"); | |
| } else { | |
| $qb->setParameter('query', '%%'); | |
| } | |
| $qb->setFirstResult(isset($filters['start']) ? $filters['start'] : 0)-> | |
| setMaxResults(isset($filters['limit']) ? $filters['limit'] : 200); | |
| // @todo: add sort | |
| return $qb->getQuery(); | |
| } | |
| /** | |
| * Merge an array with the entity. | |
| * | |
| * @todo rewrite this method | |
| * @param array $data | |
| */ | |
| public function fromArray($data) | |
| { | |
| // get the entity metadata | |
| $metadata = $this->_em->getClassMetadata($this->_entityName); | |
| // instantiate the entity | |
| $entity = $this->getEntityInstance($data); | |
| // set the property value | |
| foreach ($data as $property => $value) { | |
| // the field exists in field list | |
| if ($metadata->hasField($property)) { | |
| // get the method name | |
| $method = 'set'.ucfirst($property); | |
| // check if the method is callable | |
| if (is_callable(array($entity, $method))) { | |
| $entity->$method($value); | |
| } | |
| // if the field not exists in field list | |
| } else { | |
| // try call fromArray recursively | |
| if ($metadata->hasAssociation($property) && (is_array($value) || is_object($value))) { | |
| $assoc = $metadata->getAssociationMapping($property); | |
| // define the method name | |
| $method = (is_array($value) ? 'add' : 'set').$this->getSingleEntityName($assoc['targetEntity']); | |
| // check if the method is callable | |
| if (is_callable(array($entity, $method))) { | |
| $repo = $this->_em->getRepository($assoc['targetEntity']); | |
| if (is_callable(array($repo, 'fromArray'))) { | |
| if (is_array($value)) { | |
| foreach ($value as $item) { | |
| $item[$assoc['mappedBy']] = $entity; | |
| $entity->$method($repo->fromArray($item, $entity)); | |
| } | |
| } else { | |
| $entity->$method($value); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return $entity; | |
| } | |
| /** | |
| * Get the single name of entity. | |
| * | |
| * @param string $namespaced Full qualified class name | |
| * @return string | |
| */ | |
| private function getSingleEntityName($namespaced) | |
| { | |
| $parts = explode('\\',$namespaced); | |
| return end($parts); | |
| } | |
| /** | |
| * Retrieves or create an entity instance. | |
| * | |
| * @param array $data | |
| * @return Entity | |
| */ | |
| private function getEntityInstance($data) | |
| { | |
| $entity = null; | |
| // if the id is defined | |
| if (isset($data['id']) && $data['id'] > 0){ | |
| // instantiate the entity based on it id | |
| $entity = $this->find($data['id']); | |
| // clear id reference | |
| unset($data['id']); | |
| } else { | |
| // create a new entity instance | |
| $entity = new $this->_entityName(); | |
| } | |
| return $entity; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Neton\PropositalBundle\Entity; | |
| /** | |
| * Neton\PropositalBundle\Entity\Proposal | |
| * | |
| * @todo implement listeners events | |
| * @orm:Table(name="proposal") | |
| * @orm:Entity(repositoryClass="Neton\PropositalBundle\Model\ProposalRepository") | |
| * @orm:HasLifecycleCallbacks | |
| */ | |
| class Proposal | |
| { | |
| /** | |
| * @var integer $id | |
| * | |
| * @orm:Column(name="id", type="integer", nullable=false) | |
| * @orm:Id | |
| * @orm:GeneratedValue(strategy="IDENTITY") | |
| */ | |
| private $id; | |
| /** | |
| * @var datetime $dtCreate | |
| * | |
| * @orm:Column(name="dt_create", type="datetime", nullable=true) | |
| */ | |
| private $dtCreate; | |
| /** | |
| * @var string $state | |
| * | |
| * @orm:Column(name="state", type="string", length=1, nullable=true) | |
| */ | |
| private $state; | |
| /** | |
| * @var integer $term | |
| * | |
| * @orm:Column(name="term", type="integer", nullable=true) | |
| */ | |
| private $term; | |
| /** | |
| * @var integer $validity | |
| * | |
| * @orm:Column(name="validity", type="integer", nullable=true) | |
| */ | |
| private $validity; | |
| /** | |
| * @var decimal $price | |
| * | |
| * @orm:Column(name="price", type="decimal", nullable=true) | |
| */ | |
| private $price; | |
| /** | |
| * @var datetime $dtSended | |
| * | |
| * @orm:Column(name="dt_sended", type="datetime", nullable=true) | |
| */ | |
| private $dtSended; | |
| /** | |
| * @var datetime $dtApproved | |
| * | |
| * @orm:Column(name="dt_approved", type="datetime", nullable=true) | |
| */ | |
| private $dtApproved; | |
| /** | |
| * @var datetime $dtDeclined | |
| * | |
| * @orm:Column(name="dt_declined", type="datetime", nullable=true) | |
| */ | |
| private $dtDeclined; | |
| /** | |
| * @var datetime $dtRevised | |
| * | |
| * @orm:Column(name="dt_revised", type="datetime", nullable=true) | |
| */ | |
| private $dtRevised; | |
| /** | |
| * @var string $number | |
| * | |
| * @orm:Column(name="number", type="string", length=20, nullable=true) | |
| */ | |
| private $number; | |
| /** | |
| * @var string $crc | |
| * | |
| * @orm:Column(name="crc", type="string", length=50, nullable=true) | |
| */ | |
| private $crc; | |
| /** | |
| * @var Member | |
| * | |
| * @orm:ManyToMany(targetEntity="Member", mappedBy="proposal") | |
| */ | |
| private $member; | |
| /** | |
| * @var Proposal | |
| * | |
| * @orm:ManyToOne(targetEntity="Proposal") | |
| * @orm:JoinColumns({ | |
| * @orm:JoinColumn(name="origin_id", referencedColumnName="id") | |
| * }) | |
| */ | |
| private $origin; | |
| /** | |
| * @var Account | |
| * | |
| * @orm:ManyToOne(targetEntity="Account") | |
| * @orm:JoinColumns({ | |
| * @orm:JoinColumn(name="receiver_id", referencedColumnName="id") | |
| * }) | |
| */ | |
| private $receiver; | |
| /** | |
| * @var Organization | |
| * | |
| * @orm:ManyToOne(targetEntity="Organization") | |
| * @orm:JoinColumns({ | |
| * @orm:JoinColumn(name="customer_id", referencedColumnName="id") | |
| * }) | |
| */ | |
| private $customer; | |
| /** | |
| * @var Organization | |
| * | |
| * @orm:ManyToOne(targetEntity="Organization") | |
| * @orm:JoinColumns({ | |
| * @orm:JoinColumn(name="organization_id1", referencedColumnName="id") | |
| * }) | |
| */ | |
| private $organization1; | |
| /** | |
| * @var Topics | |
| * | |
| * @orm:OneToMany(targetEntity="Topic", mappedBy="proposal", cascade={"persist", "remove"}) | |
| */ | |
| private $topics; | |
| /** | |
| * Initialize proposal. | |
| */ | |
| public function __construct() | |
| { | |
| $this->topics = new \Doctrine\Common\Collections\ArrayCollection(); | |
| } | |
| /** | |
| * Get id | |
| * | |
| * @return integer $id | |
| */ | |
| public function getId() | |
| { | |
| return $this->id; | |
| } | |
| /** | |
| * Set dtCreate | |
| * | |
| * @param datetime $dtCreate | |
| */ | |
| public function setDtCreate($dtCreate) | |
| { | |
| $this->dtCreate = $dtCreate; | |
| } | |
| /** | |
| * Get dtCreate | |
| * | |
| * @return datetime $dtCreate | |
| */ | |
| public function getDtCreate() | |
| { | |
| return $this->dtCreate; | |
| } | |
| /** | |
| * Set state | |
| * | |
| * @param string $state | |
| */ | |
| public function setState($state) | |
| { | |
| $this->state = $state; | |
| } | |
| /** | |
| * Get state | |
| * | |
| * @return string $state | |
| */ | |
| public function getState() | |
| { | |
| return $this->state; | |
| } | |
| /** | |
| * Set term | |
| * | |
| * @param integer $term | |
| */ | |
| public function setTerm($term) | |
| { | |
| $this->term = $term; | |
| } | |
| /** | |
| * Get term | |
| * | |
| * @return integer $term | |
| */ | |
| public function getTerm() | |
| { | |
| return $this->term; | |
| } | |
| /** | |
| * Set validity | |
| * | |
| * @param integer $validity | |
| */ | |
| public function setValidity($validity) | |
| { | |
| $this->validity = $validity; | |
| } | |
| /** | |
| * Get validity | |
| * | |
| * @return integer $validity | |
| */ | |
| public function getValidity() | |
| { | |
| return $this->validity; | |
| } | |
| /** | |
| * Set price | |
| * | |
| * @param decimal $price | |
| */ | |
| public function setPrice($price) | |
| { | |
| $this->price = $price; | |
| } | |
| /** | |
| * Get price | |
| * | |
| * @return decimal $price | |
| */ | |
| public function getPrice() | |
| { | |
| return $this->price; | |
| } | |
| /** | |
| * Set dtSended | |
| * | |
| * @param datetime $dtSended | |
| */ | |
| public function setDtSended($dtSended) | |
| { | |
| $this->dtSended = $dtSended; | |
| } | |
| /** | |
| * Get dtSended | |
| * | |
| * @return datetime $dtSended | |
| */ | |
| public function getDtSended() | |
| { | |
| return $this->dtSended; | |
| } | |
| /** | |
| * Set dtApproved | |
| * | |
| * @param datetime $dtApproved | |
| */ | |
| public function setDtApproved($dtApproved) | |
| { | |
| $this->dtApproved = $dtApproved; | |
| } | |
| /** | |
| * Get dtApproved | |
| * | |
| * @return datetime $dtApproved | |
| */ | |
| public function getDtApproved() | |
| { | |
| return $this->dtApproved; | |
| } | |
| /** | |
| * Set dtDeclined | |
| * | |
| * @param datetime $dtDeclined | |
| */ | |
| public function setDtDeclined($dtDeclined) | |
| { | |
| $this->dtDeclined = $dtDeclined; | |
| } | |
| /** | |
| * Get dtDeclined | |
| * | |
| * @return datetime $dtDeclined | |
| */ | |
| public function getDtDeclined() | |
| { | |
| return $this->dtDeclined; | |
| } | |
| /** | |
| * Set dtRevised | |
| * | |
| * @param datetime $dtRevised | |
| */ | |
| public function setDtRevised($dtRevised) | |
| { | |
| $this->dtRevised = $dtRevised; | |
| } | |
| /** | |
| * Get dtRevised | |
| * | |
| * @return datetime $dtRevised | |
| */ | |
| public function getDtRevised() | |
| { | |
| return $this->dtRevised; | |
| } | |
| /** | |
| * Set number | |
| * | |
| * @param string $number | |
| */ | |
| public function setNumber($number) | |
| { | |
| $this->number = $number; | |
| } | |
| /** | |
| * Get number | |
| * | |
| * @return string $number | |
| */ | |
| public function getNumber() | |
| { | |
| return $this->number; | |
| } | |
| /** | |
| * Set crc | |
| * | |
| * @param string $crc | |
| */ | |
| public function setCrc($crc) | |
| { | |
| $this->crc = $crc; | |
| } | |
| /** | |
| * Get crc | |
| * | |
| * @return string $crc | |
| */ | |
| public function getCrc() | |
| { | |
| return $this->crc; | |
| } | |
| /** | |
| * Add member | |
| * | |
| * @param Neton\PropositalBundle\Entity\Member $member | |
| */ | |
| public function addMember(\Neton\PropositalBundle\Entity\Member $member) | |
| { | |
| $this->member[] = $member; | |
| } | |
| /** | |
| * Get member | |
| * | |
| * @return Doctrine\Common\Collections\Collection $member | |
| */ | |
| public function getMember() | |
| { | |
| return $this->member; | |
| } | |
| /** | |
| * Set origin | |
| * | |
| * @param Neton\PropositalBundle\Entity\Proposal $origin | |
| */ | |
| public function setOrigin(\Neton\PropositalBundle\Entity\Proposal $origin) | |
| { | |
| $this->origin = $origin; | |
| } | |
| /** | |
| * Get origin | |
| * | |
| * @return Neton\PropositalBundle\Entity\Proposal $origin | |
| */ | |
| public function getOrigin() | |
| { | |
| return $this->origin; | |
| } | |
| /** | |
| * Set receiver | |
| * | |
| * @param Neton\PropositalBundle\Entity\Account $receiver | |
| */ | |
| public function setReceiver(\Neton\PropositalBundle\Entity\Account $receiver) | |
| { | |
| $this->receiver = $receiver; | |
| } | |
| /** | |
| * Get receiver | |
| * | |
| * @return Neton\PropositalBundle\Entity\Account $receiver | |
| */ | |
| public function getReceiver() | |
| { | |
| return $this->receiver; | |
| } | |
| /** | |
| * Set customer | |
| * | |
| * @param Neton\PropositalBundle\Entity\Organization $customer | |
| */ | |
| public function setCustomer(\Neton\PropositalBundle\Entity\Organization $customer) | |
| { | |
| $this->customer = $customer; | |
| } | |
| /** | |
| * Get customer | |
| * | |
| * @return Neton\PropositalBundle\Entity\Organization $customer | |
| */ | |
| public function getCustomer() | |
| { | |
| return $this->customer; | |
| } | |
| /** | |
| * Set organization1 | |
| * | |
| * @param Neton\PropositalBundle\Entity\Organization $organization1 | |
| */ | |
| public function setOrganization1(\Neton\PropositalBundle\Entity\Organization $organization1) | |
| { | |
| $this->organization1 = $organization1; | |
| } | |
| /** | |
| * Get organization1 | |
| * | |
| * @return Neton\PropositalBundle\Entity\Organization $organization1 | |
| */ | |
| public function getOrganization1() | |
| { | |
| return $this->organization1; | |
| } | |
| /** | |
| * Add topic. | |
| * | |
| * @param Neton\PropositalBundle\Entity\Topic $topic | |
| */ | |
| public function addTopic(\Neton\PropositalBundle\Entity\Topic $topic) | |
| { | |
| $this->topics[] = $topic; | |
| } | |
| /** | |
| * @orm:PostPersist | |
| */ | |
| public function doOnPrePersist() | |
| { | |
| //die('oi'); | |
| $this->price = 2015.1; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Neton\PropositalBundle\Controller; | |
| use Neton\PropositalBundle\Entity\Proposal; | |
| /** | |
| * Proposal actions. | |
| * | |
| * @author Otávio Fernandes <[email protected]> | |
| */ | |
| class ProposalController extends SessionController | |
| { | |
| /** | |
| * List the proposals by type. | |
| * | |
| * @remote | |
| * @param array $params ['type'] | |
| * @return array | |
| */ | |
| public function listAction($params) | |
| { | |
| $params = array_merge($params, $this->getEnviroment()); | |
| $em = $this->get('doctrine.orm.entity_manager'); | |
| $list = $em->getRepository('PropositalBundle:Proposal')->listByType($params); | |
| return $this->createList($list); | |
| } | |
| /** | |
| * Change the proposal state. | |
| * | |
| * @remote | |
| * @param array $params ['newState','proposalId'] | |
| * @return boolean | |
| */ | |
| public function changeStateAction($params) | |
| { | |
| // get entity manager | |
| $em = $this->get('doctrine.orm.entity_manager'); | |
| // find the proposal | |
| $proposal = $em->find('PropositalBundle:Proposal',$params['proposalId']); | |
| $proposal->setState($params['newState']); | |
| // @todo: notify change | |
| // update record | |
| $em->persist($proposal); | |
| $em->flush(); | |
| return true; | |
| } | |
| /** | |
| * Remove a proposal and send it to trash. | |
| * | |
| * @remote | |
| * @param array $params ['proposalId'] | |
| * @return boolean | |
| */ | |
| public function removeAction($params) | |
| { | |
| // get entity manager | |
| $em = $this->get('doctrine.orm.entity_manager'); | |
| // find the proposal | |
| $proposal = $em->find('PropositalBundle:Proposal',$params['proposalId']); | |
| // set the trash state | |
| $proposal->setState('T'); | |
| // @todo: notify change | |
| // update record | |
| $em->persist($proposal); | |
| $em->flush(); | |
| return true; | |
| } | |
| /** | |
| * Generate the pdf document to preview a proposal and return its full | |
| * path. | |
| * | |
| * @remote | |
| * @param array $params ['proposalId'] | |
| * @return string full path of generated PDF document | |
| */ | |
| public function previewAction($params) | |
| { | |
| // @todo: implement the preview proposal method | |
| return 'http://localhost/proposital/web/pdf.txt'; | |
| } | |
| /** | |
| * Send the proposal to customer e-mail. | |
| * | |
| * @remote | |
| * @param array $params ['proposalId'] | |
| * @return boolean send status | |
| */ | |
| public function sendAction($params) | |
| { | |
| // @todo: implement the send proposal method | |
| return true; | |
| } | |
| /** | |
| * Save the proposal. | |
| * | |
| * @remote | |
| * @param array $params | |
| * @return array the saved proposal | |
| */ | |
| public function saveAction($params) | |
| { | |
| // get entity manager | |
| $em = $this->get('doctrine.orm.entity_manager'); | |
| // get the proposal | |
| $proposal = $em->getRepository('PropositalBundle:Proposal')->fromArray($params); | |
| $proposal = $this->setBasicValues($proposal, $params); | |
| // persist | |
| $em->persist($proposal); | |
| $em->flush(); | |
| // get the proposal to return it | |
| $proposal = $em->getRepository('PropositalBundle:Proposal')->getById($proposal->getId()); | |
| return $proposal; | |
| } | |
| /** | |
| * Set the basic values of a proposal. | |
| * | |
| * @param Proposal $proposal | |
| * @param array $params | |
| * @return Proposal | |
| */ | |
| private function setBasicValues($proposal, $params) | |
| { | |
| $orgId = $this->get('session')->get('organization'); | |
| // get entity manager | |
| $em = $this->get('doctrine.orm.entity_manager'); | |
| // get proposal associations | |
| $receiver = $em->getRepository('PropositalBundle:Account')->getEntityByName($params['receiver']); | |
| $customer = $em->getRepository('PropositalBundle:Organization')->getEntityByName($params['customer'],$receiver); | |
| $organization = $em->find('PropositalBundle:Organization', $orgId); | |
| // set propoerties | |
| if (($params['id']) == 0) { | |
| $proposal->setDtCreate(new \DateTime()); | |
| $proposal->setState('E'); | |
| $proposal->setNumber('2011-03-0006-1'); | |
| } | |
| // set associations | |
| $proposal->setCustomer($customer); | |
| $proposal->setReceiver($receiver); | |
| $proposal->setOrganization1($organization); | |
| return $proposal; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Neton\PropositalBundle\Model; | |
| /** | |
| * ProposalRepository | |
| * | |
| * @author Otávio Fernandes <[email protected]> | |
| */ | |
| class ProposalRepository extends AbstractRepository | |
| { | |
| private $Proposal = 'Neton\PropositalBundle\Entity\Proposal'; | |
| /** | |
| * Get all proposals from a specific type and enviroment. | |
| * | |
| * @param array $filters | |
| * @return array | |
| */ | |
| public function listByType($filters) | |
| { | |
| // create the dql to get proposals | |
| $qb = $this->_em->createQueryBuilder()-> | |
| select('p, c.name as customer, r.name as receiver, r.email')-> | |
| from($this->Proposal, 'p')-> | |
| join('p.customer', 'c')-> | |
| join('p.receiver', 'r')-> | |
| join('p.organization1', 'o')-> | |
| where('p.state = :type')-> | |
| andWhere('o.id = :organization')-> // @todo: add collaborator filter | |
| setParameter('type', $filters['type'])-> | |
| setParameter('organization', $filters['organization']); | |
| $q = $this->createQueryFilter($qb, array('c.name','r.name'), $filters); | |
| $rs = $q->getResult('FlatScalar'); | |
| return $rs; | |
| } | |
| /** | |
| * Get a proposal from your id. | |
| * | |
| * @param array $filters | |
| * @return array | |
| */ | |
| public function getById($id) | |
| { | |
| // create the dql to get proposals | |
| $qb = $this->_em->createQueryBuilder()-> | |
| select('p, c.name as customer, r.name as receiver, r.email')-> | |
| from($this->Proposal, 'p')-> | |
| join('p.customer', 'c')-> | |
| join('p.receiver', 'r')-> | |
| andWhere('p.id = :id')-> | |
| setParameter('id', $id); | |
| //$q = $this->createQueryFilter($qb, array('c.name','r.name'), $filters); | |
| $rs = $qb->getQuery()->getResult('FlatScalar'); | |
| return $rs[0]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment