/** * @return Table_Messages */ public static function getInstance() { if (!self::$uniqueInstance) { self::$uniqueInstance = new Table_Messages(); } return self::$uniqueInstance; }
/** * Enter description here... * */ public function createAction() { $this->requirePost(); $form = $this->getNewByingsForm(); if ($form->isValid($_POST)) { // do some inputting $buyings = array(); for ($i = 0; $i < 10; $i++) { if ($form->getValue("product{$i}")) { $product = $form->getValue("product{$i}"); $price = $form->getValue("price{$i}"); $buying = Table_Buyings::getInstance()->createRow(); $buying->setDescription($product); $buying->setPriceInEuro($price); $buying->setResident($this->getCurrentResident()); $buying->save(); $buyings[] = $buying; } else { break; } } if (!empty($buyings)) { # tailor message that going to be sent to the other residents $message = $this->getCurrentResident()->getName(); $message .= " hat folgende Dinge gekauft:\n"; $buying = null; $summ = 0.0; foreach ($buyings as $buying) { $message .= $buying->getDescription() . " für " . $buying->getPriceInEuro() . "€\n"; $summ += $buying->getPriceInEuro(); } $message .= "\nGesammtwert: \t {$summ} €"; foreach (Table_Residents::getInstance()->fetchAll() as $resident) { if ($resident->getId() != $this->getCurrentResident()->getId()) { # Send a message to the other residents. Telling them the current one # entered a new buying. $msg = Table_Messages::getInstance()->createRow(); $msg->setFrom('System'); $msg->setFor($resident); $msg->setTitle('Neuer Einkauf'); $msg->setMessage($message); $msg->save(); } } } // list byings $this->flash('Einkäufte eingetragen'); $this->redirect('index'); } else { $this->redirect('new'); } }
/** * Creates an Message to all residents. * * @param int $buyingId */ protected function _createMessage($buyingId) { $buying = Table_Buyings::getInstance()->findById($buyingId); // create a message for each resident $residents = Table_Residents::getInstance()->fetchAll(); foreach ($residents as $resident) { $date = array(); $data['residend_id'] = $resident->getId(); $data['title'] = 'Neuer Einkauf'; $data['message'] = 'Neuer Einkauf'; Table_Messages::getInstance()->createRow($data); } }
/** * * @param int $amount * @return array<Message> */ public function getLatestMessages($amount) { return Table_Messages::getInstance()->getLatestFor($this, $amount); }