1: <?php
2: 3: 4: 5:
6: class projectRegistrationHandler extends reportHandler {
7:
8: public $viewhandler;
9: public $notificationhandler;
10:
11: private $grouphandler;
12: private $db;
13:
14: private $appconfig = array(
15: "PRODUCT_APP" => array('project_mode'=>false,'erp'=>true),
16: "WARE_APP" => array('project_mode'=>false,'erp'=>true),
17: "SALES_APP" => array('project_mode'=>false,'erp'=>true),
18: "FINANCE_SALES_INV_APP" => array('project_mode'=>false,'erp'=>true),
19: "PROJECT_APP" => array('project_mode'=>true,'erp'=>false),
20: "ADMINISTRATE_APP" => array('project_mode'=>false,'erp'=>true),
21: "FINANCE_PURCHASE_INV_APP" => array('project_mode'=>false,'erp'=>true),
22: "REPORT_APP" => array('project_mode'=>false,'erp'=>true),
23: "PURCHASE_APP" => array('project_mode'=>false,'erp'=>true),
24: "PREF_APP" => array('project_mode'=>true,'erp'=>true),
25: "WEB_STORE_APP" => array('project_mode'=>false,'erp'=>false),
26: "WEB_STORE_CONFIG_APP" => array('project_mode'=>false,'erp'=>false),
27: "SETUP_APP" => array('project_mode'=>false,'erp'=>false),
28: "ADMIN_APP" => array('project_mode'=>false,'erp'=>false));
29:
30: private $applist = '
31: {
32: "PRODUCT_APP":{"ID":10,
33: "ENABLE":true,
34: "APPNAME":"product",
35: "HTEXT":"product",
36: "URI":"?page=product&handler=product&action=",
37: "SCHEMA": "",
38: "GUEST":false},
39: "WARE_APP":{"ID":1,
40: "ENABLE":true,
41: "APPNAME":"warehouse",
42: "HTEXT":"warehouse",
43: "URI":"?page=warehouse&handler=ware&action=",
44: "SCHEMA": "",
45: "GUEST":false},
46: "SALES_APP":{ "ID":0,
47: "ENABLE":true,
48: "APPNAME":"sales",
49: "HTEXT":"sales",
50: "URI":"?page=sales&handler=sales&action=",
51: "SCHEMA": "",
52: "GUEST":false},
53: "FINANCE_SALES_INV_APP":{ "ID":30,
54: "ENABLE":true,
55: "APPNAME":"finance",
56: "HTEXT":"finance",
57: "URI":"?page=finance&handler=invoice_sales&action=",
58: "SCHEMA": "",
59: "GUEST":false},
60: "PROJECT_APP":{"ID":8,
61: "ENABLE":true,
62: "APPNAME":"project",
63: "HTEXT":"project",
64: "URI":"?page=project&handler=manage&action=",
65: "SCHEMA": "",
66: "GUEST":false},
67: "ADMINISTRATE_APP":{"ID":2,
68: "ENABLE":true,
69: "APPNAME":"administrate",
70: "URI":"?page=administrate&handler=options&action=",
71: "SCHEMA": "",
72: "GUEST":false},
73: "FINANCE_PURCHASE_INV_APP":{"ID":31,
74: "ENABLE":false,
75: "APPNAME":"purchase_invoice",
76: "HTEXT":"invoice_purchase",
77: "URI":"?page=finance&handler=options&action=",
78: "SCHEMA": "",
79: "GUEST":false},
80: "REPORT_APP":{"ID":4,
81: "ENABLE":true,
82: "APPNAME":"reporting",
83: "HTEXT":"purchase",
84: "URI":"?page=report&handler=options&action=",
85: "SCHEMA": "",
86: "GUEST":false},
87: "PURCHASE_APP":{"ID":5,
88: "ENABLE":false,
89: "APPNAME":"purchase",
90: "HTEXT":"purchase",
91: "URI":"?page=sales&handler=options&action=",
92: "SCHEMA": "",
93: "GUEST":false},
94: "PREF_APP":{"ID":6,
95: "ENABLE":false,
96: "APPNAME":"preferences",
97: "HTEXT":"preferences",
98: "URI":"?page=preference&handler=options&action=",
99: "SCHEMA": "",
100: "GUEST":false},
101: "WEB_STORE_APP":{"ID":11,
102: "ENABLE":false,
103: "APPNAME":null,
104: "HTEXT":"webstore",
105: "URI":"?page=storeindx&handler=options&action=",
106: "SCHEMA": "",
107: "GUEST":false},
108: "WEB_STORE_CONFIG_APP":{"ID":12,
109: "ENABLE":false,
110: "APPNAME":"webstore_config",
111: "HTEXT":"webstore_config",
112: "URI":"?page=storeconfig&handler=options&action=",
113: "SCHEMA": "",
114: "GUEST":false},
115: "SETUP_APP":{ "ID":"1000",
116: "ENABLE":false,
117: "APPNAME":"setup",
118: "HTEXT":"setup",
119: "SCHEMA": "",
120: "URI":"?page=options&handler=options&action="},
121: "ADMIN_APP":{ "ID":6,
122: "ENABLE":true,
123: "APPNAME":"admin",
124: "HTEXT":"_settings",
125: "URI":"?page=preference&handler=options&action=",
126: "SCHEMA": "",
127: "GUEST":false}
128: }';
129:
130: private $apdplist = '"app_list":
131: {
132: "PRODUCT_APP":{"ID":10,
133: "ENABLE":true,
134: "APPNAME":"product",
135: "HTEXT":"product",
136: "URI":"?page=product&handler=product&action=",
137: "SCHEMA": "",
138: "GUEST":false}}"';
139:
140:
141: public function __construct($appid = null){
142: $this->ident = 'project_registration';
143: $this->appid = $appid;
144: $this->instance = $this;
145: $this->createObjInstance(array("notificationhandler","notificationHandler",null));
146: $this->createObjInstance(array("viewhandler","projectRegistrationViewHandler",$this->instance));
147: $this->initialize();
148:
149: $this->viewhandler->setHeaderIdent("registration",null);
150:
151: parent::__construct($appid,false);
152: }
153:
154: public function setDataSource(&$db){
155: $this->db = $db;
156: $this->dbhandler = new projectRegistrationDbHandler($db);
157: }
158:
159: public function &getDataSource(){
160: return $this->dbhandler;
161: }
162:
163: public function destructDbInstances(){
164: unset($this->dbhandler);
165: unset($this->db);
166: }
167:
168: public function &getDataObject($ident){
169: switch($ident){
170: default:
171: return $this->instance;
172: break;
173: }
174: }
175:
176: public function initialize(){
177: $this->viewhandler->setHeaderIdent("project_registration",null);
178: }
179:
180: public function initializeById($id){
181: $this->viewhandler->setHeaderIdent("project_registration","{$this->getProjectName()}");
182: }
183:
184: public function registerProject($project_mode,$project_name,$project_key,$admin_user_email,$admin_password,$contact,$language,$standalone = false,$post){
185:
186: $project_mode = $project_mode == 'project' ? 'project_mode' : 'erp';
187: $admin_user_email = trim($admin_user_email);
188: $admin_password = trim($admin_password);
189: $db_schema_name = $standalone == true ? 'public' : substr(str_shuffle(str_repeat("abcdefghijklmnopqrstuvwxyz", 5)), 0, 3).time();
190:
191: try {
192: if (strtolower($project_key) == 'public' && $standalone == false){
193: error_log("LOG: invalid project key $project_key");
194: throw new Exception('invalid project key');
195:
196: }
197:
198: $this->dbhandler->db->transactionStart();
199: $database_user = $this->dbhandler->createDatabaseEnvironment($db_schema_name,$admin_user_email,$admin_password,$post,$standalone);
200: }catch (Exception $e){
201: error_log($e->getMessage());
202: $this->dbhandler->db->transactionRollback();
203: if (strstr($e->getMessage(),'already exists'))
204: throw new Exception("User already exists");
205: else
206: throw new Exception($e->getMessage());
207: }
208:
209: try {
210:
211: error_log("LOG: set database information");
212: $dbid = $this->dbhandler->insertDatabaseInformation($db_schema_name,$database_user,'localhost',$admin_password);
213: }catch (Exception $e){
214: error_log($e->getMessage());
215: $this->dbhandler->db->transactionRollback();
216: if (strstr($e->getMessage(),'already exists'))
217: throw new Exception("Project already exists");
218: else
219: throw new Exception($e->getMessage());
220: }
221:
222: try {
223:
224: error_log("LOG: set customer information");
225:
226: $this->dbhandler->insertCustomerInformation($dbid,$project_key,$admin_user_email,'default',$admin_user_email,$contact,md5($admin_password));
227: $this->dbhandler->updateUserPassword($admin_user_email,$admin_password,$db_schema_name);
228:
229: }catch (Exception $e){
230: error_log($e->getMessage());
231: $this->dbhandler->db->transactionRollback();
232: throw new Exception("error: username already exists");
233: }
234:
235: try {
236:
237: if ($standalone == false){
238: error_log("LOG: import application schema");
239: $this->importApplicationSchema('localhost',$post['dbname'],$database_user,$admin_password,'postgres',$admin_user_email,$contact);
240: }
241: $this->dbhandler->db->transactionEnd();
242: }catch (Exception $e){
243: error_log($e->getMessage());
244: $this->dbhandler->db->transactionRollback();
245: throw new Exception("error: project key already exists");
246: }
247:
248:
249: try {
250:
251: error_log("LOG: set customer details");
252: $this->createCustomerInformation($this->db,$admin_user_email,$contact,'customer');
253: $this->createCustomerInformation($this->db,$admin_user_email,$contact,'organization','');
254: $db = $this->connectUserDatabase('localhost',$post['dbname'],$database_user,$admin_password,'postgres');
255: $this->createInitialProject($db,$project_name,$project_key);
256: $this->updateSchemaOptions($db,$language);
257: $this->setApplicationList($db,$project_mode);
258: unset($db);
259: }catch (Exception $e){
260: error_log($e->getMessage());
261: $this->dbhandler->db->transactionRollback();
262: throw new Exception("error: initial project creation failed:".$e->getMessage());
263: }
264:
265: $this->dbhandler->db->transactionEnd();
266:
267: return true;
268: }
269:
270: private function importApplicationSchema($dbhost,$dbname,$dbuser,$dbpasswd,$dbtype,$email,$contact = null){
271: $setuphandler = new setupHandler();
272: $admin_arr = array('username'=>$email,'password' => md5($dbpasswd));
273: $setuphandler->setupDatabaseSchema($dbhost,$dbname,$dbuser,md5($dbpasswd),$dbtype,$admin_arr,false,$contact);
274: }
275:
276: private function updateSchemaOptions(&$db,$language){
277: $handler = new optionsHandler();
278: $handler->setDataSource($db);
279:
280:
281:
282:
283:
284:
285: $handler->setOptionValue("app_search_header_background_color","#444444");
286: $handler->setOptionValue("lang","$language",true);
287:
288: $handler->destructDbInstances();
289: unset($handler);
290: }
291:
292: private function createInitialProject(&$db,$project_name,$project_key){
293:
294: $handler = new projectHandler();
295: $handler->initialize();
296:
297: $handler->setDataSource($db);
298: $handler->setProjectName($project_name);
299: $handler->setProjectKey($project_key);
300:
301:
302: $handler->setUserId(1);
303: $handler->setProjectManager(1);
304:
305:
306: $handler->syncStorage();
307:
308: $handler->destructDbInstances();
309: unset($handler);
310: }
311:
312: public function registerProjectMember($fullname,$password,$token){
313:
314: try {
315: $this->dbhandler->db->transactionStart();
316: $regdata = $this->dbhandler->getRegistrationMemberdata($token);
317: if ($regdata == null)
318: throw new Exception('invalid registration data');
319:
320: list ($token,$username,$key,$project_key,$id_ref_schema_information) = $reqdata[0];
321: }catch (Exception $e){
322: error_log($e->getMessage());
323: $this->dbhandler->db->transactionRollback();
324: throw new Exception("error: initial project creation failed");
325: }
326: }
327:
328: private function &connectUserDatabase($dbhost,$dbname,$dbuser,$dbpasswd,$dbtype){
329: $opts = null;
330: $dbs = array();
331: $dbpasswd = md5($dbpasswd);
332: list( $dbs['dbName'], $dbs['dbUser'], $dbs['dbPass'], $dbs['dbHost']) = array($dbname,$dbuser,$dbpasswd,'localhost');
333: $db = new dbSql($dbs);
334: $db->dbSqlConnect(null,$dbtype,$opts);
335:
336: return $db;
337: }
338:
339: private function setApplicationList(&$db,$mode){
340: $config = json_decode($this->applist,true);
341: $mode = constant('INSTALL_TYPE') == 'SERVER_INSTALL' && constant('SCRUMIX') == true ? 'project_mode' : $mode;
342: error_log("MODE: $mode");
343: foreach (array_keys($this->appconfig) as $key){
344: error_log("KEY: $key MODE: $mode VAL: {$this->appconfig[$key][$mode]}");
345: $config[$key]['ENABLE'] = intval($this->appconfig[$key][$mode]);
346: }
347: $config['app_mode'] = $mode;
348: $stm = "INSERT INTO application_config VALUES('".json_encode($config)."')";
349: $db->execQuery($stm);
350: }
351: }
352: ?>
353: