From 80c8971a46a49fde795fbc0feb4780e40224a95f Mon Sep 17 00:00:00 2001 From: Deon George Date: Tue, 28 Oct 2014 16:33:15 +1100 Subject: [PATCH] Added Add Node capability --- application/classes/Controller/Node.php | 5 ++ application/classes/Controller/User/Node.php | 61 +++++++++++++++ application/config/tsm.php | 17 ++++ application/views/node/user/add.php | 13 ++++ application/views/welcome/user/shortcuts.php | 2 +- modules/lnapp | 2 +- .../classes/Database/Query/Builder/Insert.php | 4 + modules/tsm/classes/ORM/TSM.php | 29 +++++++ .../lnApp/Database/Query/Builder/Insert.php | 77 +++++++++++++++++++ modules/tsm/classes/lnApp/Database/TSM.php | 7 +- modules/tsm/classes/lnApp/Model/NODE.php | 8 ++ 11 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 application/classes/Controller/Node.php create mode 100644 application/classes/Controller/User/Node.php create mode 100644 application/config/tsm.php create mode 100644 application/views/node/user/add.php create mode 100644 modules/tsm/classes/Database/Query/Builder/Insert.php create mode 100644 modules/tsm/classes/lnApp/Database/Query/Builder/Insert.php diff --git a/application/classes/Controller/Node.php b/application/classes/Controller/Node.php new file mode 100644 index 0000000..2806a8d --- /dev/null +++ b/application/classes/Controller/Node.php @@ -0,0 +1,5 @@ +0, + 'view'=>0, + ); + + private function node_name($i) { + return sprintf('%s%02d',$this->ao->id(),$i); + } + + public function action_add() { + if ($this->request->post()) { + $n = ORM::factory('NODE')->where('EMAIL_ADDRESS','=',$this->ao->email)->find_all(); + + if (FALSE AND $n->count() > $this->ao->max_nodes) { + SystemMessage::add(array( + 'title'=>_('Maximum nodes created'), + 'type'=>'info', + 'body'=>_('Additional nodes cannot be created for your account, you have already reached your maximum. If you really need more, please contact us.'), + )); + + HTTP::redirect(URL::link('user','welcome')); + } + + // Find a free name. + for ($i=1;$i<$n->count();$i++) + if (! Object::in_array('NODE_NAME',$this->node_name($i),$n->as_array())) + break; + + $no = ORM::factory('NODE'); + $no->NODE_NAME=$this->node_name($i); + $no->CONTACT=$this->ao->name(); + $no->EMAILADDRESS=$this->ao->email; + $no->values($x=Arr::merge(Kohana::$config->load('tsm')->node_create,$this->request->post())); + + $this->save($no); + + if ($no->saved()) + #HTTP::redirect(URL::link('user','node/view/'.$no->NODE_NAME)); + HTTP::redirect(URL::link('user','welcome')); + } + + Block::factory() + ->type('form-horizontal') + ->title('Register NODE') + ->title_icon('fa-desktop') + ->body(View::factory('node/user/add')->set('o',$this->ao)); + } +} +?> diff --git a/application/config/tsm.php b/application/config/tsm.php new file mode 100644 index 0000000..65d3d6e --- /dev/null +++ b/application/config/tsm.php @@ -0,0 +1,17 @@ + array( + 'PASSEXP'=>90, + 'PASSWORD'=>'ch@ng3Me!', + 'DOMAIN_NAME'=>'IDPR_DEMO', + 'COMPRESSION'=>'Client', + 'ARCHDELETE'=>'YES', + 'BACKDELETE'=>'YES', + 'OPTION_SET'=>'DEMO', + 'DEDUPLICATION'=>'Client', + 'USERID'=>'NONE', + 'FORCEPWRESET'=>'YES', + ), +); diff --git a/application/views/node/user/add.php b/application/views/node/user/add.php new file mode 100644 index 0000000..fe3f6a6 --- /dev/null +++ b/application/views/node/user/add.php @@ -0,0 +1,13 @@ +
+ Register a TSM node + +

Please give us a description for this node.

+ 'col-md-6','label'=>'Description','placeholder'=>'Description for Node')); ?> +

