PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/web/editor/common/delegate.php

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