Example #1
0
 /**
  * @param QueryWrapperInterface $query
  * @return QueryWrapperInterface
  */
 public function queryFilter(QueryWrapperInterface $query)
 {
     if ($query instanceof QueryWrapperInterface === false) {
         $queryWrapperClass = Settings::getInstance()->getDefaultQueryWrapperClass();
         $query = new $queryWrapperClass($query);
     }
     return $query;
 }
Example #2
0
 /**
  * Format a string into title case
  *
  * @param string $string
  * @return string
  */
 public static function toTitleCase($string)
 {
     // Replace underscores, dashes with spaces
     $string = str_replace(["_", "-"], " ", $string);
     // Break the string into an array of words
     $name_array = explode(" ", $string);
     // Words not-to capitalize
     $smallWords = ['of', 'a', 'the', 'and', 'an', 'or', 'nor', 'but', 'is', 'if', 'then', 'else', 'when', 'at', 'from', 'by', 'on', 'off', 'for', 'in', 'out', 'over', 'to', 'into', 'with'];
     $acronyms = Settings::getInstance()->getAcronyms();
     foreach ($name_array as $index => $value) {
         if (in_array($value, $acronyms) === true) {
             $name_array[$index] = strtoupper($value);
         } elseif ($index === 0 || $index === sizeof($name_array) - 1) {
             $name_array[$index] = ucfirst($value);
         } elseif (in_array($value, $smallWords) === true) {
             // do nothing
         } else {
             $name_array[$index] = ucfirst($value);
         }
     }
     // Recombine the array into a single string, and convert to capital case
     $string = implode(" ", $name_array);
     return $string;
 }
 /**
  * @return string[] An array containing all Framework-registered Twig template directories.
  */
 protected function getTemplatesDirectories()
 {
     return array_merge(Settings::getInstance()->getTemplateDirectories(), [dirname(__FILE__) . '/../../templates']);
 }
Example #4
0
 /**
  * @return FilterInterface
  * @throws \Exception If an appropriate combination of fields have not been set.
  */
 public function build()
 {
     $this->validateId();
     $type = $this->retrieveOrException("type", __METHOD__);
     $statements = [];
     switch ($type) {
         case Filter::TYPE_STATIC:
             $fieldName = $this->retrieveOrException("fieldName", __METHOD__);
             $condition = $this->retrieveOrException("condition", __METHOD__);
             if (in_array($condition, [FilterStatementInterface::COND_SORT_ASC, FilterStatementInterface::COND_SORT_DESC]) === true) {
                 $criterion = $this->criterion;
                 $statements[] = new SortingFilterStatement($fieldName, $condition, $criterion, null);
             } else {
                 $criterion = $this->criterionHasBeenSet === true ? $this->criterion : $this->retrieveOrException("criterion", __METHOD__);
                 $statements[] = new ExcludingFilterStatement($fieldName, $condition, $criterion, null);
             }
             return new Filter($this->id, $this->classes, $this->data, $statements, $this->nextFilter);
             break;
         case Filter::TYPE_PAGINATION:
             $maxPerPage = $this->maxPerPage === null ? Settings::getInstance()->getDefaultPagination() : $this->maxPerPage;
             $page = $this->page === null ? FilterControls::getControl($this->id, "page", 1) : $this->page;
             return new PaginationFilter($this->id, $this->classes, $maxPerPage, $page, $this->nextFilter);
             break;
         case Filter::TYPE_SORT:
             return new SortFilter($this->id, $this->classes, $this->data, $this->nextFilter);
             break;
         case Filter::TYPE_SEARCH:
             return new SearchFilter($this->id, $this->classes, $this->data, $this->nextFilter);
             break;
         case Filter::TYPE_SELECT:
             $options = $this->retrieveOrException("options", __METHOD__, "chose to create a select filter");
             $default = $this->retrieveOrException("default", __METHOD__, "chose to create a select filter");
             if (array_key_exists($default, $options) === false) {
                 $optionsText = implode(", ", array_keys($options));
                 throw new \Exception("For select filter '{$this->id}', your default choice " . "'{$default}' must be among options '{$optionsText}'.");
             }
             $options = array_merge([" " . $default => $options[$default], " " => $options[$default]], $options);
             $statements = array_map(function ($option) {
                 return new ExcludingFilterStatement($option[0], $option[1], $option[2], null);
             }, $options);
             return new SelectFilter($this->id, $this->classes, $this->data, $statements, $default, $this->nextFilter);
             break;
         case Filter::TYPE_RELATION:
             $query = $this->retrieveOrException("query", __METHOD__, "chose to create a relation filter");
             $default = $this->retrieveOrException("default", __METHOD__, "chose to create a relation filter");
             return new RelationFilter($this->id, $this->classes, $this->data, $query, $default, $this->nextFilter);
             break;
         default:
             throw new \Exception("Invalid filter type.");
     }
 }