+ +
+
+ + +
+
diff --git a/application/views/welcome/user/shortcuts.php b/application/views/welcome/user/shortcuts.php index 8d4b8bc..49e770e 100644 --- a/application/views/welcome/user/shortcuts.php +++ b/application/views/welcome/user/shortcuts.php @@ -1,5 +1,5 @@
- Create Node + Create Node Create SSL Create Admin
diff --git a/modules/lnapp b/modules/lnapp index f7567dc..db7f5f8 160000 --- a/modules/lnapp +++ b/modules/lnapp @@ -1 +1 @@ -Subproject commit f7567dc2507403430f3e34d3d663aee5d756fb51 +Subproject commit db7f5f8d55fb2d467d7e70df5f7bd15b7a21efbc diff --git a/modules/tsm/classes/Database/Query/Builder/Insert.php b/modules/tsm/classes/Database/Query/Builder/Insert.php new file mode 100644 index 0000000..24acb6d --- /dev/null +++ b/modules/tsm/classes/Database/Query/Builder/Insert.php @@ -0,0 +1,4 @@ + diff --git a/modules/tsm/classes/ORM/TSM.php b/modules/tsm/classes/ORM/TSM.php index 1e52f38..ea81f99 100644 --- a/modules/tsm/classes/ORM/TSM.php +++ b/modules/tsm/classes/ORM/TSM.php @@ -27,6 +27,7 @@ abstract class ORM_TSM extends ORM { protected $_formated = FALSE; protected $_formats = array(); + protected $_custom_cols = array(); protected $_tsm = array(); /** @@ -55,10 +56,31 @@ abstract class ORM_TSM extends ORM { // Get a substited column name - need for DB2/DSMADMC schema differences if (isset($this->_tsm[$this->_db_group]['translate']) AND array_key_exists($column,$this->_tsm[$this->_db_group]['translate'])) return is_null($c=$this->_tsm[$this->_db_group]['translate'][$column]) ? NULL : parent::__get($c); + else return parent::__get($column); } + public function __set($column,$value) { + if (in_array($column,$this->_custom_cols)) { + + // See if the data really changed + if (! isset($this->_object[$column]) OR $value !== $this->_object[$column]) { + $this->_table_columns[$column]['data_type'] = 'custom'; + + $this->_object[$column] = $value; + + // Data has changed + $this->_changed[$column] = $column; + + // Object is no longer saved or valid + $this->_saved = $this->_valid = FALSE; + } + + } else + parent::__set($column,$value); + } + public function find() { Log::instance()->add(LOG::DEBUG,'ENTER :method',array(':method'=>__METHOD__)); @@ -170,5 +192,12 @@ abstract class ORM_TSM extends ORM { return array_key_exists($type,$x) ? $x[$type] : $x; } + + public function values(array $values,array $expected=NULL) { + foreach (array_intersect(array_keys($values),$this->_custom_cols) as $col) + $this->_table_columns[$col]['data_type'] = 'custom'; + + return parent::values($values,$expected); + } } ?> diff --git a/modules/tsm/classes/lnApp/Database/Query/Builder/Insert.php b/modules/tsm/classes/lnApp/Database/Query/Builder/Insert.php new file mode 100644 index 0000000..930d457 --- /dev/null +++ b/modules/tsm/classes/lnApp/Database/Query/Builder/Insert.php @@ -0,0 +1,77 @@ +array( + 'OPTION_SET'=>'CLOPTSET', + 'DOMAIN_NAME'=>'DOMAIN', + ), + ); + + public function compile($db = NULL) { + if ( ! is_object($db)) { + // Get the database instance + $db = Database::instance($db); + } + + switch ($this->_table) { + case 'NODES': + $query = sprintf('REGISTER NODE %s %s %s',$this->pop_value('NODE_NAME'),$this->pop_value('PASSWORD'),$this->insert_values()); + + break; + + default: + throw HTTP_Exception::factory(501,'Not setup for table :table',array(':table'=>$this->_table)); + } + + return $query; + } + + private function get_keyalt($key) { + return isset($this->_commands[$this->_table][$key]) ? $this->_commands[$this->_table][$key] : $key; + } + + private function get_key($key) { + return array_search($key,$this->_columns); + } + + // @todo Values always go in as an array - should see if we can stop that + private function get_value($key) { + return (is_numeric($x=$this->get_key($key))) ? $this->_values[0][$x] : ''; + } + + private function insert_values() { + $result = ''; + + foreach ($this->_columns as $k=>$v) { + if ($result) + $result .= ' '; + + $result .= sprintf('%s=\'%s\'',$this->get_keyalt($v),$this->get_value($v)); + } + + return $result; + } + + private function pop_value($key) { + $result = NULL; + + $x = $this->get_key($key); + + $result = $this->get_value($key); + unset($this->_columns[$x]); + unset($this->_values[0][$x]); + + return $result; + } +} +?> diff --git a/modules/tsm/classes/lnApp/Database/TSM.php b/modules/tsm/classes/lnApp/Database/TSM.php index d449646..7aa3625 100644 --- a/modules/tsm/classes/lnApp/Database/TSM.php +++ b/modules/tsm/classes/lnApp/Database/TSM.php @@ -48,9 +48,6 @@ abstract class lnApp_Database_TSM extends Database { public function set_charset($charset) {} public function list_tables($like = NULL) {} - // Required methods - abstract protected function execute($sql); - /** * Return the caching defined in the current configuration. * @@ -90,7 +87,7 @@ abstract class lnApp_Database_TSM extends Database { if (isset($benchmark)) Profiler::delete($benchmark); - SystemMessage::TSM('error',sprintf('%s (%s)',$this->execute_stdout,$this->execute_stderr),$sql); + SystemMessage::TSM('danger',sprintf('%s (%s)',$this->execute_stdout,$this->execute_stderr),$sql); } if (isset($benchmark)) @@ -103,7 +100,7 @@ abstract class lnApp_Database_TSM extends Database { // Return an iterator of results return new Database_TSM_Result($result, $sql, $as_object, $params); elseif ($type === Database::INSERT) - throw new Kohana_Exception('Database INSERTS are not supported'); + return new Database_TSM_Result($result, $sql, $as_object, $params); elseif ($type === Database::SHOW) return new Database_TSM_Show($result, $sql, $as_object, $params); elseif ($type === Database::SET) diff --git a/modules/tsm/classes/lnApp/Model/NODE.php b/modules/tsm/classes/lnApp/Model/NODE.php index 204ae1d..bdb8a77 100644 --- a/modules/tsm/classes/lnApp/Model/NODE.php +++ b/modules/tsm/classes/lnApp/Model/NODE.php @@ -61,6 +61,13 @@ abstract class lnApp_Model_NODE extends ORM_TSM { ), ); + protected $_custom_cols = array( + 'EMAILADDRESS', + 'FORCEPWRESET', + 'PASSWORD', + 'USERID', + ); + /** * Get all the ACTIVITIY LOG for this NODE */ @@ -105,6 +112,7 @@ abstract class lnApp_Model_NODE extends ORM_TSM { } Log::instance()->add(LOG::DEBUG,'EXIT :method',array(':method'=>__METHOD__)); + return $result; }