PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/web/editor/common/delegate.php

https://bitbucket.org/scriptoid/diagramo-alex
PHP | 792 lines | 492 code | 143 blank | 157 comment | 73 complexity | cb147783951c293b8db944c5f59b8c44 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. /*
  3. Copyright [2014] [Diagramo]
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. define('DEBUG', false);
  15. require_once dirname(__FILE__) . '/utils.php';
  16. /**
  17. * LICENSE
  18. * License Class is not reflected in the Database (it was not generates by SQLarity)
  19. * To generate a license you need first 8 fields completed
  20. * @deprecated
  21. */
  22. class License {
  23. //from client
  24. public $serial; // buyer's serial number
  25. public $host; //Where the license will be installed
  26. //from server
  27. public $date; // purchase date (SQL datetime) as 'yyyy-mm-dd'
  28. public $unlockKey; // full key of the license (license object saved to a string)
  29. /**
  30. * Saves the License object (this) object to a string
  31. */
  32. public function save() {
  33. $this->unlockKey = $this->computeUnlockKey();
  34. return base64_encode(strrev(serialize($this)));
  35. }
  36. /**
  37. * Load the License object (this) from a string
  38. */
  39. public function load($str) {
  40. //nothing
  41. }
  42. /*
  43. * Computes License's full key based on its other values
  44. * The key is based on email, date, expiryDate, maxUsers and serial
  45. */
  46. protected function computeUnlockKey() {
  47. return "";
  48. }
  49. /** Check a license */
  50. public function checkLicense() {
  51. return true;
  52. }
  53. }
  54. class Diagram {
  55. public $id;
  56. public $title;
  57. public $description;
  58. public $public;
  59. public $createdDate;
  60. public $lastUpdate;
  61. function loadFromSQL($row) {
  62. $this->id = is_null($row['id']) ? null : $row['id'];
  63. $this->title = is_null($row['title']) ? null : $row['title'];
  64. $this->description = is_null($row['description']) ? null : $row['description'];
  65. $this->public = is_null($row['public']) ? null : $row['public'];
  66. $this->createdDate = is_null($row['createdDate']) ? null : $row['createdDate'];
  67. $this->lastUpdate = is_null($row['lastUpdate']) ? null : $row['lastUpdate'];
  68. }
  69. }
  70. class User {
  71. public $id;
  72. public $email;
  73. public $password;
  74. public $name;
  75. public $createdDate;
  76. public $lastLoginDate;
  77. public $lastLoginIP;
  78. public $lastBrowserType;
  79. public $admin;
  80. public $tutorial;
  81. function loadFromSQL($row) {
  82. $this->id = is_null($row['id']) ? null : $row['id'];
  83. $this->email = is_null($row['email']) ? null : $row['email'];
  84. $this->password = is_null($row['password']) ? null : $row['password'];
  85. $this->name = is_null($row['name']) ? null : $row['name'];
  86. $this->createdDate = is_null($row['createdDate']) ? null : $row['createdDate'];
  87. $this->lastLoginDate = is_null($row['lastLoginDate']) ? null : $row['lastLoginDate'];
  88. $this->lastLoginIP = is_null($row['lastLoginIP']) ? null : $row['lastLoginIP'];
  89. $this->lastBrowserType = is_null($row['lastBrowserType']) ? null : $row['lastBrowserType'];
  90. $this->admin = is_null($row['admin']) ? null : $row['admin'];
  91. $this->tutorial = is_null($row['tutorial']) ? null : $row['tutorial'];
  92. }
  93. }
  94. class Diagramdata {
  95. const TYPE_DMO = 'dia';
  96. const TYPE_SVG = 'svg';
  97. const TYPE_JPG = 'jpg';
  98. const TYPE_PNG = 'png';
  99. const TYPE_CSV = 'csv';
  100. public $diagramId;
  101. public $type;
  102. public $fileName;
  103. public $fileSize;
  104. public $lastUpdate;
  105. function loadFromSQL($row) {
  106. $this->diagramId = is_null($row['diagramId']) ? null : $row['diagramId'];
  107. $this->type = is_null($row['type']) ? null : $row['type'];
  108. $this->fileName = is_null($row['fileName']) ? null : $row['fileName'];
  109. $this->fileSize = is_null($row['fileSize']) ? null : $row['fileSize'];
  110. $this->lastUpdate = is_null($row['lastUpdate']) ? null : $row['lastUpdate'];
  111. }
  112. }
  113. class Setting {
  114. public $name;
  115. public $value;
  116. function loadFromSQL($row) {
  117. $this->name = is_null($row['name']) ? null : $row['name'];
  118. $this->value = is_null($row['value']) ? null : $row['value'];
  119. }
  120. }
  121. class Delegate extends SQLite3 {
  122. function __construct() {
  123. $this->open( dirname(__FILE__) . '/../data/diagramo.db');
  124. }
  125. /**a wrapper method for executing a query*/
  126. public function executeSQL($query) {
  127. $result = $this->query($query);
  128. return $result;
  129. }
  130. /* * Add a new entry. We should make wrappers around this function (make it private !?!)
  131. * and never call it directly from outside Delegate
  132. * $tableName - name of the table
  133. * $object - the object
  134. * $ids - list of ids (default 'id'), usefull for multiple key or keys other then 'id'
  135. * $nullify - if true unset values will be set to NULL, if false we will not touch existing column value
  136. * returns the 'id' of the created entry
  137. * author: alex
  138. */
  139. protected function create($object, $ids = array('id'), $tableName = null, $nullify = false, $autoincrement = true) {
  140. //detect class name
  141. if (empty($tableName)) {
  142. $tableName = strtolower(get_class($object));
  143. }
  144. //start query
  145. $query = "INSERT INTO `{$tableName}` ( ";
  146. //start collecting column names
  147. $comma = false;
  148. foreach ($object as $key => $value) {
  149. //ignore the primary keys (usually id) if autogenerated
  150. if ($autoincrement && in_array($key, $ids)) {
  151. continue;
  152. }
  153. //set column names
  154. if (isset($value)) { //ok the value is set
  155. if (is_null($value)) { //but it's set to null
  156. if ($nullify) { //we will add columns that will have NULL values
  157. if ($comma) {
  158. $query .= ",";
  159. } else {
  160. $comma = true;
  161. }
  162. $query .= "`{$key}`"; #protect the column names in case they are the same as SQL keywords (ex: order)
  163. } else { //we will ignore the columns with null values
  164. //do nothing
  165. }
  166. } else { //now, it's not null
  167. if ($comma) {
  168. $query .= ",";
  169. } else {
  170. $comma = true;
  171. }
  172. $query .= "`{$key}`";
  173. }
  174. } else {
  175. //just ignore unset values
  176. }
  177. }//end collecting column names
  178. //start collecting values
  179. $query .= ') VALUES (';
  180. //TODO: test for cases where there is not need for a value - ex. table with 1 autogenerated column
  181. //even if this is kinda stupid :P
  182. $comma = false;
  183. foreach ($object as $key => $value) {
  184. //ignore the primary keys (usually id) if autogeneated
  185. if ($autoincrement && in_array($key, $ids)) {
  186. continue;
  187. }
  188. //add VALUES(....)
  189. //right now we skip not set NULL values...but maybe we should reconsider for set to Null values (ex: $o->deadDate = null)
  190. if (isset($value)) {
  191. if ($comma) {
  192. $query .= ", ";
  193. } else {
  194. $comma = true;
  195. }
  196. //based on it's type we quote the value
  197. switch (gettype($value)) {
  198. case 'string':
  199. $query .= sprintf("'%s'", addslashes($value));
  200. break;
  201. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  202. $query .= $value ? 'true' : 'false';
  203. break;
  204. case 'NULL' : //if $conditionValue is null the gettype($conditionValue) returns 'NULL'
  205. $query .= 'NULL';
  206. break;
  207. default:
  208. $query .= sprintf("%s", $value);
  209. }
  210. } else {
  211. if ($nullify) { //should we set the unset values to null ?
  212. if ($comma) {
  213. $query .= ", ";
  214. } else {
  215. $comma = true;
  216. }
  217. $query .= " NULL";
  218. }
  219. }
  220. }//end collecting values
  221. $query .= ')';
  222. // print $query;
  223. #exit();
  224. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ . '{#}' . __FUNCTION__ . "{#}{$query}{#}" . __LINE__ : '';
  225. //EXECUTE
  226. $result = $this->query($query);
  227. if ($autoincrement) {//autogenerated ID
  228. // print "log: autoincrement used";
  229. return $this->lastId();
  230. } else { //"by hand" ids
  231. // print "log: by hand used";
  232. if ($this->changes() > 0) {
  233. // print "log: affected";
  234. return true;
  235. } else {
  236. // print "log: not affected";
  237. return false;
  238. }
  239. }
  240. }
  241. /* retuns last inserted Id */
  242. protected function lastId() {
  243. $result = $this->query('SELECT last_insert_rowid() as last_insert_rowid')->fetchArray();
  244. return $result['last_insert_rowid'];
  245. }
  246. /* * Update an entry from an object. We should make wrappers around this function (make it private !?!)
  247. * and never call it directly from outside Delegate
  248. * $tableName - name of the table
  249. * $object - the object
  250. * $ids - list of ids (default 'id'), usefull for multiple key or keys other then 'id'
  251. * $nullify - if true unset values will be set to NULL, if false we will not touch existing column value
  252. * author: liviu, alex
  253. *
  254. * Note: The update is made based on the object/record id, so the id should not be changed!
  255. */
  256. protected function update($object, $ids = array('id'), $tableName = null, $nullify = false) {
  257. //detect class name
  258. if (empty($tableName)) {
  259. $tableName = strtolower(get_class($object));
  260. }
  261. //start query
  262. $query = "UPDATE `{$tableName}` SET ";
  263. $comma = false;
  264. foreach ($object as $key => $value) {
  265. //ignore the primary keys (usually id)
  266. if (in_array($key, $ids)) {
  267. continue;
  268. }
  269. //set values
  270. // if(isset($value)) { //pick only set values and ignore not set ones
  271. //TODO: here is wrong as $v= null; isset($v) returns False and we can not get inside this branch/scope
  272. if (is_null($value)) { //the value is null so we have to see what to do with it
  273. if ($nullify) { //should we set the unset values to null ?
  274. if ($comma) {
  275. $query .= ", ";
  276. } else {
  277. $comma = true;
  278. }
  279. $query .= "{$key} = NULL ";
  280. } else {
  281. //do nothing, we will ignore set & null values
  282. }
  283. } else { //the value is not null
  284. if ($comma) {
  285. $query .= ", ";
  286. } else {
  287. $comma = true;
  288. }
  289. //based on it's type we quote the value
  290. switch (gettype($value)) {
  291. case 'string':
  292. $query .= sprintf(" `{$key}` = '%s' ", addslashes($value));
  293. break;
  294. // case 'boolean':
  295. // $query .= sprintf(" `{$key}` = %s ", $value ? "true" : "false");
  296. // break;
  297. default:
  298. $query .= sprintf(" `{$key}` = %s ", addslashes($value));
  299. break;
  300. }
  301. }
  302. // } else {
  303. // //ignore unset values
  304. // }
  305. }//end foreach
  306. //use the keys
  307. $query .= " WHERE "; //'WHERE' should always be present as there should always be an id
  308. $comma = false;
  309. foreach ($ids as $id) {
  310. foreach ($object as $key => $value) {
  311. // print "ID: $id -------" . "($key,$value) ----------- " . var_export($object, true) . "<br>";
  312. if ($id == $key) { //ok we found a key
  313. if ($comma) {
  314. $query .= " AND ";
  315. } else {
  316. $comma = true;
  317. }
  318. switch (gettype($value)) {
  319. case 'string':
  320. $query .= sprintf(" {$key} = '%s' ", addslashes($value));
  321. break;
  322. // case 'boolean':
  323. // $query .= sprintf(" {$key} = '%s' ", $value?'true':'false');
  324. // break;
  325. default: //we place together integers, booleans and aliens
  326. $query .= sprintf(" {$key} = %s ", addslashes($value));
  327. break;
  328. }
  329. }
  330. }
  331. } //end foreach
  332. // print $query;
  333. // exit();
  334. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ . '{#}' . __FUNCTION__ . "{#}{$query}{#}" . __LINE__ : '';
  335. /* EXECUTE
  336. * @see http://www.php.net/manual/en/sqlite3.query.php
  337. */
  338. return $this->query($query);
  339. }
  340. /**
  341. * Get a number of object from the database
  342. * $tableName - table name
  343. * $conditions - AND like conditions ex: array('name'=>'alex', 'age'=>'31')
  344. * $orders - ORDER BY part ex: array('name'=>'ASC', 'age'=>'DESC')
  345. * $start - start offset
  346. * $nr - number of rows returned
  347. * author: alex
  348. */
  349. protected function getMultiple($tableName, $conditions = null, $orders = null, $start = null, $nr = null) {
  350. $objects = array(); //this will contain all the found objects
  351. $tableName = strtolower($tableName);
  352. //start query building
  353. $query = sprintf("SELECT * FROM `%s`", $tableName);
  354. //conditions
  355. if (count($conditions) > 0) {
  356. $query .= " WHERE ";
  357. $and = false;
  358. foreach ($conditions as $conditionName => $conditionValue) {
  359. if ($and) {
  360. $query .= " AND ";
  361. } else {
  362. $and = true;
  363. }
  364. //based on it's type we quote the value
  365. switch (gettype($conditionValue)) {
  366. case 'string':
  367. $query .= sprintf(" `%s` = '%s'", $conditionName, addslashes($conditionValue));
  368. break;
  369. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  370. $query .= sprintf(" `%s` = %s", $conditionName, $conditionValue ? 'true' : 'false');
  371. break;
  372. case 'NULL' : //if $conditionValue is null the gettype($conditionValue) returns 'NULL'
  373. $query .= sprintf(" `%s` IS NULL", $conditionName);
  374. break;
  375. default:
  376. $query .= sprintf(" `%s` = %s", $conditionName, $conditionValue);
  377. }
  378. }
  379. }
  380. //add orders
  381. if (count($orders) > 0) {
  382. $query .= " ORDER BY ";
  383. $comma = false;
  384. foreach ($orders as $order => $direction) {
  385. if ($comma) {
  386. $query .= sprintf(", `%s` %s ", $order, $direction);
  387. } else {
  388. $query .= sprintf(" `%s` %s", $order, $direction);
  389. $comma = true;
  390. }
  391. }
  392. }
  393. if (!is_null($start)) {
  394. $query .= sprintf(" LIMIT %d", $start);
  395. }
  396. if (!is_null($nr)) {
  397. $query .= sprintf(", %d", $nr);
  398. }
  399. #print $query;
  400. #exit();
  401. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ . '{#}' . __FUNCTION__ . "{#}{$query}{#}" . __LINE__ : '';
  402. //EXECUTE query
  403. $result = $this->query($query);
  404. $className = ucfirst($tableName);
  405. while ($row = $result->fetchArray()) {
  406. $object = new $className;
  407. $object->loadFromSQL($row);
  408. $objects[] = $object;
  409. }
  410. return $objects;
  411. }
  412. /* * Return single */
  413. protected function getSingle($tableName, $conditions = null) {
  414. $foundedObjects = $this->getMultiple($tableName, $conditions);
  415. if (isset($foundedObjects) && count($foundedObjects) > 0) {
  416. return $foundedObjects[0];
  417. }
  418. return;
  419. }
  420. /* * Return single */
  421. protected function getCount($tableName, $conditions = null) {
  422. $foundedObjects = $this->getMultiple($tableName, $conditions);
  423. return count($foundedObjects);
  424. }
  425. /* * Remove all entries from a table that met conditions
  426. * param: $conditions (an array of $key=>$value)
  427. * Returns true if data was deleted, false otherwise
  428. *
  429. * Ex: delete('user', array('id'=>1)) //delete the user with id 1
  430. * Ex2: delete('user') //delete ALL users
  431. */
  432. protected function delete($tableName, $conditions = null) {
  433. $tableName = strtolower($tableName);
  434. //start query building
  435. $query = sprintf("DELETE FROM `%s`", $tableName);
  436. //conditions
  437. if (count($conditions) > 0) {
  438. $query .= " WHERE ";
  439. $and = false;
  440. foreach ($conditions as $conditionName => $conditionValue) {
  441. if ($and) {
  442. $query .= " AND ";
  443. } else {
  444. $and = true;
  445. }
  446. //based on it's type we quote the value
  447. switch (gettype($conditionValue)) {
  448. case 'string':
  449. $query .= sprintf(" %s = '%s'", $conditionName, addslashes($conditionValue));
  450. break;
  451. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  452. $query .= sprintf(" %s = %s", $conditionName, $conditionValue ? 'true' : 'false');
  453. break;
  454. default:
  455. $query .= sprintf(" %s = %s", $conditionName, $conditionValue);
  456. }
  457. }
  458. }
  459. // print $query;
  460. // exit();
  461. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ . '{#}' . __FUNCTION__ . "{#}{$query}{#}" . __LINE__ : '';
  462. $this->query($query);
  463. /*
  464. * @see: http://stackoverflow.com/questions/313567/how-can-i-determine-the-number-of-affected-rows-in-a-sqlite-2-query-in-php
  465. */
  466. if ($this->changes() > 0) {
  467. return true;
  468. } else {
  469. return false;
  470. }
  471. }
  472. /************************************************************************* */
  473. /************************************************************************* */
  474. /************************************************************************* */
  475. public function userGetByEmailAndPassword($email,$password) {
  476. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  477. return $this->getSingle('user', array('email'=>$email, 'password'=>md5($password) ));
  478. }
  479. public function userGetByEmailAndCryptedPassword($email,$cryptedPassword) {
  480. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  481. return $this->getSingle('user', array('email'=>$email, 'password'=>$cryptedPassword ));
  482. }
  483. public function userGetById($userId) {
  484. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  485. return $this->getSingle('user', array('id'=>$userId));
  486. }
  487. public function userGetAll() {
  488. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  489. return $this->getMultiple('user', null, array('email' => 'ASC'));
  490. }
  491. /************************************************************************* */
  492. /************************************************************************* */
  493. /************************************************************************* */
  494. public function diagramGetAll() {
  495. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ . '{#}' . __FUNCTION__ . "{#}{#}" . __LINE__ : '';
  496. return $this->getMultiple('diagram', null, array('title' => 'DESC'));
  497. }
  498. public function diagramCreate($entry) {
  499. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ . '{#}' . __FUNCTION__ . "{#}{#}" . __LINE__ : '';
  500. return $this->create($entry);
  501. }
  502. public function diagramGetById($diagramId) {
  503. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  504. return $this->getSingle('diagram', array('id'=>$diagramId));
  505. }
  506. public function diagramDeleteById($diagramId) {
  507. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  508. return $this->delete('diagram', array('id'=>$diagramId));
  509. }
  510. public function diagramUpdate($diagram) {
  511. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  512. return $this->update($diagram);
  513. }
  514. /**This create a cascade delete to diagramdata*/
  515. public function diagramDelete($diagramId){
  516. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  517. return $this->delete('diagram', array('id'=>$diagramId));
  518. }
  519. /**************************************************************************/
  520. /*********************************SETTINGS*********************************/
  521. /**************************************************************************/
  522. public function settingsGetByKeyNative($key){
  523. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  524. $query = sprintf("select `value` from `setting` where `name` = '%s' ",$key);
  525. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  526. $value = '';
  527. //EXECUTE query
  528. $result = $this->query($query);
  529. if( $row = $result->fetchArray() ){
  530. $value = $row['value'];
  531. }
  532. return $value;
  533. }
  534. public function settingsSaveNative($key, $value){
  535. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  536. //see http://stackoverflow.com/questions/418898/sqlite-upsert-not-insert-or-replace
  537. $query = sprintf("insert or REPLACE into `setting` (`value`,`name`) VALUES('%s', '%s')", $value, $key);
  538. $this->query($query);
  539. if($this->changes() > 0) {
  540. return true;
  541. } else {
  542. return false;
  543. }
  544. }
  545. /**************************************************************************/
  546. /*********************************USER*************************************/
  547. /**************************************************************************/
  548. public function userCreate($user) {
  549. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  550. return $this->create($user);
  551. }
  552. public function userDeleteById($id) {
  553. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  554. return $this->delete('user', array('id' => $id));
  555. }
  556. public function userUpdate($user) {
  557. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  558. return $this->update($user);
  559. }
  560. /**************************************************************************/
  561. /*****************************DIAGRAMDATA**********************************/
  562. /**************************************************************************/
  563. public function diagramdataCreate($entry) {
  564. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  565. //$object, $ids = array('id'), $tableName=null, $nullify=false, $autoincrement=true) {
  566. return $this->create($entry, array('diagramId', 'type'), 'diagramdata', false, false);
  567. }
  568. public function diagramdataGetByDiagramIdAndType($diagramId, $type) {
  569. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  570. return $this->getSingle('diagramdata', array('diagramId'=>$diagramId, 'type'=>$type));
  571. }
  572. public function diagramdataUpdate($diagramdata) {
  573. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  574. return $this->update($diagramdata, array('diagramId', 'type'), 'diagramdata'); //do not update the key
  575. }
  576. public function diagramdataGetByDiagramId($diagramId) {
  577. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  578. return $this->getMultiple('diagramdata', array('diagramId'=>$diagramId));
  579. }
  580. /**This create a cascade delete to diagramdata*/
  581. public function diagramdataDeleteByDiagramIdAndType($diagramId, $type){
  582. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  583. return $this->delete('diagramdata', array('diagramId'=>$diagramId, 'type'=>$type));
  584. }
  585. }
  586. function diagramCreate($dbhandle, $title, $description, $public) {
  587. $stm1 = sprintf("INSERT INTO diagram (title, description, public, createdDate, lastUpdate)
  588. VALUES('%s', '%s', '%s', '%s', '%s')", $title, $description, $public, gmdate('Y-m-d H:i:s'), gmdate('Y-m-d H:i:s'));
  589. #print($stm1);
  590. $ok1 = sqlite_exec($dbhandle, $stm1);
  591. if (!$ok1)
  592. die("Cannot execute statement.");
  593. }
  594. function diagramGetById($dbhandle, $diagramId) {
  595. $d = false;
  596. $query = sprintf("SELECT * FROM diagram where id=%d", $diagramId);
  597. $result = sqlite_query($dbhandle, $query);
  598. if ($result) {
  599. $row = sqlite_fetch_array($result, SQLITE_ASSOC);
  600. if ($row) {
  601. $d = new Diagram();
  602. $d->loadFromSQL($row);
  603. }
  604. }
  605. return $d;
  606. }
  607. function diagramGetAll2($dbhandle) {
  608. $diagrams = array();
  609. $query = "SELECT * FROM diagram ORDER BY title";
  610. $results = $this->query($query);
  611. if ($results) {
  612. while ($row = $results->fetchArray()) {
  613. #print_r($row);
  614. $d = new Diagram();
  615. $d->loadFromSQL($row);
  616. $diagrams[] = $d;
  617. }
  618. }
  619. return $diagrams;
  620. }
  621. function diagramDeleteById($dbhandle, $diagramId) {
  622. $query = sprintf("delete FROM diagram where id=%d", $diagramId);
  623. $result = sqlite_query($dbhandle, $query);
  624. if ($result) {
  625. }
  626. }
  627. if(false && PHP_SAPI == 'cli'){ //see http://php.net/manual/en/features.commandline.php
  628. print("\nOn the console");
  629. //test
  630. $d = new Delegate();
  631. $diagrams = $d->diagramGetAll();
  632. print_r($diagrams);
  633. $diagram = new Diagram();
  634. $diagram->title = 'Ana are mere';
  635. $diagram->description = 'Ana are foarte multe mere';
  636. $diagram->public = 0;
  637. $diagram->createdDate = gmdate('Y-m-d h:i:s');
  638. $diagram->lastUpdate = gmdate('Y-m-d h:i:s');
  639. $dId = $d->diagramCreate($diagram);
  640. print("Diagram Id: " + $dId);
  641. $nd = $d->diagramGetById($dId);
  642. $nd->title = 'Zzoz';
  643. $d->diagramUpdate($nd);
  644. $d->close();
  645. }
  646. ?>