Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Enhance codepad adn add 2 example sub-pojects

  • Loading branch information...
commit f09263ad302a33d1002090e0dea9ca83e8a682b9 1 parent c760fdb
Romans Malinovskis authored June 12, 2012

Showing 30 changed files with 676 additions and 21 deletions. Show diff stats Hide diff stats

  1. 136  doc/dbupdates/codepad.001.sql
  2. 5  example/learn1/config-default.php
  3. 4  example/learn1/index.php
  4. 12  example/learn1/lib/Frontend.php
  5. 22  example/learn1/page/register.php
  6. 15  example/learn1/templates/default/page/index.html
  7. 12  example/learn1/templates/default/page/register.html
  8. 5  example/learn2/index.php
  9. 33  example/learn2/lib/Frontend.php
  10. 18  example/learn2/lib/Model/Book.php
  11. 15  example/learn2/lib/Model/BookAvailable.php
  12. 27  example/learn2/lib/Model/Borrower.php
  13. 22  example/learn2/lib/Model/Borrowing.php
  14. 11  example/learn2/lib/Model/BorrowingNow.php
  15. 17  example/learn2/lib/Model/Member.php
  16. 13  example/learn2/lib/Model/Staff.php
  17. 22  example/learn2/page/books.php
  18. 11  example/learn2/page/history.php
  19. 110  example/learn2/page/members.php
  20. 9  example/learn2/page/staff.php
  21. 11  index.php
  22. 10  lib/Frontend.php
  23. 3  lib/TreeView.php
  24. 116  page/projectview.php
  25. 1  templates/default/css/atk-codepad.css
  26. 2  templates/default/css/atk-codepad.less
  27. 5  templates/default/page/learning/model.html
  28. 19  templates/default/page/learning/model/1.html
  29. 9  templates/default/page/learning/model/2.html
  30. 2  templates/default/shared.html
136  doc/dbupdates/codepad.001.sql
... ...
@@ -0,0 +1,136 @@
  1
+-- MySQL dump 10.13  Distrib 5.5.9, for osx10.6 (i386)
  2
+--
  3
+-- Host: localhost    Database: codepad
  4
+-- ------------------------------------------------------
  5
+-- Server version	5.5.9
  6
+
  7
+/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  8
+/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  9
+/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  10
+/*!40101 SET NAMES utf8 */;
  11
+/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
  12
+/*!40103 SET TIME_ZONE='+00:00' */;
  13
+/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  14
+/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  15
+/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  16
+/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  17
+
  18
+--
  19
+-- Table structure for table `library_staff`
  20
+--
  21
+
  22
+DROP TABLE IF EXISTS `library_staff`;
  23
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
  24
+/*!40101 SET character_set_client = utf8 */;
  25