Example #5
0
 public function testBuildPaginationFilterUsesPaginateSetting()
 {
     $paginateBy = rand();
     $handle = (string) rand();
     $type = Filter::TYPE_PAGINATION;
     // Store the current default pagination
     $defaultPagination = Settings::getInstance()->getDefaultPagination();
     // Set our new pagination
     Settings::getInstance()->setDefaultPagination($paginateBy);
     $filter = FilterBuilder::begin()->setType($type)->setId($handle)->build();
     $this->assertEquals(1, sizeof($filter->getStatements()));
     $this->assertEquals($handle, $filter->getId());
     $this->assertTrue($filter instanceof PaginationFilter);
     $statement = $filter->getStatements()[0];
     $this->assertEquals(FilterStatement::COND_PAGINATE_BY, $statement->getCondition());
     $this->assertEquals($paginateBy, $statement->getCriterion());
     // Return the old default pagination
     Settings::getInstance()->setDefaultPagination($defaultPagination);
 }
Example #6
0
 /**
  * Render $page into html.
  *
  * This method is generally called via double-dispatch, as provided by Visitor\VisitableTrait.
  *
  * @param PageInterface $page
  * @return string
  */
 public function visitPage(PageInterface $page)
 {
     $template = 'page/' . $page->getType() . '.twig';
     return $this->loadTemplate($template)->render(["id" => $page->getId(), "classes" => $page->getClasses(), "data" => $page->getData(), "title" => $page->getTitle(), "baseHref" => $page->getBaseHref(), "writables" => $page->getWritables(), "projectCSS" => Settings::getInstance()->getProjectCSS(), "projectJS" => Settings::getInstance()->getProjectJS()]);
 }
Example #7
0
 /**
  * @return SettingsInterface;
  */
 protected function getSettingsInstance()
 {
     return $this->settingsInstance === null ? Settings::getInstance() : $this->getSettingsInstance();
 }
Example #8
0
<?php

