/** * Testing the DEnumItem::loadItems method directly. * * @since 1.2.1 */ public function testDEnumItemLoadItems() { $DEnumID = $this->denum1->getOID(); $item = new DEnumItem(); $items = $item->loadItems($DEnumID); $this->assertGreaterThan(0, count($items), 'testing the DEnumItem::loadItems method directly'); }
/** * Handle POST requests. * * @param Alpha\Util\Http\Request $request * * @return Alpha\Util\Http\Response * * @throws Alpha\Exception\SecurityException * * @since 1.0 */ public function doPOST($request) { self::$logger->debug('>>doPOST($request=[' . var_export($request, true) . '])'); $params = $request->getParams(); try { // check the hidden security fields before accepting the form POST data if (!$this->checkSecurityFields()) { throw new SecurityException('This page cannot accept post data from remote servers!'); self::$logger->debug('<<doPOST'); } // ensure that a OID is provided if (isset($params['denumOID'])) { $BOoid = $params['denumOID']; } else { throw new IllegalArguementException('Could not load the DEnum object as an denumOID was not supplied!'); } if (isset($params['saveBut'])) { try { $this->BO->load($BOoid); // update the object from post data $this->BO->populateFromArray($params); ActiveRecord::begin(); $this->BO->save(); self::$logger->action('DEnum ' . $this->BO->getOID() . ' saved'); // now save the DEnumItems $tmp = new DEnumItem(); $denumItems = $tmp->loadItems($this->BO->getID()); foreach ($denumItems as $item) { $item->set('value', $params['value_' . $item->getID()]); $item->save(); self::$logger->action('DEnumItem ' . $item->getOID() . ' saved'); } // handle new DEnumItem if posted if (isset($params['new_value']) && trim($params['new_value']) != '') { $newItem = new DEnumItem(); $newItem->set('value', $params['new_value']); $newItem->set('DEnumID', $this->BO->getID()); $newItem->save(); self::$logger->action('DEnumItem ' . $newItem->getOID() . ' created'); } ActiveRecord::commit(); $this->setStatusMessage(View::displayUpdateMessage(get_class($this->BO) . ' ' . $this->BO->getID() . ' saved successfully.')); return $this->doGET($request); } catch (FailedSaveException $e) { self::$logger->error('Unable to save the DEnum of id [' . $params['oid'] . '], error was [' . $e->getMessage() . ']'); ActiveRecord::rollback(); } ActiveRecord::disconnect(); } } catch (SecurityException $e) { $this->setStatusMessage(View::displayErrorMessage($e->getMessage())); self::$logger->warn($e->getMessage()); } catch (IllegalArguementException $e) { $this->setStatusMessage(View::displayErrorMessage($e->getMessage())); self::$logger->error($e->getMessage()); } catch (RecordNotFoundException $e) { self::$logger->warn($e->getMessage()); $this->setStatusMessage(View::displayErrorMessage('Failed to load the requested item from the database!')); } $body = View::displayPageHead($this); $message = $this->getStatusMessage(); if (!empty($message)) { $body .= $message; } $body .= View::displayPageFoot($this); self::$logger->debug('<<doPOST'); return new Response(200, $body, array('Content-Type' => 'text/html')); }
/** * Custom edit view. * * @return string * * @since 1.0 */ public function editView($fields = array()) { $config = ConfigProvider::getInstance(); $labels = $this->BO->getDataLabels(); $obj_type = ''; $html = '<form action="' . $fields['URI'] . '" method="POST" accept-charset="UTF-8">'; $temp = new StringBox($this->BO->getPropObject('name'), $labels['name'], 'name', '', 0, true, true); $html .= $temp->render(); $html .= '<h3>DEnum display values:</h3>'; // now get all of the options for the enum and render $denum = $this->BO; $tmp = new DEnumItem(); $denumItems = $tmp->loadItems($denum->getID()); foreach ($denumItems as $item) { $labels = $item->getDataLabels(); $temp = new StringBox($item->getPropObject('value'), $labels['value'], 'value_' . $item->getID(), ''); $html .= $temp->render(); } $fieldname = $config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('version_num')) : 'version_num'; $html .= '<input type="hidden" name="' . $fieldname . '" value="' . $this->BO->getVersion() . '"/>'; $html .= '<h3>Add a new value to the DEnum dropdown list:</h3>'; $temp = new StringBox(new String(), 'Dropdown value', 'new_value', ''); $html .= $temp->render(); $temp = new Button('submit', 'Save', 'saveBut'); $html .= $temp->render(); $html .= ' '; $temp = new Button("document.location = '" . FrontController::generateSecureURL('act=Alpha\\Controller\\DEnumController') . "'", 'Back to List', 'cancelBut'); $html .= $temp->render(); $html .= ''; $html .= View::renderSecurityFields(); $html .= '</form>'; return $html; }
/** * Get the array of DEnum options from the database. * * @param bool $alphaSort * * @return array * * @since 1.0 * * @throws Alpha\Exception\AlphaException */ public function getOptions($alphaSort = false) { try { $options = new self(); $options->loadByAttribute('name', $this->name->getValue()); } catch (RecordNotFoundException $e) { throw new AlphaException('Failed to load DEnum ' . $this->name->getValue() . ', not found in database.'); } // now build an array of item indexes to be returned $count = 0; $this->options = array(); $tmp = new DEnumItem(); foreach ($tmp->loadItems($options->getOID()) as $DEnumItem) { $this->options[$DEnumItem->getID()] = $DEnumItem->getValue(); ++$count; } if ($alphaSort) { asort($this->options, SORT_STRING); } return $this->options; }
/** * Testing the doPOST method. */ public function testDoPOST() { $config = ConfigProvider::getInstance(); $sessionProvider = $config->get('session.provider.name'); $session = SessionProviderFactory::getInstance($sessionProvider); $front = new FrontController(); $controller = new DEnumController(); $securityParams = $controller->generateSecurityFields(); $item = new DEnumItem(); $denumItems = $item->loadItems($this->denum->getOID()); $item = $denumItems[0]; $params = array('saveBut' => true, 'var1' => $securityParams[0], 'var2' => $securityParams[1], 'value_' . $item->getOID() => 'updated'); $params = array_merge($params, $item->toArray()); $request = new Request(array('method' => 'POST', 'URI' => '/denum/' . $this->denum->getOID(), 'params' => $params)); $response = $front->process($request); $this->assertEquals(200, $response->getStatus(), 'Testing the doPOST method'); $this->assertEquals('text/html', $response->getHeader('Content-Type'), 'Testing the doPOST method'); }