+CREATE TABLE `library_staff` (
  26
+  `id` int(11) NOT NULL AUTO_INCREMENT,
  27
+  `name` varchar(255) DEFAULT NULL,
  28
+  PRIMARY KEY (`id`)
  29
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  30
+/*!40101 SET character_set_client = @saved_cs_client */;
  31
+
  32
+--
  33
+-- Dumping data for table `library_staff`
  34
+--
  35
+
  36
+LOCK TABLES `library_staff` WRITE;
  37
+/*!40000 ALTER TABLE `library_staff` DISABLE KEYS */;
  38
+INSERT INTO `library_staff` VALUES (1,'Dennis Ritchie'),(2,'Will Smith');
  39
+/*!40000 ALTER TABLE `library_staff` ENABLE KEYS */;
  40
+UNLOCK TABLES;
  41
+
  42
+--
  43
+-- Table structure for table `library_member`
  44
+--
  45
+
  46
+DROP TABLE IF EXISTS `library_member`;
  47
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
  48
+/*!40101 SET character_set_client = utf8 */;
  49
+CREATE TABLE `library_member` (
  50
+  `id` int(11) NOT NULL AUTO_INCREMENT,
  51
+  `name` varchar(255) DEFAULT NULL,
  52
+  `card_number` varchar(255) DEFAULT NULL,
  53
+  PRIMARY KEY (`id`)
  54
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  55
+/*!40101 SET character_set_client = @saved_cs_client */;
  56
+
  57
+--
  58
+-- Dumping data for table `library_member`
  59
+--
  60
+
  61
+LOCK TABLES `library_member` WRITE;
  62
+/*!40000 ALTER TABLE `library_member` DISABLE KEYS */;
  63
+INSERT INTO `library_member` VALUES (1,'Joe Blogs','029982'),(2,'Ken Thompson','00777');
  64
+/*!40000 ALTER TABLE `library_member` ENABLE KEYS */;
  65
+UNLOCK TABLES;
  66
+
  67
+--
  68
+-- Table structure for table `library_book`
  69
+--
  70
+
  71
+DROP TABLE IF EXISTS `library_book`;
  72
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
  73
+/*!40101 SET character_set_client = utf8 */;
  74
+CREATE TABLE `library_book` (
  75
+  `id` int(11) NOT NULL AUTO_INCREMENT,
  76
+  `name` varchar(255) DEFAULT NULL,
  77
+  `issn` varchar(255) DEFAULT NULL,
  78
+  PRIMARY KEY (`id`)
  79
+) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  80
+/*!40101 SET character_set_client = @saved_cs_client */;
  81
+
  82
+--
  83
+-- Dumping data for table `library_book`
  84
+--
  85
+
  86
+LOCK TABLES `library_book` WRITE;
  87
+/*!40000 ALTER TABLE `library_book` DISABLE KEYS */;
  88
+INSERT INTO `library_book` VALUES (1,'Adaptation as Compendium: Tim Burton\'s Alice in Wonderland','1755-0637'),(2,'The Ultimate Hitchhiker\'s Guide to the Galaxy','0345391802');
  89
+/*!40000 ALTER TABLE `library_book` ENABLE KEYS */;
  90
+UNLOCK TABLES;
  91
+
  92
+--
  93
+-- Table structure for table `library_borrowing`
  94
+--
  95
+
  96
+DROP TABLE IF EXISTS `library_borrowing`;
  97
+/*!40101 SET @saved_cs_client     = @@character_set_client */;
  98
+/*!40101 SET character_set_client = utf8 */;
  99
+CREATE TABLE `library_borrowing` (
  100
+  `id` int(11) NOT NULL AUTO_INCREMENT,
  101
+  `library_book_id` int(11) NOT NULL,
  102
+  `library_member_id` int(11) NOT NULL,
  103
+  `verified_by` int(11) NOT NULL,
  104
+  `borrowed` date DEFAULT NULL,
  105
+  `is_returned` varchar(255) DEFAULT NULL,
  106
+  `returned` date DEFAULT NULL,
  107
+  PRIMARY KEY (`id`),
  108
+  KEY `fk_library_borrowing_book1` (`library_book_id`),
  109
+  KEY `fk_library_borrowing_library_member1` (`library_member_id`),
  110
+  KEY `fk_library_borrowing_library_staff1` (`verified_by`),
  111
+  CONSTRAINT `fk_library_borrowing_book1` FOREIGN KEY (`library_book_id`) REFERENCES `library_book` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  112
+  CONSTRAINT `fk_library_borrowing_library_member1` FOREIGN KEY (`library_member_id`) REFERENCES `library_member` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION,
  113
+  CONSTRAINT `fk_library_borrowing_library_staff1` FOREIGN KEY (`verified_by`) REFERENCES `library_staff` (`id`) ON DELETE CASCADE ON UPDATE NO ACTION
  114
+) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
  115
+/*!40101 SET character_set_client = @saved_cs_client */;
  116
+
  117
+--
  118
+-- Dumping data for table `library_borrowing`
  119
+--
  120
+
  121
+LOCK TABLES `library_borrowing` WRITE;
  122
+/*!40000 ALTER TABLE `library_borrowing` DISABLE KEYS */;
  123
+INSERT INTO `library_borrowing` VALUES (2,1,1,1,'2012-06-12','1','2012-06-12'),(4,2,2,1,'2012-06-12','1','2012-06-12'),(5,2,1,1,'2012-06-12','0',NULL);
  124
+/*!40000 ALTER TABLE `library_borrowing` ENABLE KEYS */;
  125
+UNLOCK TABLES;
  126
+/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
  127
+
  128
+/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  129
+/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  130
+/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  131
+/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  132
+/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  133
+/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  134
+/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
  135
+
  136
+-- Dump completed on 2012-06-12  2:38:09
5  example/learn1/config-default.php
... ...
@@ -0,0 +1,5 @@
  1
+<?php
  2
+
  3
+$config['atk']['base_path']='/codepad/atk4/';
  4
+$config['url_postfix']='';
  5
+$config['url_prefix']='?page=';
4  example/learn1/index.php
... ...
@@ -0,0 +1,4 @@
  1
+<?php
  2
+include '../../atk4/loader.php';
  3
+$api=new Frontend('learn1');
  4
+$api->main();
12  example/learn1/lib/Frontend.php
... ...
@@ -0,0 +1,12 @@
  1
+<?php
  2
+class Frontend extends ApiFrontend {
  3
+	function init(){
  4
+		parent::init();
  5
+
  6
+		$this->add('jUI');
  7
+
  8
+		$menu=$this->add('Menu',null,'Menu');
  9
+		$menu->addMenuItem('register');
  10
+		
  11
+	}
  12
+}
22  example/learn1/page/register.php
... ...
@@ -0,0 +1,22 @@
  1
+<?php
  2
+class page_register extends Page {
  3
+	function init(){
  4
+		parent::init();
  5
+		
  6
+		$form = $this->add('Form');
  7
+        $form->addField('line','email');
  8
+        $form->addField('password','password');
  9
+        $form->addSubmit();
  10
+        $form->addButton('Cancel')->js('click')
  11
+        	->univ()->location($this->api->url('/'));
  12
+
  13
+        if($form->isSubmitted()){
  14
+        	$form->js()->univ()->alert('Thank you, '.$form->get('email').
  15
+        		', but this example is not ready yet')->execute();
  16
+
  17
+        }
  18
+	}
  19
+	function defaultTemplate(){
  20
+        return array('page/register');
  21
+    }
  22
+}
15  example/learn1/templates/default/page/index.html
... ...
@@ -0,0 +1,15 @@
  1
+<p>
  2
+	This is your first Agile Toolkit application. It may contain static page just like a regular site.
  3
+</p>
  4
+
  5
+
  6
+<p>
  7
+	Inside a static page you may use <b>HTML</b> and it will be passed to the frontend-unmodified
  8
+</p>
  9
+
  10
+<div class="atk-notification ui-state-highlight ui-corner-all ">
  11
+	<div class="atk-notification-text">
  12
+		<i class="ui-icon ui-icon-info"></i>You may take advantage of a jQuery UI
  13
+		ThemeRoller and Agile Toolkit CSS Frameworks!
  14
+	</div>
  15
+</div>
12  example/learn1/templates/default/page/register.html
... ...
@@ -0,0 +1,12 @@
  1
+<h1>Registration form</h1>
  2
+<div id="<?$_name?>" class="atk-row">
  3
+  <div class="span8">
  4
+    <?$Content?>
  5
+  </div><div class="span4">
  6
+    <div class="atk-notification ui-state-highlight ui-corner-all ">
  7
+    	<div class="atk-notification-text">
  8
+    		<i class="ui-icon ui-icon-info"></i>You must apply for new account before you can use this application.
  9
+    	</div>
  10
+    </div>
  11
+</div>
  12
+</div>
5  example/learn2/index.php
... ...
@@ -0,0 +1,5 @@
  1
+<?php
  2
+// This is a standard catch-all file
  3
+include '../../atk4/loader.php';
  4
+$api=new Frontend('learn1');
  5
+$api->main();
33  example/learn2/lib/Frontend.php
... ...
@@ -0,0 +1,33 @@
  1
+<?php
  2
+class Frontend extends ApiFrontend {
  3
+	function init(){
  4
+		parent::init();
  5
+
  6
+		// Add jQuery UI and connect to database
  7
+		$this->add('jUI');
  8
+		$this->dbConnect();
  9
+
  10
+		// Creates menu on top
  11
+		$menu=$this->add('Menu',null,'Menu');
  12
+		$menu->addMenuItem('books');
  13
+		$menu->addMenuItem('members');
  14
+		$menu->addMenuItem('staff');
  15
+		$menu->addMenuItem('history');
  16
+	}
  17
+
  18
+	function page_index(){
  19
+		$this->add('View_Info')->set('This example data is available for public edit. If you find that content on the pages is empty or is vandalized, please click this button to re-initialize database');
  20
+		if($this->add('Button')->set('Reset Database')->isClicked()){
  21
+			$this->db->query('delete from library_borrowing');
  22
+			$this->db->query('delete from library_book');
  23
+			$this->db->query('delete from library_member');
  24
+			$this->db->query('delete from library_staff');
  25
+			$this->db->query("INSERT INTO `library_book` VALUES (1,'Adaptation as Compendium: Tim Burton\'s Alice in Wonderland','1755-0637'),(2,'The Ultimate Hitchhiker\'s Guide to the Galaxy','0345391802')");
  26
+			$this->db->query("INSERT INTO `library_member` VALUES (1,'Joe Blogs','029982'),(2,'Ken Thompson','00777')");
  27
+			$this->db->query("INSERT INTO `library_staff` VALUES (1,'Dennis Ritchie'),(2,'Will Smith')");
  28
+			$this->db->query("INSERT INTO `library_borrowing` VALUES (2,1,1,1,'2012-06-12','1','2012-06-12'),(4,2,2,1,'2012-06-12','1','2012-06-12'),(5,2,1,1,'2012-06-12','0',NULL)");
  29
+			$this->js()->univ()->successMessage('Database cleaned up successfully')->execute();
  30
+		}
  31
+
  32
+	}
  33
+}
18  example/learn2/lib/Model/Book.php
... ...
@@ -0,0 +1,18 @@
  1
+<?php
  2
+/**
  3
+ * Base class implementing book
  4
+ */
  5
+class Model_Book extends Model_Table {
  6
+	public $table="library_book";
  7
+	function init(){
  8
+		parent::init();
  9
+		
  10
+		$this->addField('name');
  11
+		$this->addField('issn')->sortable(true);
  12
+
  13
+		// hasMany does not impact logic or performance of the model
  14
+		// so we can have multiple in here.
  15
+		$this->hasMany('Borrowing');
  16
+		$this->hasMany('Borrower');
  17
+	}
  18
+}
15  example/learn2/lib/Model/BookAvailable.php
... ...
@@ -0,0 +1,15 @@
  1
+<?php
  2
+/**
  3
+ * A dedicated model for books which are available to borrow
  4
+ */
  5
+class Model_BookAvailable extends Model_Book {
  6
+	function init(){
  7
+		parent::init();
  8
+		
  9
+		// We must select books which are either never been borrowed or have been returned
  10
+		$f=$this->join('library_borrowing.library_book_id',null,'left')->addField('is_returned')->system(true);
  11
+
  12
+		// Now that our relation is defined we can add custom condition with the OR clause.
  13
+		$this->addCondition($this->dsql()->orExpr()->where($f,'is',null)->where($f,'1'));
  14
+	}
  15
+}
27  example/learn2/lib/Model/Borrower.php
... ...
@@ -0,0 +1,27 @@
  1
+<?php
  2
+/**
  3
+ * Models can be defined in either way and can be based on as many tables as you wish. Borrower shows
  4
+ * members which currently have got some books
  5
+ */
  6
+class Model_Borrower extends Model_Member {
  7
+	public $b;
  8
+	function init(){
  9
+		parent::init();
  10
+		
  11
+		$this->b=$this->join('library_borrowing.library_member_id');
  12
+
  13
+		$this->b->hasOne('Staff','verified_by');
  14
+		$this->b->addField('borrowed');
  15
+		$this->b->addField('is_returned');
  16
+		$this->b->addField('returned');
  17
+
  18
+		$this->b->join('library_book')->addField('book_name','name');
  19
+
  20
+		// Showing only if borrowed
  21
+		//$this->addCondition('is_returned',false);
  22
+
  23
+		// We don't want anyone to accidentally insert into this model as it will impact both 
  24
+		// joined tables, so we create validation to avoid that.
  25
+		$this->addHook('beforeInsert',function($m){ throw $m->exception('Do not add borrower directly');});
  26
+	}
  27
+}
22  example/learn2/lib/Model/Borrowing.php
... ...
@@ -0,0 +1,22 @@
  1
+<?php
  2
+class Model_Borrowing extends Model_Table {
  3
+	public $table="library_borrowing";
  4
+	function init(){
  5
+		parent::init();
  6
+		
  7
+		$this->hasOne('Book');
  8
+		$this->hasOne('Member');
  9
+		$this->hasOne('Staff','verified_by');
  10
+
  11
+		$this->addField('borrowed')->type('date')->defaultValue(date('Y-m-d'));
  12
+
  13
+		$this->addField('is_returned')->type('boolean')->defaultValue(false);
  14
+		$this->addField('returned')->type('date');
  15
+	}
  16
+	function returnBook(){
  17
+		if($this['is_returned'])throw $this->exception('Book was already returned');
  18
+		$this['is_returned']=true;
  19
+		$this['returned']=date('Y-m-d');
  20
+		return $this->save();
  21
+	}
  22
+}
11  example/learn2/lib/Model/BorrowingNow.php
... ...
@@ -0,0 +1,11 @@
  1
+<?php
  2
+/**
  3
+ * Anohter handy model which shows only current borrowings
  4
+ */
  5
+class Model_BorrowingNow extends Model_Borrowing {
  6
+	function init(){
  7
+		parent::init();
  8
+		
  9
+		$this->addCondition('is_returned',false);
  10
+	}
  11
+}
17  example/learn2/lib/Model/Member.php
... ...
@@ -0,0 +1,17 @@
  1
+<?php
  2
+class Model_Member extends Model_Table {
  3
+	public $table="library_member";
  4
+	function init(){
  5
+		parent::init();
  6
+		
  7
+		$this->addField('name');
  8
+		$this->addField('card_number');
  9
+
  10
+		// We can define theese only if we plan to reference them. While it makes sense to see curent 
  11
+		// member's borrowed book, there is no need to see all the preious borrowings, so i commented
  12
+		// that relation
  13
+
  14
+		//$this->hasMany('Borrowing');
  15
+		$this->hasMany('BorrowingNow');
  16
+	}
  17
+}
13  example/learn2/lib/Model/Staff.php
... ...
@@ -0,0 +1,13 @@
  1
+<?php
  2
+class Model_Staff extends Model_Table {
  3
+	public $table="library_staff";
  4
+	function init(){
  5
+		parent::init();
  6
+
  7
+		$this->addField('name');
  8
+
  9
+		// Reference for showing books issued by that member of staff. Consider this your practice excercise
  10
+		// to create a page which would show all the books which were issued by a certain member
  11
+		$this->hasMany('Borrowing');
  12
+	}
  13
+}
22  example/learn2/page/books.php
... ...
@@ -0,0 +1,22 @@
  1
+<?php
  2
+/**
  3
+ * Showing list of books
  4
+ */
  5
+class page_books extends Page {
  6
+	function init(){
  7
+		parent::init();
  8
+		
  9
+		// Will show 2 tabs on a page, one for all books, other for books available to borrow
  10
+		$tt=$this->add('Tabs');
  11
+
  12
+		$cr=$tt->addTab('All')->add('CRUD');
  13
+		$cr->setModel('Book');
  14
+		if($cr->grid)$cr->grid->addQuickSearch(array('name','issn'));
  15
+
  16
+		$cr=$tt->addTab('Available to Borrow')->add('CRUD');
  17
+		$cr->setModel('BookAvailable');
  18
+		if($cr->grid)$cr->grid->addQuickSearch(array('name','issn'));
  19
+
  20
+		// We also include quicksearch for easier search
  21
+	}
  22
+}
11  example/learn2/page/history.php
... ...
@@ -0,0 +1,11 @@
  1
+<?php
  2
+class page_history extends Page {
  3
+	function init(){
  4
+		parent::init();
  5
+		
  6
+		// Simply display contents of a model with pagination
  7
+		$grid=$this->add('Grid');
  8
+		$grid->setModel('Borrower');
  9
+		$grid->addPaginator();
  10
+	}
  11
+}
110  example/learn2/page/members.php
... ...
@@ -0,0 +1,110 @@
  1
+<?php
  2
+class page_members extends Page {
  3
+
  4
+	// This page consists of 3 sub-pages. I placed them in the same file for easire access.
  5
+
  6
+	function page_index(){
  7
+
  8
+		$m=$this->add('Model_Member');
  9
+
  10
+		// Dynamically add expression showing number of currently borrowed books by member
  11
+		$m->addExpression('books')->set($m->refSQL('BorrowingNow')->count())
  12
+			->caption('Borrowed');
  13
+		
  14
+		// Add CRUD for managing members
  15
+		$cr=$this->add('CRUD');
  16
+		$cr->setModel($m);
  17
+
  18
+		// Enhance CRUD
  19
+		if($cr->grid){
  20
+
  21
+			// Add quick-search by name and issn and also convert 'Borrowed' column into expander
  22
+			$cr->grid->addQuickSearch(array('name','issn'));
  23
+			$cr->grid->addFormatter('books','expander');
  24
+
  25
+			// Add button allowing for a member to borrow a book
  26
+			$cr->grid->addColumn('button','borrow');
  27
+
  28
+			// Grid's button will execute AJAX request with borrow=<id>
  29
+			if($_GET['borrow']){
  30
+
  31
+				// Get the name of currently selected member
  32
+				$name = $cr->grid->model->load($_GET['borrow'])->get('name');
  33
+
  34
+				// Open frame with member's name in the title. Load content through AJAX from subpage
  35
+				$this->js()->univ()->frameURL('New borrowing by '.$name,
  36
+					$this->api->url('./borrow',array('id'=>$_GET['borrow'])))
  37
+					->execute();
  38
+			}
  39
+		}
  40
+
  41
+	}
  42
+
  43
+	function page_books(){
  44
+		$m=$this->add('Model_Book');
  45
+
  46
+		// Dynamically change model to include only related entires
  47
+		$bor=$m->join('library_borrowing.library_book_id');
  48
+
  49
+		// We might need more fields from joined table
  50
+		$bor->addField('library_member_id');
  51
+		$bor->addField('is_returned')->type('boolean');
  52
+		$bor->addField('library_borrowing_id','id')->system(true);
  53
+		// System fields will not appear in froms and grids by defaultt
  54
+
  55
+		// We are only interested in currently borrowed book by this specific member
  56
+		$m->addCondition('library_member_id',$_GET['library_member_id']);
  57
+		$m->addCondition('is_returned',false);
  58
+		// Normally all this should be stored in model! Keep your UI logic clean
  59
+
  60
+		// Clicking on grid buttons should also pass library_member_id
  61
+		$this->api->stickyGET('library_member_id');
  62
+
  63
+		// Place grid in a box with grayish background
  64
+		$g=$this->add('View')->addStyle('background','#eee')
  65
+			->add('Grid');
  66
+
  67
+		$g->setModel($m,array('name','issn'));
  68
+		$g->addColumn('button','return');
  69
+		if($_GET['return']){
  70
+
  71
+			// Returning borrowed book by book_id and calling custom model method
  72
+			$this->add('Model_Borrowing')->loadBy('library_book_id',$_GET['return'])->returnBook();
  73
+			$g->js()->univ()->location($this->api->url('..'))->execute();
  74
+		}
  75
+
  76
+
  77
+	}
  78
+
  79
+	function page_borrow(){
  80
+
  81
+		// This page for borrowing new book
  82
+		$m=$this->add('Model_Borrowing');
  83
+
  84
+		// Substitute model for existing field, because we want to show only
  85
+		// available books
  86
+		$m->getElement('library_book_id')->refModel('Model_BookAvailable');
  87
+
  88
+		// Perform quick check to make sure there are some books to borrow
  89
+		if(!$m->ref('library_book_id')->count()->getOne()){
  90
+			$this->add('View_Error')->set('No Books to Borrow');
  91
+			$this->add('Button')->set('Close')->js('click')->univ()->closeDialog();
  92
+			return;
  93
+		}
  94
+
  95
+		// Change look of the form, will place labels above fields
  96
+		$f=$this->add('Form')->addClass('stacked');
  97
+		$f->setModel($m,array('library_book_id','library_member_id','verified_by'));
  98
+
  99
+		// Set default value for a field
  100
+		$f->set('library_member_id',$_GET['id']);
  101
+		$f->addSubmit();
  102
+		if($f->isSubmitted()){
  103
+
  104
+			// will create new record in the database
  105
+			$f->update();
  106
+			$f->js()->univ()->location($this->api->url('..'))->execute();
  107
+		}
  108
+	}
  109
+
  110
+}
9  example/learn2/page/staff.php
... ...
@@ -0,0 +1,9 @@
  1
+<?php
  2
+class page_staff extends Page {
  3
+	function init(){
  4
+		parent::init();
  5
+		
  6
+		// The most trivial usage of CRUD
  7
+		$this->add('CRUD')->setModel('Staff');
  8
+	}
  9
+}
11  index.php
... ...
@@ -1,15 +1,4 @@
1 1
 <?php
2  
-
3  
-// DO NOT ADD ANYTHING TO THIS FILE!!
4  
-
5  
-// This is a catch-all file for your project. You can change
6  
-// some of the values here, which are going to have affect
7  
-// on your project
8  
-
9  
-// AgileProject - change to your own API name.
10  
-// agile_project - this is realm. It should be unique per-project
11  
-// jui - this is theme. Keep it jui unless you want to make your own theme
12  
-
13 2
 include 'atk4/loader.php';
14 3
 $api=new Frontend('codepad');
15 4
 $api->main();
10  lib/Frontend.php
... ...
@@ -1,6 +1,8 @@
1 1
 <?php
2 2
 class Frontend extends ApiFrontend {
3 3
     public $example_cut;
  4
+    public $tree;
  5
+    
4 6
 	function init(){
5 7
 		parent::init();
6 8
 		$this->dbConnect();
@@ -14,9 +16,6 @@ function init(){
14 16
 
15 17
 		$this->add('jUI');
16 18
 
17  
-        if($this->template->hasTag('SubMenu'))
18  
-            $this->add('TreeView',null,'SubMenu',array('submenu'))
19  
-            ->setModel('Menu');
20 19
 	}
21 20
     function page_back($p){
22 21
         $this->redirect('http://agiletoolkit.org/doc/');
@@ -28,6 +27,11 @@ function initLayout(){
28 27
         $page->template->eachTag('Silent',function($a,$b) use($page){ $page->add('View_Example',null,$b)->set($a,true); });
29 28
         $page->template->eachTag('ExecuteTrigger',function($a,$b) use($page){ $page->add('View_ExecuteTrigger',null,$b)->set($a,'trigger'); });
30 29
 
  30
+
  31
+        if(!$this->tree && $this->template->hasTag('SubMenu')){
  32
+            $tree=$this->add('TreeView',null,'SubMenu',array('submenu'));
  33
+            $tree->setModel('Menu');
  34
+        }
31 35
     }
32 36
     function render(){
33 37
         $this->js(true)->_load('myuniv')->univ()->softScroll();
3  lib/TreeView.php
@@ -25,9 +25,12 @@ function setModel($m){
25 25
         parent::setModel($m);
26 26
 
27 27
         $this->child_ref=$this->model->hierarchy_controller->child_ref;
  28
+        return $this->model;
28 29
     }
29 30
 
30 31
     function formatRow(){
  32
+        if($this->current_row['page'] instanceof URL)return;
  33
+        if($this->stack[0] instanceof Model)return;
31 34
         $this->current_row['page']=$this->api->url($this->stack[0]['page'].'/'.$this->current_row['page']);
32 35
     }
33 36
 
116  page/projectview.php
... ...
@@ -0,0 +1,116 @@
  1
+<?php
  2
+class page_projectview extends Page {
  3
+	function init(){
  4
+		parent::init();
  5
+		$this->api->stickyGET('e');
  6
+		$this->api->template->trySet('title',strtoupper($_GET['e']));
  7
+		
  8
+		$base='example/'.$_GET['e'];
  9
+		if($this->api->template->hasTag('SubMenu')){
  10
+			$this->api->tree=$this->api->add('TreeView',null,'SubMenu',array('submenu'));
  11
+			$this->api->tree->setModel('Directory')
  12
+				->sub('templates','templates/default/page')
  13
+				->setBasePath($base);
  14
+		}
  15
+
  16
+		// only show files from the example directories
  17
+
  18
+		if($_GET['p']){
  19
+			if(substr($_GET['p'],0,strlen($base)) != $base)exit;
  20
+			$this->api->stickyGET('p');
  21
+			if(!preg_match('/^[\/a-z1-9A-Z-]*(\.php|\.html)?$/',$_GET['p']))exit;
  22
+
  23
+			$this->add('ViewSource')->setFile($_GET['p']);
  24
+
  25
+		}else{
  26
+			$this->add('P')->set('You are exploring project files. Select the files on the left to see the source');
  27
+			$this->add('P')->setHTML('You can also browse this on github');
  28
+		}
  29
+	}
  30
+}
  31
+
  32
+/**
  33
+ * Traverses into directories producing model with all files
  34
+ */
  35
+class Model_Directory extends hierarchy\Model_Array {
  36
+
  37
+	public $substitute=array();
  38
+
  39
+	function sub($dir,$dir2){
  40
+		$this->substitute[$dir]=$dir2;
  41
+		return $this;
  42
+	}
  43
+
  44
+	function setBasePath($path,$depth=5){
  45
+		if(!preg_match('/^[\/a-z1-9A-Z]*$/',$path))exit;
  46
+		$x=$this->traverse($path,$depth);
  47
+
  48
+		foreach($x as $k1=>&$top)if($top['children']){
  49
+			foreach($top['children'] as $k2=>&$l2)if($l2['children']){
  50
+				$x[]=array(
  51
+					'name'=>$top['name'].'/'.$l2['name'],
  52
+					'page'=>$l2['page'],
  53
+					'children'=>$l2['children']
  54
+					);
  55
+				unset($top['children'][$k2]);
  56
+			}
  57
+
  58
+		}
  59
+
  60
+		$this->setSource($x);
  61
+	}
  62
+
  63
+	function traverse($path,$depth){
  64
+		if(!$depth)return array();
  65
+		$res=array();
  66
+		$d=opendir($path);
  67
+		while($file=readdir($d)){
  68
+			if($file[0]=='.')continue;
  69
+
  70
+			if($this->substitute[$file])$file=$this->substitute[$file];
  71
+
  72
+			$item=array();
  73
+			$item['name']=$file;
  74
+			if(is_dir($path.'/'.$file)){
  75
+				$item['children']=$this->traverse($path.'/'.$file,$depth-1);
  76
+			}else{
  77
+				$item['page']=$this->api->url(null,array('p'=>$path.'/'.$file));
  78
+			}
  79
+			$res[]=$item;
  80
+		}
  81
+		return $res;
  82
+	}
  83
+}
  84
+
  85
+class ViewSource extends View {
  86
+
  87
+
  88
+	function setFile($f){
  89
+		$brush='Php';
  90
+        if($this->template->is_set('brush')){
  91
+            $brush=$this->template->get('brush');
  92
+            $this->template->del('brush');
  93
+        }
  94
+
  95
+        $this->template->set('Code',file_get_contents($f));
  96
+
  97
+        if(!@$this->api->highlighter_initialized){
  98
+        	$this->api->jui->addStaticInclude('syntaxhighlighter/scripts/shCore');
  99
+        	$this->api->jui->addStaticInclude('syntaxhighlighter/scripts/shBrush'.$brush);
  100
+        	$this->api->jui->addStaticInclude('shJQuery');
  101
+        	$this->api->jui->addStaticStylesheet('shCoreDefault','.css','js');
  102
+        	$this->api->highlighter_initialized=true;
  103
+        }
  104
+
  105
+        $this->js(true)->_selector('#'.$this->name.'_ex')->syntaxhighlighter(array('lang'=>$brush));
  106
+
  107
+        $this->template->tryDel('has_demo');
  108
+    }
  109
+
  110
+    function defaultTemplate(){
  111
+    	if($_GET['cut'])return array('view/example_cut');
  112
+    	return array('view/example');
  113
+    }
  114
+
  115
+
  116
+}
1  templates/default/css/atk-codepad.css
@@ -1380,7 +1380,6 @@ input.ui-button,
1380 1380
 .sidebar > ul > li > a {
1381 1381
   font-size: 10px !important;
1382 1382
   font-weight: bold;
1383  
-  text-transform: uppercase;
1384 1383
   color: #2C3F53;
1385 1384
   padding: 6px 10px;
1386 1385
   background-color: #ecf3f9;
2  templates/default/css/atk-codepad.less
@@ -30,7 +30,7 @@
30 30
 	>ul {
31 31
 		>li {border-bottom:1px solid #c3ced9;border-top:1px solid #fff;
32 32
 			>a {
33  
-				font-size:10px !important;font-weight:bold;text-transform:uppercase;color:#2C3F53;padding:6px 10px;
  33
+				font-size:10px !important;font-weight:bold;color:#2C3F53;padding:6px 10px;
34 34
 				#gradient > .vertical(#f2f8fc,#ecf3f9);
35 35
 				i {float:left;margin-right:2px;}
36 36
 				&:hover {}
5  templates/default/page/learning/model.html
@@ -76,7 +76,7 @@
76 76
 $item->tryLoadAny();
77 77
 
78 78
 $item['price'] = 14.99;
79  
-$item['name'] = 'Mouse Pad';
  79
+$item['name'] = 'Mouse Pad '.rand(1,100);
80 80
 
81 81
 $item->save();
82 82
 <?/?>
@@ -95,7 +95,10 @@
95 95
   $form->update();
96 96
   $form->js(null,$page->js()->reload())->univ()->successMessage('Order added')->execute();
97 97
 }
  98
+<?/?>
98 99
 
  100
+<?ExecuteTrigger?>Clean Up Orders
  101
+$page->add('Model_Order')->deleteAll();
99 102
 <?/?>
100 103
 
101 104
 <a name="test"></div>
19  templates/default/page/learning/model/1.html
... ...
@@ -0,0 +1,19 @@
  1
+<?Silent?>
  2
+$page->api->redirect('..');
  3
+<?/?>
  4
+
  5
+<?Silent?>Sample 1b
  6
+$item = $this->add('Model_Item');
  7
+
  8
+$item->tryLoadAny();
  9
+
  10
+$order = $item->ref('Order');
  11
+
  12
+$order['amount']=5;
  13
+$order->save();
  14
+
  15
+$item->reload();
  16
+
  17
+$res = $item['successful_orders'];   // will increment by 5
  18
+<?/?>
  19
+
9  templates/default/page/learning/model/2.html
... ...
@@ -1,3 +1,7 @@
  1
+<?Silent?>
  2
+$page->api->redirect('..');
  3
+<?/?>
  4
+
1 5
 <?Silent?>lib/Model/Item.php
2 6
 class Model_Item extends Model_Table {
3 7
   public $table='item';
@@ -11,7 +15,7 @@
11 15
 <?/?>
12 16
 
13 17
 <?Silent?>Sample 1b
14  
-$item = $htis->add('Model_Item');
  18
+$item = $this->add('Model_Item');
15 19
 
16 20
 $item->tryLoadAny();
17 21
 
@@ -25,6 +29,3 @@
25 29
 $res = $item['successful_orders'];   // will increment by 5
26 30
 <?/?>
27 31
 
28  
-<?Silent?>
29  
-$page->api->redirect('..');
30  
-<?/?>
2  templates/default/shared.html
@@ -55,6 +55,8 @@
55 55
           </div>				
56 56
         </div>
57 57
       </div>
  58
+
  59
+      <h1><?$title?></h1>
58 60
       <div class="atk-row">
59 61
 
60 62
         <div class="atk-flexy main">

0 notes on commit f09263a

Please sign in to comment.
Something went wrong with that request. Please try again.