From 40005e0c0699dd120e672ad395723a3c005264db Mon Sep 17 00:00:00 2001 From: Hector Date: Mon, 6 Jan 2014 22:11:32 +0100 Subject: [PATCH] Version 2.24, released 06/01/2014. [Refactor] - Mayor change. After intense struggles and researches, decided to use PHP ActiveRecord. Because of this, deleted all Database and Model related constructions used so far. I am aiming to implement my own PHP ActiveRecord technology for the third Hecnel Framework version. [Feature] - Test 4 is designed to be the testing workspace for multiple model work. However, as PHP ActiveRecord comes to play, it is halfway implemented. To be reworked in future. --- application/config/system.php | 7 +- application/controllers/releaseLog.php | 10 +- application/controllers/test3.php | 69 +- application/controllers/test4.php | 160 ++ application/engine/Database.php | 29 - application/engine/Model.php | 23 - application/models/Data.php | 17 +- application/models/Dog.php | 31 +- application/models/User.php | 52 +- application/services/DashboardService.php | 21 +- application/services/LoginService.php | 18 +- .../services/UsersManagementService.php | 52 +- application/views/general/top.php | 2 +- application/views/tests/test3/index.php | 15 +- application/views/tests/test4/css/test4.css | 45 + application/views/tests/test4/index.php | 68 +- application/views/tests/test4/js/test4.js | 138 ++ engine/Database.php | 102 - engine/DatabaseOptions.php | 16 - engine/Model.php | 227 -- engine/ModelCollection.php | 20 - engine/drivers/Databases/MockDatabase.php | 130 -- engine/drivers/Databases/MySqlDatabase.php | 332 --- .../drivers/Exceptions/DatabaseException.php | 24 - engine/drivers/Exceptions/ModelException.php | 23 - engine/php-activerecord/.gitignore | 8 + engine/php-activerecord/.travis.yml | 16 + engine/php-activerecord/ActiveRecord.php | 50 + engine/php-activerecord/CHANGELOG | 25 + engine/php-activerecord/LICENSE | 23 + engine/php-activerecord/README.md | 166 ++ engine/php-activerecord/composer.json | 24 + .../examples/orders/models/Order.php | 37 + .../examples/orders/models/Payment.php | 9 + .../examples/orders/models/Person.php | 13 + .../examples/orders/orders.php | 79 + .../examples/orders/orders.sql | 29 + .../examples/simple/simple.php | 17 + .../examples/simple/simple.sql | 7 + .../examples/simple/simple_with_options.php | 32 + .../examples/simple/simple_with_options.sql | 6 + engine/php-activerecord/lib/Cache.php | 80 + engine/php-activerecord/lib/CallBack.php | 252 +++ engine/php-activerecord/lib/Column.php | 155 ++ engine/php-activerecord/lib/Config.php | 304 +++ engine/php-activerecord/lib/Connection.php | 536 +++++ .../lib/ConnectionManager.php | 51 + engine/php-activerecord/lib/DateTime.php | 151 ++ engine/php-activerecord/lib/Exceptions.php | 137 ++ engine/php-activerecord/lib/Expressions.php | 185 ++ engine/php-activerecord/lib/Inflector.php | 120 ++ engine/php-activerecord/lib/Model.php | 1862 +++++++++++++++++ engine/php-activerecord/lib/Reflections.php | 86 + engine/php-activerecord/lib/Relationship.php | 729 +++++++ engine/php-activerecord/lib/SQLBuilder.php | 423 ++++ engine/php-activerecord/lib/Serialization.php | 372 ++++ engine/php-activerecord/lib/Singleton.php | 57 + engine/php-activerecord/lib/Table.php | 555 +++++ engine/php-activerecord/lib/Utils.php | 366 ++++ engine/php-activerecord/lib/Validations.php | 912 ++++++++ .../lib/adapters/MysqlAdapter.php | 99 + .../lib/adapters/OciAdapter.php | 146 ++ .../lib/adapters/PgsqlAdapter.php | 139 ++ .../lib/adapters/SqliteAdapter.php | 113 + .../php-activerecord/lib/cache/Memcache.php | 45 + engine/php-activerecord/phpunit.xml | 2 + .../test/ActiveRecordCacheTest.php | 44 + .../test/ActiveRecordFindTest.php | 468 +++++ .../test/ActiveRecordTest.php | 566 +++++ .../test/ActiveRecordWriteTest.php | 427 ++++ engine/php-activerecord/test/CacheTest.php | 82 + engine/php-activerecord/test/CallbackTest.php | 294 +++ engine/php-activerecord/test/ColumnTest.php | 121 ++ engine/php-activerecord/test/ConfigTest.php | 95 + .../test/ConnectionManagerTest.php | 39 + .../php-activerecord/test/ConnectionTest.php | 80 + .../php-activerecord/test/DateFormatTest.php | 18 + engine/php-activerecord/test/DateTimeTest.php | 127 ++ .../php-activerecord/test/ExpressionsTest.php | 201 ++ .../test/HasManyThroughTest.php | 60 + .../php-activerecord/test/InflectorTest.php | 28 + .../test/ModelCallbackTest.php | 97 + .../test/MysqlAdapterTest.php | 37 + .../php-activerecord/test/OciAdapterTest.php | 46 + .../test/PgsqlAdapterTest.php | 44 + .../test/RelationshipTest.php | 730 +++++++ .../php-activerecord/test/SQLBuilderTest.php | 282 +++ .../test/SerializationTest.php | 215 ++ .../test/SqliteAdapterTest.php | 75 + engine/php-activerecord/test/UtilsTest.php | 107 + .../test/ValidatesFormatOfTest.php | 111 + .../ValidatesInclusionAndExclusionOfTest.php | 157 ++ .../test/ValidatesLengthOfTest.php | 335 +++ .../test/ValidatesNumericalityOfTest.php | 165 ++ .../test/ValidatesPresenceOfTest.php | 74 + .../php-activerecord/test/ValidationsTest.php | 182 ++ .../test/fixtures/amenities.csv | 4 + .../test/fixtures/authors.csv | 5 + .../test/fixtures/awesome_people.csv | 4 + .../php-activerecord/test/fixtures/books.csv | 3 + .../test/fixtures/employees.csv | 4 + .../php-activerecord/test/fixtures/events.csv | 7 + .../php-activerecord/test/fixtures/hosts.csv | 5 + .../test/fixtures/newsletters.csv | 2 + .../test/fixtures/positions.csv | 4 + .../test/fixtures/property.csv | 3 + .../test/fixtures/property_amenities.csv | 5 + .../test/fixtures/rm-bldg.csv | 2 + .../test/fixtures/user_newsletters.csv | 2 + .../php-activerecord/test/fixtures/users.csv | 2 + .../test/fixtures/valuestore.csv | 0 .../php-activerecord/test/fixtures/venues.csv | 7 + .../test/helpers/AdapterTest.php | 411 ++++ .../test/helpers/DatabaseLoader.php | 130 ++ .../test/helpers/DatabaseTest.php | 81 + .../SnakeCase_PHPUnit_Framework_TestCase.php | 64 + .../php-activerecord/test/helpers/config.php | 79 + engine/php-activerecord/test/helpers/foo.php | 28 + .../php-activerecord/test/models/Amenity.php | 11 + .../php-activerecord/test/models/Author.php | 29 + .../test/models/AuthorAttrAccessible.php | 17 + .../test/models/AwesomePerson.php | 6 + engine/php-activerecord/test/models/Book.php | 36 + .../test/models/BookAttrAccessible.php | 10 + .../test/models/BookAttrProtected.php | 13 + .../php-activerecord/test/models/Employee.php | 6 + engine/php-activerecord/test/models/Event.php | 14 + engine/php-activerecord/test/models/Host.php | 9 + .../test/models/JoinAuthor.php | 7 + .../php-activerecord/test/models/JoinBook.php | 8 + .../test/models/NamespaceTest/Book.php | 17 + .../NamespaceTest/SubNamespaceTest/Page.php | 10 + .../php-activerecord/test/models/Position.php | 6 + .../php-activerecord/test/models/Property.php | 12 + .../test/models/PropertyAmenity.php | 12 + .../php-activerecord/test/models/RmBldg.php | 33 + engine/php-activerecord/test/models/Venue.php | 37 + .../test/models/VenueAfterCreate.php | 16 + .../php-activerecord/test/models/VenueCB.php | 44 + engine/php-activerecord/test/sql/mysql.sql | 107 + .../test/sql/oci-after-fixtures.sql | 35 + engine/php-activerecord/test/sql/oci.sql | 97 + .../test/sql/pgsql-after-fixtures.sql | 16 + engine/php-activerecord/test/sql/pgsql.sql | 109 + engine/php-activerecord/test/sql/sqlite.sql | 106 + index.php | 20 +- 146 files changed, 15920 insertions(+), 1131 deletions(-) delete mode 100644 application/engine/Database.php delete mode 100644 application/engine/Model.php create mode 100644 application/views/tests/test4/css/test4.css create mode 100644 application/views/tests/test4/js/test4.js delete mode 100644 engine/Database.php delete mode 100644 engine/DatabaseOptions.php delete mode 100644 engine/Model.php delete mode 100644 engine/ModelCollection.php delete mode 100644 engine/drivers/Databases/MockDatabase.php delete mode 100644 engine/drivers/Databases/MySqlDatabase.php delete mode 100644 engine/drivers/Exceptions/DatabaseException.php delete mode 100644 engine/drivers/Exceptions/ModelException.php create mode 100644 engine/php-activerecord/.gitignore create mode 100644 engine/php-activerecord/.travis.yml create mode 100644 engine/php-activerecord/ActiveRecord.php create mode 100644 engine/php-activerecord/CHANGELOG create mode 100644 engine/php-activerecord/LICENSE create mode 100644 engine/php-activerecord/README.md create mode 100644 engine/php-activerecord/composer.json create mode 100644 engine/php-activerecord/examples/orders/models/Order.php create mode 100644 engine/php-activerecord/examples/orders/models/Payment.php create mode 100644 engine/php-activerecord/examples/orders/models/Person.php create mode 100644 engine/php-activerecord/examples/orders/orders.php create mode 100644 engine/php-activerecord/examples/orders/orders.sql create mode 100644 engine/php-activerecord/examples/simple/simple.php create mode 100644 engine/php-activerecord/examples/simple/simple.sql create mode 100644 engine/php-activerecord/examples/simple/simple_with_options.php create mode 100644 engine/php-activerecord/examples/simple/simple_with_options.sql create mode 100644 engine/php-activerecord/lib/Cache.php create mode 100644 engine/php-activerecord/lib/CallBack.php create mode 100644 engine/php-activerecord/lib/Column.php create mode 100644 engine/php-activerecord/lib/Config.php create mode 100644 engine/php-activerecord/lib/Connection.php create mode 100644 engine/php-activerecord/lib/ConnectionManager.php create mode 100644 engine/php-activerecord/lib/DateTime.php create mode 100644 engine/php-activerecord/lib/Exceptions.php create mode 100644 engine/php-activerecord/lib/Expressions.php create mode 100644 engine/php-activerecord/lib/Inflector.php create mode 100644 engine/php-activerecord/lib/Model.php create mode 100644 engine/php-activerecord/lib/Reflections.php create mode 100644 engine/php-activerecord/lib/Relationship.php create mode 100644 engine/php-activerecord/lib/SQLBuilder.php create mode 100644 engine/php-activerecord/lib/Serialization.php create mode 100644 engine/php-activerecord/lib/Singleton.php create mode 100644 engine/php-activerecord/lib/Table.php create mode 100644 engine/php-activerecord/lib/Utils.php create mode 100644 engine/php-activerecord/lib/Validations.php create mode 100644 engine/php-activerecord/lib/adapters/MysqlAdapter.php create mode 100644 engine/php-activerecord/lib/adapters/OciAdapter.php create mode 100644 engine/php-activerecord/lib/adapters/PgsqlAdapter.php create mode 100644 engine/php-activerecord/lib/adapters/SqliteAdapter.php create mode 100644 engine/php-activerecord/lib/cache/Memcache.php create mode 100644 engine/php-activerecord/phpunit.xml create mode 100644 engine/php-activerecord/test/ActiveRecordCacheTest.php create mode 100644 engine/php-activerecord/test/ActiveRecordFindTest.php create mode 100644 engine/php-activerecord/test/ActiveRecordTest.php create mode 100644 engine/php-activerecord/test/ActiveRecordWriteTest.php create mode 100644 engine/php-activerecord/test/CacheTest.php create mode 100644 engine/php-activerecord/test/CallbackTest.php create mode 100644 engine/php-activerecord/test/ColumnTest.php create mode 100644 engine/php-activerecord/test/ConfigTest.php create mode 100644 engine/php-activerecord/test/ConnectionManagerTest.php create mode 100644 engine/php-activerecord/test/ConnectionTest.php create mode 100644 engine/php-activerecord/test/DateFormatTest.php create mode 100644 engine/php-activerecord/test/DateTimeTest.php create mode 100644 engine/php-activerecord/test/ExpressionsTest.php create mode 100644 engine/php-activerecord/test/HasManyThroughTest.php create mode 100644 engine/php-activerecord/test/InflectorTest.php create mode 100644 engine/php-activerecord/test/ModelCallbackTest.php create mode 100644 engine/php-activerecord/test/MysqlAdapterTest.php create mode 100644 engine/php-activerecord/test/OciAdapterTest.php create mode 100644 engine/php-activerecord/test/PgsqlAdapterTest.php create mode 100644 engine/php-activerecord/test/RelationshipTest.php create mode 100644 engine/php-activerecord/test/SQLBuilderTest.php create mode 100644 engine/php-activerecord/test/SerializationTest.php create mode 100644 engine/php-activerecord/test/SqliteAdapterTest.php create mode 100644 engine/php-activerecord/test/UtilsTest.php create mode 100644 engine/php-activerecord/test/ValidatesFormatOfTest.php create mode 100644 engine/php-activerecord/test/ValidatesInclusionAndExclusionOfTest.php create mode 100644 engine/php-activerecord/test/ValidatesLengthOfTest.php create mode 100644 engine/php-activerecord/test/ValidatesNumericalityOfTest.php create mode 100644 engine/php-activerecord/test/ValidatesPresenceOfTest.php create mode 100644 engine/php-activerecord/test/ValidationsTest.php create mode 100644 engine/php-activerecord/test/fixtures/amenities.csv create mode 100644 engine/php-activerecord/test/fixtures/authors.csv create mode 100644 engine/php-activerecord/test/fixtures/awesome_people.csv create mode 100644 engine/php-activerecord/test/fixtures/books.csv create mode 100644 engine/php-activerecord/test/fixtures/employees.csv create mode 100644 engine/php-activerecord/test/fixtures/events.csv create mode 100644 engine/php-activerecord/test/fixtures/hosts.csv create mode 100644 engine/php-activerecord/test/fixtures/newsletters.csv create mode 100644 engine/php-activerecord/test/fixtures/positions.csv create mode 100644 engine/php-activerecord/test/fixtures/property.csv create mode 100644 engine/php-activerecord/test/fixtures/property_amenities.csv create mode 100644 engine/php-activerecord/test/fixtures/rm-bldg.csv create mode 100644 engine/php-activerecord/test/fixtures/user_newsletters.csv create mode 100644 engine/php-activerecord/test/fixtures/users.csv create mode 100644 engine/php-activerecord/test/fixtures/valuestore.csv create mode 100644 engine/php-activerecord/test/fixtures/venues.csv create mode 100644 engine/php-activerecord/test/helpers/AdapterTest.php create mode 100644 engine/php-activerecord/test/helpers/DatabaseLoader.php create mode 100644 engine/php-activerecord/test/helpers/DatabaseTest.php create mode 100644 engine/php-activerecord/test/helpers/SnakeCase_PHPUnit_Framework_TestCase.php create mode 100644 engine/php-activerecord/test/helpers/config.php create mode 100644 engine/php-activerecord/test/helpers/foo.php create mode 100644 engine/php-activerecord/test/models/Amenity.php create mode 100644 engine/php-activerecord/test/models/Author.php create mode 100644 engine/php-activerecord/test/models/AuthorAttrAccessible.php create mode 100644 engine/php-activerecord/test/models/AwesomePerson.php create mode 100644 engine/php-activerecord/test/models/Book.php create mode 100644 engine/php-activerecord/test/models/BookAttrAccessible.php create mode 100644 engine/php-activerecord/test/models/BookAttrProtected.php create mode 100644 engine/php-activerecord/test/models/Employee.php create mode 100644 engine/php-activerecord/test/models/Event.php create mode 100644 engine/php-activerecord/test/models/Host.php create mode 100644 engine/php-activerecord/test/models/JoinAuthor.php create mode 100644 engine/php-activerecord/test/models/JoinBook.php create mode 100644 engine/php-activerecord/test/models/NamespaceTest/Book.php create mode 100644 engine/php-activerecord/test/models/NamespaceTest/SubNamespaceTest/Page.php create mode 100644 engine/php-activerecord/test/models/Position.php create mode 100644 engine/php-activerecord/test/models/Property.php create mode 100644 engine/php-activerecord/test/models/PropertyAmenity.php create mode 100644 engine/php-activerecord/test/models/RmBldg.php create mode 100644 engine/php-activerecord/test/models/Venue.php create mode 100644 engine/php-activerecord/test/models/VenueAfterCreate.php create mode 100644 engine/php-activerecord/test/models/VenueCB.php create mode 100644 engine/php-activerecord/test/sql/mysql.sql create mode 100644 engine/php-activerecord/test/sql/oci-after-fixtures.sql create mode 100644 engine/php-activerecord/test/sql/oci.sql create mode 100644 engine/php-activerecord/test/sql/pgsql-after-fixtures.sql create mode 100644 engine/php-activerecord/test/sql/pgsql.sql create mode 100644 engine/php-activerecord/test/sql/sqlite.sql diff --git a/application/config/system.php b/application/config/system.php index 39c3a88..a01dc9c 100644 --- a/application/config/system.php +++ b/application/config/system.php @@ -27,4 +27,9 @@ define ('_DEFAULT_CONTROLLER', 'index'); define ('_DEFAULT_METHOD', 'index'); define ('_ERROR_CONTROLLER', 'Error'); -define ('_EXCEPTION_METHOD', 'exception'); \ No newline at end of file +define ('_EXCEPTION_METHOD', 'exception'); + +/** + * Config required for ActiveRecord. It disables its autoloading function. + */ +define ('PHP_ACTIVERECORD_AUTOLOAD_DISABLE', true); \ No newline at end of file diff --git a/application/controllers/releaseLog.php b/application/controllers/releaseLog.php index 9a5762f..936c9e1 100644 --- a/application/controllers/releaseLog.php +++ b/application/controllers/releaseLog.php @@ -31,12 +31,16 @@ public function __construct() parent::__construct(); // Setting version under construction - $this->_setDevelopmentVersion('2.23', '04/01/2014', array( - '[Refactor] - Modified libraries concept to Service concepts. A Service is tightly related to a controller. It stores intelligence regarding what to do with validated information, which models request a CRUD, and informs the controller about the message to put into the related view.', - '[Refactor] - Modified Hecnel Framework in order to use the new Model architecture.' + $this->_setDevelopmentVersion('2.24', '06/01/2014', array( + '[Refactor] - Mayor change. After intense struggles and researches, decided to use PHP ActiveRecord. Because of this, deleted all Database and Model related constructions used so far. I am aiming to implement my own PHP ActiveRecord technology for the third Hecnel Framework version.', + '[Feature] - Test 4 is designed to be the testing workspace for multiple model work. However, as PHP ActiveRecord comes to play, it is halfway implemented. To be reworked in future.', )); // Setting Historical Log of releases + $this->_addHistoryLog('2.23', '04/01/2014', array( + '[Refactor] - Modified libraries concept to Service concepts. A Service is tightly related to a controller. It stores intelligence regarding what to do with validated information, which models request a CRUD, and informs the controller about the message to put into the related view.', + '[Refactor] - Modified Hecnel Framework in order to use the new Model architecture.' + )); $this->_addHistoryLog('2.22', '04/01/2014', array( '[Refactor] - Mayor change in Model architecture. Now all models extend the same Model object, which handles all commands that a Model offers and instantiates and communicates with the Database.', '[Refactor] - Major change in Database architecture. Now it is allowed to have different storing systems.', diff --git a/application/controllers/test3.php b/application/controllers/test3.php index 202c507..785fdf1 100644 --- a/application/controllers/test3.php +++ b/application/controllers/test3.php @@ -12,7 +12,6 @@ use application\engine\Controller; use application\models\Dog; use engine\Input; -use engine\Model; use engine\drivers\Exceptions\ModelException; use engine\drivers\Exceptions\RuleException; @@ -58,14 +57,14 @@ public function makeADog() $inputDogName->validate(); $inputDogAge->validate(); $inputDogBreed->validate(); + + $dog = Dog::create(array( + 'name' => $inputDogName->getValue(), + 'age' => $inputDogAge->getValue(), + 'breed' => $inputDogBreed->getValue() + )); - $dog = new Dog(); - $dog->name = $inputDogName->getValue(); - $dog->age = $inputDogAge->getValue(); - $dog->breed = $inputDogBreed->getValue(); - $dog->save(); - - echo json_encode(array('id' => $dog->getId())); + echo json_encode(array('id' => $dog->id)); } catch (RuleException $rEx) { header("HTTP/1.1 400 Could not make a dog."); echo json_encode(array('errorMessage' => $rEx->getMessage())); @@ -77,24 +76,8 @@ public function makeADog() */ public function getADog() { - try { - $dog = new Dog(); - $dog->find(); - } catch (ModelException $mEx) { - switch ($mEx->getCode()) { - case Model::ERR_RECORDS_FOUND: - header("HTTP/1.1 400 Could not find a dog."); - echo json_encode(array('errorMessage' => 'Could not find a dog.')); - exit; - break; - default: - header("HTTP/1.1 400 Could not make a dog."); - echo json_encode(array('errorMessage' => 'Unexpected error: ' . $mEx->getMessage())); - exit; - } - } - - echo json_encode($dog->toArray()); + $dog = Dog::first(); + echo json_encode($dog->attributes()); } /** @@ -125,9 +108,9 @@ public function changeADog() $inputDogName->validate(); $inputDogAge->validate(); $inputDogBreed->validate(); - - $dog = new Dog(); - $dog->find(array('id' => $inputDogId->getValue())); + + $dog = Dog::find(array('id'=> $inputDogId->getValue())); + $dog->name = $inputDogName->getValue(); $dog->age = $inputDogAge->getValue(); $dog->breed = $inputDogBreed->getValue(); @@ -136,20 +119,7 @@ public function changeADog() } catch (RuleException $rEx) { header("HTTP/1.1 400 Could not change a dog."); echo json_encode(array('errorMessage' => $rEx->getMessage())); - } catch (ModelException $mEx) { - switch ($mEx->getCode()) { - case Model::ERR_RECORDS_FOUND: - header("HTTP/1.1 400 Dog does not exist."); - echo json_encode(array('errorMessage' => 'Dog you are trying to change does not exist.')); - exit; - break; - default: - header("HTTP/1.1 400 Could not change a dog."); - echo json_encode(array('errorMessage' => 'Unexpected error: ' . $mEx->getMessage())); - exit; - } } - } /** @@ -164,25 +134,12 @@ public function sayByeToADog() try { $inputDogId->validate(); - $dog = new Dog(); - $dog->find(array('id' => $inputDogId->getValue())); + $dog = Dog::find(array('id' => $inputDogId->getValue())); $dog->delete(); } catch (RuleException $rEx) { header("HTTP/1.1 400 Could not delete a dog."); echo json_encode(array('errorMessage' => $rEx->getMessage())); - } catch (ModelException $mEx) { - switch ($mEx->getCode()) { - case Model::ERR_RECORDS_FOUND: - header("HTTP/1.1 400 Dog does not exist."); - echo json_encode(array('errorMessage' => 'Dog you are trying to delete does not exist.')); - exit; - break; - default: - header("HTTP/1.1 400 Could not delete a dog."); - echo json_encode(array('errorMessage' => 'Unexpected error: ' . $mEx->getMessage())); - exit; - } } } } \ No newline at end of file diff --git a/application/controllers/test4.php b/application/controllers/test4.php index d11a52c..fbf76ec 100644 --- a/application/controllers/test4.php +++ b/application/controllers/test4.php @@ -10,6 +10,11 @@ namespace application\controllers; use application\engine\Controller; +use application\models\Dog; +use engine\Input; +use engine\ModelCollection; +use engine\drivers\Exceptions\ModelException; +use engine\drivers\Exceptions\RuleException; class Test4 extends Controller { @@ -23,6 +28,161 @@ public function __construct() public function index() { + $this->_view->addLibrary('js', 'application/views/tests/test4/js/test4.js'); + $this->_view->addLibrary('css', 'application/views/tests/test4/css/test4.css'); + $this->_view->addChunk('tests/test4/index'); } + + /** + * CRUD - Create + */ + public function createDogs() + { + $inputDogName = Input::build('Text', 'dogName') + ->addRule('minLength', 3) + ->addRule('maxLength', 12); + $inputDogAge = Input::build('Number', 'dogAge') + ->addRule('min', 0) + ->addRule('max', 100) + ->addRule('isInt'); + $inputDogBreed = Input::build('Select', 'dogBreed') + ->addRule('availableOptions', array( + 'germanSheppard', + 'siberianHusky', + 'biggles', + 'borderCollie' + )); + + try { + $inputDogName->validate(); + $inputDogAge->validate(); + $inputDogBreed->validate(); + + $dogCollection = new ModelCollection(new Dog); + $dogCollection-> + $dog->name = $inputDogName->getValue(); + $dog->age = $inputDogAge->getValue(); + $dog->breed = $inputDogBreed->getValue(); + $dog->save(); + + echo json_encode(array('id' => $dog->getId())); + } catch (RuleException $rEx) { + header("HTTP/1.1 400 Could not make a dog."); + echo json_encode(array('errorMessage' => $rEx->getMessage())); + } + } + + /** + * CRUD - Read + */ + public function getDoggies() + { + try { + $dog = new Dog(); + $dog->find(); + } catch (ModelException $mEx) { + switch ($mEx->getCode()) { + case Dog::ERR_RECORDS_FOUND: + header("HTTP/1.1 400 Could not find a dog."); + echo json_encode(array('errorMessage' => 'Could not find a dog.')); + exit; + break; + default: + header("HTTP/1.1 400 Could not make a dog."); + echo json_encode(array('errorMessage' => 'Unexpected error: ' . $mEx->getMessage())); + exit; + } + } + + echo json_encode($dog->toArray()); + } + + /** + * CRUD - Update + */ + public function changeDogs() + { + $inputDogId = Input::build('Number', 'dogId') + ->addRule('min', 1) + ->addRule('isInt'); + $inputDogName = Input::build('Text', 'dogName') + ->addRule('minLength', 3) + ->addRule('maxLength', 12); + $inputDogAge = Input::build('Number', 'dogAge') + ->addRule('min', 0) + ->addRule('max', 100) + ->addRule('isInt'); + $inputDogBreed = Input::build('Select', 'dogBreed') + ->addRule('availableOptions', array( + 'germanSheppard', + 'siberianHusky', + 'biggles', + 'borderCollie' + )); + + try { + $inputDogId->validate(); + $inputDogName->validate(); + $inputDogAge->validate(); + $inputDogBreed->validate(); + + $dog = new Dog(); + $dog->find(array('id' => $inputDogId->getValue())); + $dog->name = $inputDogName->getValue(); + $dog->age = $inputDogAge->getValue(); + $dog->breed = $inputDogBreed->getValue(); + $dog->save(); + + } catch (RuleException $rEx) { + header("HTTP/1.1 400 Could not change a dog."); + echo json_encode(array('errorMessage' => $rEx->getMessage())); + } catch (ModelException $mEx) { + switch ($mEx->getCode()) { + case Dog::ERR_RECORDS_FOUND: + header("HTTP/1.1 400 Dog does not exist."); + echo json_encode(array('errorMessage' => 'Dog you are trying to change does not exist.')); + exit; + break; + default: + header("HTTP/1.1 400 Could not change a dog."); + echo json_encode(array('errorMessage' => 'Unexpected error: ' . $mEx->getMessage())); + exit; + } + } + } + + /** + * CRUD - Delete + */ + public function dogMassacre() + { + $inputDogId = Input::build('Number', 'dogId') + ->addRule('min', 1) + ->addRule('isInt'); + + try { + $inputDogId->validate(); + + $dog = new Dog(); + $dog->find(array('id' => $inputDogId->getValue())); + $dog->delete(); + + } catch (RuleException $rEx) { + header("HTTP/1.1 400 Could not delete a dog."); + echo json_encode(array('errorMessage' => $rEx->getMessage())); + } catch (ModelException $mEx) { + switch ($mEx->getCode()) { + case Dog::ERR_RECORDS_FOUND: + header("HTTP/1.1 400 Dog does not exist."); + echo json_encode(array('errorMessage' => 'Dog you are trying to delete does not exist.')); + exit; + break; + default: + header("HTTP/1.1 400 Could not delete a dog."); + echo json_encode(array('errorMessage' => 'Unexpected error: ' . $mEx->getMessage())); + exit; + } + } + } } \ No newline at end of file diff --git a/application/engine/Database.php b/application/engine/Database.php deleted file mode 100644 index 4f6b292..0000000 --- a/application/engine/Database.php +++ /dev/null @@ -1,29 +0,0 @@ -setModelName('data'); - - parent::__construct(); - - $this->addField('data'); - } + public static $table_name = 'data'; // Table name } \ No newline at end of file diff --git a/application/models/Dog.php b/application/models/Dog.php index 2bc0eed..9500eb9 100644 --- a/application/models/Dog.php +++ b/application/models/Dog.php @@ -9,7 +9,7 @@ namespace application\models; -use application\engine\Model; +use \ActiveRecord\Model as Model; /** * Class Dog @@ -17,32 +17,5 @@ */ class Dog extends Model { - /** - * Dog name. - * @var string $name - */ - public $name; - - /** - * Dog age. - * @var int $age - */ - public $age; - - /** - * Dog breed. - * @var string $breed - */ - public $breed; - - public function __construct() - { - $this->setModelName('dog'); - - parent::__construct(); - - $this->addField('name'); - $this->addField('age'); - $this->addField('breed'); - } + public static $table_name = 'dog'; // Table name } \ No newline at end of file diff --git a/application/models/User.php b/application/models/User.php index e9c1d78..0e80d07 100644 --- a/application/models/User.php +++ b/application/models/User.php @@ -8,57 +8,9 @@ namespace application\models; -use application\engine\Model; -use engine\Encrypter; +use \ActiveRecord\Model as Model; class User extends Model { - /** - * User name. - * @var string $name - */ - public $name; - - /** - * User password. - * It is protected as it is not allowed direct definition. Password has to be set through its setter, which applies Encryption. - * @var string $password - */ - protected $password; - - /** - * User role. - * @var string $role - */ - public $role; - - public function __construct() - { - $this->setModelName('user'); - - parent::__construct(); - - $this->addField('name'); - $this->addField('password'); - $this->addField('role'); - } - - /** - * @param string $password - */ - public function setPassword($password) - { - $this->password = Encrypter::encrypt($password); - } - - /** - * Verifies that the passed password matches with User's. - * - * @param string $password - * @return bool Whether this User name and password matches - */ - public function verify($password) - { - return Encrypter::verify($password, $this->password); - } + public static $table_name = 'user'; // Table name } \ No newline at end of file diff --git a/application/services/DashboardService.php b/application/services/DashboardService.php index 02d1c3e..bfd0951 100644 --- a/application/services/DashboardService.php +++ b/application/services/DashboardService.php @@ -20,11 +20,9 @@ public function __construct() public function ajaxInsert($data) { - $dataModel = new Data(); - $dataModel->data = $data; - $dataModel->save(); + $data = Data::create(array('data' => $data)); - return $dataModel->toArray(); + return $data->attributes(); } /** @@ -33,16 +31,19 @@ public function ajaxInsert($data) */ public function getListings() { - $dataModel = new Data(); - $dataModel->find(); + $dataList = Data::find('all'); - return array($dataModel->toArray()); + $dataArray = array(); + foreach ($dataList as $data) { + $dataArray[] = array('id' => $data->id, 'data' => $data->data); + } + + return $dataArray; } public function deleteData($dataId) { - $dataModel = new Data(); - $dataModel->find(array('id' => $dataId)); - $dataModel->delete(); + $data = Data::find(array('id' => $dataId)); + $data->delete(); } } \ No newline at end of file diff --git a/application/services/LoginService.php b/application/services/LoginService.php index 134c777..0631e70 100644 --- a/application/services/LoginService.php +++ b/application/services/LoginService.php @@ -10,6 +10,7 @@ use application\engine\Service; use application\models\User; +use engine\Encrypter; use engine\Session; use engine\drivers\Exceptions\ModelException; @@ -30,10 +31,14 @@ public function __construct() public function login($name, $password) { try { - $user = new User(); - $user->find(array('name' => $name)); - - if ($user->verify($password)) { + $user = User::find_by_name($name); + + if (is_null($user)) + { + return false; + } + + if ($this->verifyUser($user, $password)) { Session::set('isUserLoggedIn', true); Session::set('userName', $user->name); Session::set('userRole', $user->role); @@ -45,4 +50,9 @@ public function login($name, $password) return false; } + + private function verifyUser(User $user, $password) + { + return Encrypter::verify($password, $user->password); + } } \ No newline at end of file diff --git a/application/services/UsersManagementService.php b/application/services/UsersManagementService.php index 641700e..c4c78c5 100644 --- a/application/services/UsersManagementService.php +++ b/application/services/UsersManagementService.php @@ -10,6 +10,7 @@ use application\engine\Service; use application\models\User; +use engine\Encrypter; class UsersManagementService extends Service { @@ -20,10 +21,11 @@ public function __construct() public function getUser($userId) { - $user = new User(); - $user->find(array('id'=>$userId)); - - return $user->toArray(); + /** + * @var User $user + */ + $user = User::find(array('id' => $userId)); + return $user->attributes(); } /** @@ -32,37 +34,49 @@ public function getUser($userId) */ public function getUsersList() { - $user = new User(); - $user->find(); - - return array($user->toArray()); + $userList = User::find('all'); + + $userArray = array(); + foreach ($userList as $user) { + $userArray[] = array( + 'id' => $user->id, + 'name' => $user->name, + 'role' => $user->role + ); + } + + return $userArray; } public function createUser($userName, $password, $userRole) { - $user = new User(); - $user->name = $userName; - $user->setPassword($password); - $user->role = $userRole; - $user->save(); + User::create(array( + 'name' => $userName, + 'password' => Encrypter::encrypt($password), + 'role' => $userRole + )); } public function editUser($userId, $userName, $password, $userRole) { - $user = new User(); - $user->find(array('id'=>$userId)); + /** + * @var User $user + */ + $user = User::find(array('id' => $userId)); $user->name = $userName; - $user->setPassword($password); + $user->password = Encrypter::encrypt($password); $user->role = $userRole; - + $user->save(); } public function deleteUser($userId) { - $user = new User(); - $user->find(array('id'=>$userId)); + /** + * @var User $user + */ + $user = User::find(array('id' => $userId)); $user->delete(); } diff --git a/application/views/general/top.php b/application/views/general/top.php index 533fa2d..f8f2819 100644 --- a/application/views/general/top.php +++ b/application/views/general/top.php @@ -48,7 +48,7 @@
  • Test 1 - View Chunks.
  • Test 2 - Form and Inputs.
  • Test 3 - Models.
  • -
  • Test 4
  • +
  • Test 4 - Model Collections
  • Test 5
  • diff --git a/application/views/tests/test3/index.php b/application/views/tests/test3/index.php index b50e632..d3f90be 100644 --- a/application/views/tests/test3/index.php +++ b/application/views/tests/test3/index.php @@ -6,7 +6,8 @@ * Date: 26/12/13 21:30 * * @var \application\engine\View $this - */ ?> + */ +?> printChunk('header'); ?> @@ -33,23 +34,23 @@ - + - +
    - +
    - +
    - +
    - +