require_once dirname(__FILE__) . "/../vendor/autoload.php";
use Athens\Core\Settings\Settings;
$settings = Settings::getInstance();
$settings->setDefaultObjectWrapperClass('Athens\\Core\\Test\\Mock\\MockObjectWrapper');
$settings->setDefaultQueryWrapperClass('Athens\\Core\\Test\\Mock\\MockQueryWrapper');
$_SERVER["REQUEST_URI"] = "http://example.com/";
Example #9
0
if (REPORT_ERRORS) {
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
}
// Initialize the session
session_start();
// Initialize CSRF Protection
CSRF::init();
// Setup Framework
Settings::getInstance()->addTemplateDirectories(dirname(__FILE__) . "/project-templates");
Settings::getInstance()->addAcronyms('ssn');
Cipher::createInstance(ATHENS_ENCRYPTION_PASSWORD);
// Setup Propel
$serviceContainer = Propel::getServiceContainer();
$serviceContainer->setAdapterClass(APPLICATION_NAME, 'mysql');
$manager = new ConnectionManagerSingle();
$manager->setConfiguration(array('dsn' => MYSQL_DSN, 'user' => MYSQL_USER, 'password' => MYSQL_PASSWORD));
$serviceContainer->setConnectionManager(APPLICATION_NAME, $manager);
// Include project-specific CSS
$fullCSSFilesnames = glob(dirname(__FILE__) . "/project-assets/css/*.css");
$relativeCSSFilesnames = str_replace(dirname(__FILE__) . "/", "", $fullCSSFilesnames);
foreach ($relativeCSSFilesnames as $file) {
    Settings::getInstance()->addProjectCSS($file);
}
// Include project-specific JS
$fullJSFilesnames = glob(dirname(__FILE__) . "/project-assets/js/*.js");
$relativeJSFilesnames = str_replace(dirname(__FILE__) . "/", "", $fullJSFilesnames);
foreach ($relativeJSFilesnames as $file) {
    Settings::getInstance()->addProjectJS($file);
}
Example #10
0
 public function testVisitPage()
 {
     $writer = new HTMLWriter();
     $pageType = PageBuilder::TYPE_FULL_HEADER;
     $pageHeader = "Page Header";
     $pageSubHeader = "Page subheader";
     $id = "p" . (string) rand();
     $classes = [(string) rand(), (string) rand()];
     $data = ['d' . (string) rand() => (string) rand(), 'd' . (string) rand() => (string) rand()];
     $breadCrumbs = ['t' . (string) rand() => 'u' . (string) rand()];
     $section = SectionBuilder::begin()->setId("s" . (string) rand())->addLabel("Label")->addContent("Some content.")->build();
     $page = PageBuilder::begin()->setId($id)->addClass($classes[0])->addClass($classes[1])->addWritable($section)->addData(array_keys($data)[0], array_values($data)[0])->addData(array_keys($data)[1], array_values($data)[1])->setHeader($pageHeader)->setSubHeader($pageSubHeader)->setType($pageType)->setTitle("Page Title")->setBaseHref(".")->addBreadCrumb(array_keys($breadCrumbs)[0], array_values($breadCrumbs)[0])->build();
     // Add project CSS and JS
     $cssFile1 = "/path/to/file/1.css";
     $cssFile2 = "/path/to/file/2.css";
     $jsFile1 = "/path/to/file/1.js";
     $jsFile2 = "/path/to/file/2.js";
     Settings::getInstance()->addProjectCSS($cssFile1);
     Settings::getInstance()->addProjectCSS($cssFile2);
     Settings::getInstance()->addProjectJS($jsFile1);
     Settings::getInstance()->addProjectJS($jsFile2);
     // Provide a request URI, for the page's hash function
     $requestURI = (string) rand();
     $_SERVER["REQUEST_URI"] = $requestURI;
     // Get result and strip quotes, for easier analysis
     $result = $this->stripQuotes($writer->visitPage($page));
     $this->assertContains("<html>", $result);
     $this->assertContains("<head>", $result);
     $this->assertContains("<title>Page Title</title>", $result);
     $this->assertContains("<base href=.", $result);
     $this->assertContains("</head>", $result);
     $this->assertContains("<body id={$id}", $result);
     $this->assertContains("class=" . implode(" ", $classes), $result);
     $this->assertContains("data-" . array_keys($data)[0] . "=" . array_values($data)[0], $result);
     $this->assertContains("data-" . array_keys($data)[1] . "=" . array_values($data)[1], $result);
     $this->assertContains("href=" . array_values($breadCrumbs)[0] . ">" . array_keys($breadCrumbs)[0] . "</a>", $result);
     $this->assertRegExp("/h1.*class=header.*{$pageHeader}.*h1/s", $result);
     $this->assertRegExp("/h2.*class=subheader.*{$pageSubHeader}.*h2/s", $result);
     $this->assertContains("<div class=section-label  >Label</div>", $result);
     $this->assertContains($cssFile1, $result);
     $this->assertContains($cssFile2, $result);
     $this->assertContains($jsFile1, $result);
     $this->assertContains($jsFile2, $result);
 }