PageRenderTime 53ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/web/editor/common/delegate.php

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