Пример #1
0
/**
 * The action that displays the entry insert form .
 *
 * @param PDO $pdo The PDO object.
 * @return Opt_View
 */
function action($pdo, $config)
{
    $view = new Opt_View('add.tpl');
    $view->title = 'Add new entry';
    $form = new Form($view);
    $form->setAction('index.php?action=add');
    $form->addField('author', 'required,min_len=3,max_len=30', 'The length must be between 3 and 30 characters.');
    $form->addField('email', 'required,email,min_len=3,max_len=100', 'The value must be a valid mail with maximum 100 characters long.');
    $form->addField('website', 'url,min_len=3,max_len=100', 'The value must be a valid URL with maximum 100 characters long.');
    $form->addField('body', 'required,min_len=3', 'The body must be at least 3 characters long.');
    if ($form->validate()) {
        $values = $form->getValues();
        $stmt = $pdo->prepare('INSERT INTO `entries` (`author`, `email`, `date`, `website`, `body`)
			VALUES(:author, :email, :date, :website, :body)');
        $stmt->bindValue(':author', $values['author'], PDO::PARAM_STR);
        $stmt->bindValue(':email', $values['email'], PDO::PARAM_STR);
        $stmt->bindValue(':date', time(), PDO::PARAM_INT);
        $stmt->bindValue(':website', $values['website'], PDO::PARAM_STR);
        $stmt->bindValue(':body', $values['body'], PDO::PARAM_STR);
        $stmt->execute();
        $view->setTemplate('message.tpl');
        $view->message = 'The entry has been successfully added!';
        $view->redirect = 'index.php?action=list';
    } else {
        // The form is an object, so we need to inform OPT about it.
        $view->form = $form;
        $view->setFormat('form', 'Objective');
    }
    return $view;
}
Пример #2
0
 /**
  * Allows to perform the attribute modifications to certain HTML
  * tags (that are in the "com" namespace), so that we can add there
  * extra CSS classes depending on the component state.
  *
  * @param String $nodeName The tag name.
  * @param Array $attributes The source attribute list
  * @return Array
  */
 public function manageAttributes($nodeName, array $attributes)
 {
     if ($this->_form->status() != Form::FORM_INVALID) {
         return $attributes;
     }
     if ($nodeName == 'div' && $this->_form->getValidationStatus($this->_params['name'])) {
         $attributes['class'] = $attributes['class'] . ' ' . $this->_view->getTemplateVar('formInvalidFieldRowClass');
     }
     return $attributes;
 }
Пример #3
0
 protected function setUp()
 {
     $tpl = new Opt_Class();
     $tpl->sourceDir = 'test://templates/';
     $tpl->compileDir = CPL_DIR;
     $tpl->compileMode = Opt_Class::CM_REBUILD;
     $tpl->stripWhitespaces = false;
     $tpl->prologRequired = true;
     $tpl->register(Opt_Class::OPT_COMPONENT, 'opt:myComponent', 'myComponent');
     $tpl->register(Opt_Class::OPT_BLOCK, 'opt:myBlock', 'myBlock');
     $tpl->setup();
     $this->tpl = $tpl;
     Opt_View::clear();
 }
Пример #4
0
 /**
  * Creates a new instance of OPF.
  *
  * @param Opt_Class $opt Open Power Template instance.
  */
 public function __construct(Opt_Class $opt)
 {
     Opl_Registry::set('opf', $this);
     $opt->register(Opt_Class::OPT_NAMESPACE, 'opf');
     $opt->register(Opt_Class::OPT_INSTRUCTION, 'Form', 'Opf_View_Instruction_Form');
     $opt->register(Opt_Class::OPT_FORMAT, 'Form', 'Opf_View_Format_Form');
     $opt->register(Opt_Class::OPT_FORMAT, 'Design', 'Opf_View_Format_Design');
     $opt->register(Opt_Class::OPT_FORMAT, 'FormRepeater', 'Opf_View_Format_FormRepeater');
     $opt->register(Opt_Class::OPT_COMPONENT, 'opf:input', 'Opf_Widget_Input');
     $opt->register(Opt_Class::OPT_COMPONENT, 'opf:textarea', 'Opf_Widget_Textarea');
     $opt->register(Opt_Class::OPT_COMPONENT, 'opf:password', 'Opf_Widget_Password');
     $opt->register(Opt_Class::OPT_COMPONENT, 'opf:yesno', 'Opf_Widget_Yesno');
     $opt->register(Opt_Class::OPT_COMPONENT, 'opf:select', 'Opf_Widget_Select');
     $opt->register(Opt_Class::OPT_COMPONENT, 'opf:collection', 'Opf_Widget_Collection');
     Opt_View::setFormatGlobal('design', 'Design', false);
 }
Пример #5
0
 /**
  * The private rendering utility that performs all the basic
  * rendering tasks, such as registering the data formats for
  * the placeholders.
  *
  * @param Opt_View $view The view the form is rendered in.
  * @internal
  */
 protected function _onRender(Opt_View $view)
 {
     $this->setInternal('name', $this->_name);
     foreach ($this->_items as $placeholder => &$void) {
         $view->setFormat($placeholder, 'Form/Form');
     }
     foreach ($this->_collection as $item) {
         $item->_onRender($view);
     }
 }
Пример #6
0
Opl_Registry::setState('opl_debug_console', false);
Opl_Registry::setState('opl_extended_errors', true);
try {
    $tpl = new Opt_Class();
    $tpl->sourceDir = './templates/';
    $tpl->compileDir = './templates_c/';
    $tpl->charset = 'utf-8';
    $tpl->compileMode = Opt_Class::CM_REBUILD;
    $tpl->stripWhitespaces = false;
    //$tpl->register(Opt_Class::OPT_FORMAT, 'Paginator', 'Opc_Paginator_DataFormat');
    $tpl->setup();
    $opc = new Opc_Class();
    $pager = Opc_Paginator::create(1000, 13);
    // returns Opc_Paginator_Pager;
    $pager->all = 1000;
    $pager->page = isset($_GET['page']) ? $_GET['page'] : 1;
    $view = new Opt_View('paginator_opt.tpl');
    $view->pager = $pager;
    //$view->setFormat('pager', 'Paginator');
    $view->setFormat('pager', 'Objective/Array');
    $view->setFormat('pager.decorator', 'Objective');
    $out = new Opt_Output_Http();
    $out->setContentType(Opt_Output_Http::HTML);
    $out->render($view);
} catch (Opc_Exception $exception) {
    $handler = new Opc_ErrorHandler();
    $handler->display($exception);
} catch (Opt_Exception $exception) {
    $handler = new Opt_ErrorHandler();
    $handler->display($exception);
}
Пример #7
0
 /**
  * The interface method of Opt_Caching_Interface. Finalizes
  * the caching of the view.
  *
  * @param Opt_View $view The cached view.
  */
 public function templateCacheStop(Opt_View $view)
 {
     $header = array('timestamp' => time(), 'expire' => $this->getExpiryTime(), 'dynamic' => $view->hasDynamicContent() ? 'true' : 'false');
     $header = '<' . '?php /* ' . serialize($header) . '*/ ?>' . "\n";
     $tpl = Opl_Registry::get('opt');
     $tpl->setBufferState('cache', false);
     if ($view->hasDynamicContent()) {
         $buffer = $view->getOutputBuffers();
         $dyn = file_get_contents($tpl->compileDir . $view->_convert($view->getTemplate()) . '.dyn');
         if ($dyn !== false) {
             $dynamic = unserialize($dyn);
             unset($dyn);
         } else {
             throw new Opc_View_Cache_InvalidDynamicContent_Exception($view->getTemplate());
             return false;
         }
         $content = '';
         for ($i = 0, $endI = count($buffer); $i < $endI; $i++) {
             $content .= $buffer[$i];
             $content .= $dynamic[$i];
         }
         if (file_put_contents($this->getDirectory() . $this->_getFilename($view), $header . $content . ob_get_flush()) === false) {
             throw new Opc_View_Cache_CannotSaveFile_Exception($this->getDirectory());
             return false;
         }
     } else {
         if (file_put_contents($this->getDirectory() . $this->_getFileName($view), $header . ob_get_contents()) === false) {
             throw new Opc_View_Cache_CannotSaveFile_Exception($this->getDirectory());
             return false;
         }
     }
 }
Пример #8
0
    die('Please create the config.php file from config.sample.php!');
}
require './config.php';
require './includes/forms.php';
require './components/base.php';
require './components/input.php';
require './components/textarea.php';
try {
    $tpl = new Opt_Class();
    // Load the configuration
    $tpl->loadConfig($config['opt']);
    // Register the components
    $tpl->register(Opt_Class::OPT_COMPONENT, 'opt:input', 'InputComponent');
    $tpl->register(Opt_Class::OPT_COMPONENT, 'opt:textarea', 'TextareaComponent');
    $tpl->setup();
    Opt_View::assignGlobal('baseHref', $config['script']['baseHref']);
    // Connect to the database.
    $pdo = new PDO($config['db']['dsn'], $config['db']['user'], $config['db']['password']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->query('SET NAMES `' . $config['db']['charset'] . '`');
    // Load and execute the action.
    $action = 'list';
    if (isset($_GET['action']) && ctype_alpha($_GET['action'])) {
        $action = $_GET['action'];
    }
    require './actions/' . $action . '.php';
    $view = action($pdo, $config);
    // Render the returned view to the browser.
    $output = new Opt_Output_Http();
    $output->setContentType(Opt_Output_Http::XHTML, 'utf-8');
    $output->render($view);
Пример #9
0
 /**
  * Renders the view and sends the results to the browser. Please note
  * that in order to ensure that the script output will be a valid
  * XML document, this method can be called only once, for one XML view, forcing
  * you to modularize the templates with the template inheritance or
  * opt:include instruction.
  *
  * @param Opt_View $view The view object to be rendered.
  * @return Boolean True, if succeed.
  */
 public function render(Opt_View $view)
 {
     if ($this->_parser === null) {
         $this->_parser = $view->getParser();
         // Initialize output buffering and turn on the compression, if necessary.
         if (!$this->_tpl->debugConsole && $this->_tpl->gzipCompression == true && extension_loaded('zlib') && ini_get('zlib.output_compression') == 0) {
             ob_start('ob_gzhandler');
             ob_implicit_flush(0);
         } else {
             ob_start();
         }
         // Send the headers, if necessary
         $this->sendHeaders();
     } elseif ($this->_parser == 'Html' || $this->_parser == 'Xml') {
         throw new Opt_OutputOverloaded_Exception();
     }
     return $view->_parse($this, true);
 }
Пример #10
0
 /**
  * Finalizes everything.
  */
 protected function tearDown()
 {
     Opl_Registry::register('opt', null);
     Opt_View::clear();
     unset($this->tpl);
 }
Пример #11
0
 /**
  * Passes the OPT view to the widget.
  *
  * @param Opt_View $view The view.
  */
 public function setView(Opt_View $view)
 {
     $this->_view = $view;
     if ($this->_form === null) {
         $this->_form = $view->getTemplateVar('form');
         if ($this->_form === null) {
             throw new Opf_Exception('Item "form" not exists.');
         }
     }
 }
Пример #12
0
 /**
  * Registers the item wrappers in the template as a placeholder.
  *
  * @param Opt_View $view The view the form is rendered in
  * @internal
  */
 protected function _onRender(Opt_View $view)
 {
     $view->setFormat($this->_name, 'FormRepeater/Form');
 }
Пример #13
0
 /**
  * In this case, the result is generated directly by the caching engine, so
  * the original template should not be displayed.
  */
 public function testCacheProvidedAndUsed()
 {
     $view = new Opt_View('sample.tpl');
     $view->setCache(new activeCache());
     $output = new Opt_Output_Return();
     $this->assertEquals('CACHE-CHECK-START', $this->stripWs($output->render($view)));
 }
Пример #14
0
 /**
  * Clears all the possible static private buffers.
  */
 public static function clear()
 {
     self::$_vars = array();
     self::$_capture = array();
     self::$_global = array();
     self::$_globalFormatInfo = array();
 }
Пример #15
0
 /**
  * Executes the specified view and return the results back
  * to the script.
  *
  * @param Opt_View $view The rendered view
  * @return String
  */
 public function render(Opt_View $view)
 {
     ob_start();
     $view->_parse($this, true);
     return ob_get_clean();
 }
Пример #16
0
 /**
  * @covers Opt_View::assignGlobal
  * @covers Opt_View::getGlobal
  */
 public function testAssignGlobalVars()
 {
     Opt_View::assignGlobal('variable', 'Foo');
     $this->assertEquals('Foo', Opt_View::getGlobal('variable'));
 }