PageRenderTime 66ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/web/editor/common/delegate.php

https://bitbucket.org/keento/diagramo-janis
PHP | 965 lines | 590 code | 198 blank | 177 comment | 75 complexity | 2a5acf076ef4225f6947bf8beed90277 MD5 | raw file
Possible License(s): GPL-2.0, IPL-1.0
  1. <?php
  2. @session_start();
  3. require_once(dirname(__FILE__).'/settings.php');
  4. require_once(dirname(__FILE__).'/entities.php');
  5. require_once(dirname(__FILE__).'/utils.php');
  6. class Delegate {
  7. private $con;
  8. /**constructor*/
  9. public function __construct() {
  10. $this->con = $this->getConnection();
  11. }
  12. /**destructor*/
  13. public function __destruct() {
  14. $this->closeConnection($this->con);
  15. }
  16. /**a wrapper method for executing a query*/
  17. public function executeSQL($query, $con) {
  18. $con = mysql_connect(DB_ADDRESS, DB_USER_NAME, DB_USER_PASS);
  19. $ok = mysql_select_db(DB_NAME);
  20. $result = mysql_query($query, $con);
  21. if (mysql_errno()) {
  22. die('<br />'.mysql_errno().':'.mysql_error().'<br />'.$query);
  23. }
  24. return $result;
  25. }
  26. /**get a connection*/
  27. protected function getConnection() {
  28. $con = mysql_connect(DB_ADDRESS, DB_USER_NAME, DB_USER_PASS);
  29. $ok = mysql_select_db(DB_NAME);
  30. // add connection to array
  31. $sqlLinks[] = $con;
  32. if (mysql_errno()) {
  33. die('<br />'.mysql_errno().':'.mysql_error().'<br />');
  34. }
  35. return $con;
  36. }
  37. /**close a connection to database*/
  38. protected function closeConnection($con) {
  39. global $sqlClosedConnectionsNumber;
  40. @mysql_close($con);
  41. $sqlClosedConnectionsNumber++;
  42. if (mysql_errno()) {
  43. die('<br />'.mysql_errno().':'.mysql_error().'<br />');
  44. }
  45. }
  46. /*retuns last inserted Id*/
  47. protected function lastId($con) {
  48. if (mysql_affected_rows($con)) {
  49. $query = 'SELECT LAST_INSERT_ID() AS NEW_ID';
  50. $result = $this->executeSQL($query, $this->con);
  51. $row = mysql_fetch_array($result, MYSQL_ASSOC);
  52. $number = $row['NEW_ID'];
  53. return $number;
  54. }
  55. }
  56. /**************************************************************************/
  57. /************************GENERAL FUNCTIONS*********************************/
  58. /**************************************************************************/
  59. /**
  60. * Use this function to define strange plurals (not a simple 's' added: example company/companies)
  61. */
  62. // function singular($plural){
  63. // $dict = array('entries'=>'entry');
  64. //
  65. // if(key_exists($plural, $dict)){
  66. // return $dict[$plural];
  67. // }
  68. // else{
  69. // //try to remove the 's'
  70. // return substr($plural, 0, -1);
  71. // }
  72. // }
  73. //
  74. // function plural($singular){
  75. // $dict = array('entries'=>'entry');
  76. // $reverseDict = array_flip($dict);
  77. //
  78. // if(key_exists($singular, $reverseDict)){
  79. // return $reverseDict[$singular];
  80. // }
  81. // else{
  82. // return $singular + 's';
  83. // }
  84. // }
  85. /**Update an entry from an object. We should make wrappers around this function (make it private !?!)
  86. * and never call it directly from outside Delegate
  87. * $tableName - name of the table
  88. * $object - the object
  89. * $ids - list of ids (default 'id'), usefull for multiple key or keys other then 'id'
  90. * $nullify - if true unset values will be set to NULL, if false we will not touch existing column value
  91. * author: liviu, alex
  92. *
  93. * Note: The update is made based on the object/record id, so the id should not be changed!
  94. */
  95. protected function update($object, $ids = array('id'), $tableName=null, $nullify=false) {
  96. //detect class name
  97. if(empty($tableName)) {
  98. $tableName = strtolower(get_class($object));
  99. }
  100. //start query
  101. $query = "UPDATE `{$tableName}` SET ";
  102. $comma = false;
  103. foreach($object as $key => $value) {
  104. //ignore the primary keys (usually id)
  105. if(in_array($key, $ids)) {
  106. continue;
  107. }
  108. //set values
  109. // if(isset($value)) { //pick only set values and ignore not set ones
  110. //TODO: here is wrong as $v= null; isset($v) returns False and we can not get inside this branch/scope
  111. if(is_null($value)) { //the value is null so we have to see what to do with it
  112. if($nullify) { //should we set the unset values to null ?
  113. if($comma) {
  114. $query .= ", ";
  115. } else {
  116. $comma = true;
  117. }
  118. $query .= "{$key} = NULL ";
  119. } else {
  120. //do nothing, we will ignore set & null values
  121. }
  122. } else { //the value is not null
  123. if($comma) {
  124. $query .= ", ";
  125. } else {
  126. $comma = true;
  127. }
  128. //based on it's type we quote the value
  129. switch(gettype($value)) {
  130. case 'string':
  131. $query .= sprintf(" `{$key}` = '%s' ", addslashes($value));
  132. break;
  133. case 'boolean':
  134. $query .= sprintf(" `{$key}` = %s ", $value ? "true" : "false");
  135. break;
  136. default:
  137. $query .= sprintf(" `{$key}` = %s ", addslashes($value));
  138. break;
  139. }
  140. }
  141. // } else {
  142. // //ignore unset values
  143. // }
  144. }//end foreach
  145. //use the keys
  146. $query .= " WHERE "; //'WHERE' should always be present as there should always be an id
  147. $comma = false;
  148. foreach($ids as $id) {
  149. foreach($object as $key => $value) {
  150. // print "ID: $id -------" . "($key,$value) ----------- " . var_export($object, true) . "<br>";
  151. if ($id == $key) { //ok we found a key
  152. if($comma) {
  153. $query .= " AND ";
  154. }
  155. else {
  156. $comma = true;
  157. }
  158. switch(gettype($value)) {
  159. case 'string':
  160. $query .= sprintf(" {$key} = '%s' ", addslashes($value));
  161. break;
  162. default: //we place together integers, booleans and aliens
  163. $query .= sprintf(" {$key} = %s ", addslashes($value));
  164. break;
  165. }
  166. }
  167. }
  168. } //end foreach
  169. #print $query;
  170. #exit();
  171. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  172. //EXECUTE
  173. $result = $this->executeSQL($query, $this->con);
  174. if(mysql_affected_rows($this->con)) {
  175. return true;
  176. } else {
  177. return false;
  178. }
  179. }
  180. /**Add a new entry. We should make wrappers around this function (make it private !?!)
  181. * and never call it directly from outside Delegate
  182. * $tableName - name of the table
  183. * $object - the object
  184. * $ids - list of ids (default 'id'), usefull for multiple key or keys other then 'id'
  185. * $nullify - if true unset values will be set to NULL, if false we will not touch existing column value
  186. * returns the 'id' of the created entry
  187. * author: alex
  188. */
  189. protected function create($object, $ids = array('id'), $tableName=null, $nullify=false, $autoincrement=true) {
  190. //detect class name
  191. if(empty($tableName)) {
  192. $tableName = strtolower(get_class($object));
  193. }
  194. //start query
  195. $query = "INSERT INTO `{$tableName}` ( ";
  196. //start collecting column names
  197. $comma = false;
  198. foreach($object as $key => $value) {
  199. //ignore the primary keys (usually id) if autogenerated
  200. if($autoincrement && in_array($key, $ids)) {
  201. continue;
  202. }
  203. //set column names
  204. if(isset($value)) { //ok the value is set
  205. if(is_null($value)) { //but it's set to null
  206. if($nullify) { //we will add columns that will have NULL values
  207. if($comma) {
  208. $query .= ",";
  209. }
  210. else {
  211. $comma = true;
  212. }
  213. $query .= "`{$key}`"; #protect the column names in case they are the same as SQL keywords (ex: order)
  214. }
  215. else { //we will ignore the columns with null values
  216. //do nothing
  217. }
  218. }
  219. else { //now, it's not null
  220. if($comma) {
  221. $query .= ",";
  222. }
  223. else {
  224. $comma = true;
  225. }
  226. $query .= "`{$key}`";
  227. }
  228. } else {
  229. //just ignore unset values
  230. }
  231. }//end collecting column names
  232. //start collecting values
  233. $query .= ') VALUES (';
  234. //TODO: test for cases where there is not need for a value - ex. table with 1 autogenerated column
  235. //even if this is kinda stupid :P
  236. $comma = false;
  237. foreach($object as $key => $value) {
  238. //ignore the primary keys (usually id) if autogeneated
  239. if($autoincrement && in_array($key, $ids)) {
  240. continue;
  241. }
  242. //add VALUES(....)
  243. //right now we skip not set NULL values...but maybe we should reconsider for set to Null values (ex: $o->deadDate = null)
  244. if(isset($value)) {
  245. if($comma) {
  246. $query .= ", ";
  247. }
  248. else {
  249. $comma = true;
  250. }
  251. //based on it's type we quote the value
  252. switch(gettype($value)) {
  253. case 'string':
  254. $query .= sprintf("'%s'", addslashes($value));
  255. break;
  256. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  257. $query .= $value ? 'true' : 'false';
  258. break;
  259. case 'NULL' : //if $conditionValue is null the gettype($conditionValue) returns 'NULL'
  260. $query .= 'NULL';
  261. break;
  262. default:
  263. $query .= sprintf("%s", $value);
  264. }
  265. } else {
  266. if($nullify) { //should we set the unset values to null ?
  267. if($comma) {
  268. $query .= ", ";
  269. }
  270. else {
  271. $comma = true;
  272. }
  273. $query .= " NULL";
  274. }
  275. }
  276. }//end collecting values
  277. $query .= ')';
  278. // print $query;
  279. #exit();
  280. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  281. //EXECUTE
  282. $result = $this->executeSQL($query, $this->con);
  283. if($autoincrement) {//autogenerated ID
  284. // print "log: autoincrement used";
  285. return $this->lastId($this->con);
  286. }
  287. else { //"by hand" ids
  288. // print "log: by hand used";
  289. if(mysql_affected_rows($this->con)) {
  290. // print "log: affected";
  291. return true;
  292. } else {
  293. // print "log: not affected";
  294. return false;
  295. }
  296. }
  297. }
  298. /**
  299. * Get a number of object from the database
  300. * $tableName - table name
  301. * $conditions - AND like conditions ex: array('name'=>'alex', 'age'=>'31')
  302. * $orders - ORDER BY part ex: array('name'=>'ASC', 'age'=>'DESC')
  303. * $start - start offset
  304. * $nr - number of rows returned
  305. * author: alex
  306. */
  307. protected function getMultiple($tableName, $conditions = null, $orders=null, $start =null, $nr = null) {
  308. $objects = array(); //this will contain all the found objects
  309. $tableName = strtolower($tableName);
  310. //start query building
  311. $query = sprintf("SELECT * FROM `%s`", $tableName);
  312. //conditions
  313. if(count($conditions) > 0) {
  314. $query .= " WHERE ";
  315. $and = false;
  316. foreach($conditions as $conditionName=> $conditionValue) {
  317. if($and) {
  318. $query .= " AND ";
  319. }
  320. else {
  321. $and = true;
  322. }
  323. //based on it's type we quote the value
  324. switch(gettype($conditionValue)) {
  325. case 'string':
  326. $query .= sprintf(" `%s` = '%s'",$conditionName,addslashes($conditionValue));
  327. break;
  328. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  329. $query .= sprintf(" `%s` = %s",$conditionName, $conditionValue ? 'true' : 'false');
  330. break;
  331. case 'NULL' : //if $conditionValue is null the gettype($conditionValue) returns 'NULL'
  332. $query .= sprintf(" `%s` IS NULL",$conditionName);
  333. break;
  334. default:
  335. $query .= sprintf(" `%s` = %s",$conditionName,$conditionValue);
  336. }
  337. }
  338. }
  339. //add orders
  340. if(count($orders) > 0) {
  341. $query .= " ORDER BY ";
  342. $comma = false;
  343. foreach($orders as $order=>$direction) {
  344. if($comma) {
  345. $query .= sprintf(", `%s` %s ",$order,$direction);
  346. }
  347. else {
  348. $query .= sprintf(" `%s` %s",$order,$direction);
  349. $comma = true;
  350. }
  351. }
  352. }
  353. if(!is_null($start)) {
  354. $query .= sprintf(" LIMIT %d", $start);
  355. }
  356. if(!is_null($nr)) {
  357. $query .= sprintf(", %d", $nr);
  358. }
  359. #print $query;
  360. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  361. //EXECUTE query
  362. $result = $this->executeSQL($query, $this->con);
  363. $className = ucfirst($tableName);
  364. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  365. $object = new $className;
  366. $object->loadFromSQL($row);
  367. $objects[] = $object;
  368. }
  369. return $objects;
  370. }
  371. /**Return single */
  372. protected function getSingle($tableName, $conditions = null) {
  373. $foundedObjects = $this->getMultiple($tableName, $conditions);
  374. if(isset($foundedObjects) && count($foundedObjects) > 0 ){
  375. return $foundedObjects[0];
  376. }
  377. return;
  378. }
  379. /**Return single */
  380. protected function getCount($tableName, $conditions = null) {
  381. $foundedObjects = $this->getMultiple($tableName, $conditions);
  382. return count($foundedObjects);
  383. }
  384. /**Remove an entry from a table
  385. * param: $id the id of the entry
  386. * Returns true if data was deleted, false otherwise
  387. */
  388. protected function deprecated__delete($tableName, $id) {
  389. $query = sprintf("DELETE FROM `%s` WHERE id = '%s'" , strtolower($tableName), $id);
  390. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  391. $this->executeSQL($query, $this->con);
  392. if(mysql_affected_rows($this->con) > 0) {
  393. return true;
  394. } else {
  395. return false;
  396. }
  397. }
  398. /**Remove all entries from a table that met conditions
  399. * param: $conditions (an array of $key=>$value)
  400. * Returns true if data was deleted, false otherwise
  401. *
  402. * Ex: delete('user', array('id'=>1)) //delete the user with id 1
  403. * Ex2: delete('user') //delete ALL users
  404. */
  405. protected function delete($tableName, $conditions = null) {
  406. $tableName = strtolower($tableName);
  407. //start query building
  408. $query = sprintf("DELETE FROM `%s`", $tableName);
  409. //conditions
  410. if(count($conditions) > 0) {
  411. $query .= " WHERE ";
  412. $and = false;
  413. foreach($conditions as $conditionName=> $conditionValue) {
  414. if($and) {
  415. $query .= " AND ";
  416. }
  417. else {
  418. $and = true;
  419. }
  420. //based on it's type we quote the value
  421. switch(gettype($conditionValue)) {
  422. case 'string':
  423. $query .= sprintf(" %s = '%s'",$conditionName,addslashes($conditionValue));
  424. break;
  425. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  426. $query .= sprintf(" %s = %s",$conditionName, $conditionValue ? 'true' : 'false');
  427. break;
  428. default:
  429. $query .= sprintf(" %s = %s",$conditionName,$conditionValue);
  430. }
  431. }
  432. }
  433. // print $query;
  434. // exit();
  435. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  436. $this->executeSQL($query, $this->con);
  437. if(mysql_affected_rows($this->con) > 0) {
  438. return true;
  439. } else {
  440. return false;
  441. }
  442. }
  443. /**Remove an entry from a table, based on an object
  444. * Returns true if data was deleted, false otherwise
  445. */
  446. protected function deleteObject($object) {
  447. //detect class name
  448. if(empty($tableName)) {
  449. $tableName = strtolower(get_class($object));
  450. }
  451. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  452. return $this->delete($tableName, array('id'=>$object->id) );
  453. }
  454. /**Count the number of entries
  455. * param: $conditions (an array of $key=>$value)
  456. * Returns true if data was deleted, false otherwise
  457. *
  458. * Ex: count('user', array('id'=>1)) //return the number of users with id=1 (usually 1 or null)
  459. * Ex2: count('user') //return the number of users in the system
  460. */
  461. protected function count($tableName, $conditions = null) {
  462. $tableName = strtolower($tableName);
  463. //start query building
  464. $query = sprintf("SELECT COUNT(*) as nr FROM `%s`", $tableName);
  465. //conditions
  466. if(count($conditions) > 0) {
  467. $query .= " WHERE ";
  468. $and = false;
  469. foreach($conditions as $conditionName=> $conditionValue) {
  470. if($and) {
  471. $query .= " AND ";
  472. }
  473. else {
  474. $and = true;
  475. }
  476. //based on it's type we quote the value
  477. switch(gettype($conditionValue)) {
  478. case 'string':
  479. $query .= sprintf(" %s = '%s'",$conditionName,addslashes($conditionValue));
  480. break;
  481. case 'boolean': //special case as a 'false' value can not be concatenated with a string
  482. $query .= sprintf(" %s = %s",$conditionName, $conditionValue ? 'true' : 'false');
  483. break;
  484. default:
  485. $query .= sprintf(" %s = %s",$conditionName,$conditionValue);
  486. }
  487. }
  488. }
  489. #print $query;
  490. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__."{#}": null;
  491. $result = $this->executeSQL($query, $this->con);
  492. if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  493. return $row['nr'];
  494. }
  495. }
  496. /**************************************************************************/
  497. /*****************************USERS**************************************/
  498. /**************************************************************************/
  499. public function userGetByEmailAndPassword($email,$password) {
  500. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  501. return $this->getSingle('user', array('email'=>$email, 'password'=>md5($password) ));
  502. }
  503. public function userGetByEmail($email) {
  504. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  505. return $this->getSingle('user', array('email'=>$email));
  506. }
  507. public function userGetByAccount($account) {
  508. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  509. return $this->getSingle('user', array('account'=>$account));
  510. }
  511. public function userGetByEmailAndAccount($email, $account) {
  512. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  513. return $this->getSingle('user', array('email'=>$email, 'account'=>$account));
  514. }
  515. public function userGetByIdAndEncryptedPassword($id, $ePass) {
  516. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  517. return $this->getSingle('user', array('id'=>$id, 'password'=>$ePass));
  518. }
  519. public function userGetByEmailAndCryptedPassword($email,$cryptedPassword) {
  520. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  521. return $this->getSingle('user', array('email'=>$email, 'password'=>$cryptedPassword ));
  522. }
  523. public function userGetById($userId) {
  524. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  525. return $this->getSingle('user', array('id'=>$userId));
  526. }
  527. public function userGetAll() {
  528. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  529. return $this->getMultiple('user', null, array('email' => 'ASC'));
  530. }
  531. public function userUpdate($user) {
  532. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  533. return $this->update($user);
  534. }
  535. public function userCreate($user) {
  536. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  537. return $this->create($user);
  538. }
  539. /**Get all users that are collaborating to a diagram*/
  540. public function usersGetAsCollaboratorNative($diagramId){
  541. $users = array();
  542. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  543. $query ="select user.* from user, userdiagram
  544. where userdiagram.diagramId = ${diagramId}
  545. and userdiagram.userId = user.id
  546. order by name";
  547. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  548. // echo $query;
  549. //EXECUTE query
  550. $result = $this->executeSQL($query, $this->con);
  551. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  552. $user = new User();
  553. $user->loadFromSQL($row);
  554. $users[] = $user;
  555. }
  556. return $users;
  557. }
  558. /**Get all users that are not collaborating to a diagram*/
  559. public function usersGetNOTCollaboratorNative($diagramId){
  560. $users = array();
  561. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  562. /*See: http://stackoverflow.com/questions/5886095/whats-the-best-way-to-use-left-outer-join-to-check-for-non-existence-of-related*/
  563. $query ="select user.* from user
  564. left join userdiagram on (user.id = userdiagram.userId AND userdiagram.diagramId = ${diagramId})
  565. where userdiagram.userId is null
  566. order by name ";
  567. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  568. #echo $query;
  569. //EXECUTE query
  570. $result = $this->executeSQL($query, $this->con);
  571. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  572. $user = new User();
  573. $user->loadFromSQL($row);
  574. $users[] = $user;
  575. }
  576. return $users;
  577. }
  578. /**Get all "system" buddy for an user*/
  579. public function usersGetBuddies($userId){
  580. $users = array();
  581. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  582. $query = "select distinct user.* from user, userdiagram
  583. where user.id = userdiagram.userId
  584. and userdiagram.diagramId in
  585. (select d.id from diagram as d, userdiagram as ud
  586. where d.id = ud.diagramId
  587. and ud.userId = $userId)
  588. and user.id != $userId";
  589. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  590. // echo $query;
  591. //EXECUTE query
  592. $result = $this->executeSQL($query, $this->con);
  593. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  594. $user = new User();
  595. $user->loadFromSQL($row);
  596. $users[] = $user;
  597. }
  598. return $users;
  599. }
  600. public function userDeleteById($id) {
  601. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  602. return $this->delete('user', array('id' => $id));
  603. }
  604. /**Get the author of a diagram*/
  605. public function usersGetAuthorForDiagram($diagramId){
  606. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  607. $query ="select user.* from user, userdiagram
  608. where userdiagram.diagramId = ${diagramId}
  609. and userdiagram.level = 'author'
  610. and userdiagram.userId = user.id";
  611. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  612. // echo $query;
  613. //EXECUTE query
  614. $result = $this->executeSQL($query, $this->con);
  615. if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  616. $user = new User();
  617. $user->loadFromSQL($row);
  618. return $user;
  619. }
  620. else{
  621. return null;
  622. }
  623. }
  624. /**************************************************************************/
  625. /*****************************DIAGRAMS**************************************/
  626. /**************************************************************************/
  627. public function diagramCreate($entry) {
  628. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  629. return $this->create($entry);
  630. }
  631. public function diagramUpdate($diagram) {
  632. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  633. return $this->update($diagram);
  634. }
  635. public function diagramGetById($diagramId) {
  636. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  637. return $this->getSingle('diagram', array('id'=>$diagramId));
  638. }
  639. public function diagramGetByHash($hash) {
  640. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  641. return $this->getSingle('diagram', array('hash'=>$hash));
  642. }
  643. public function diagramCountByHash($hash){
  644. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  645. return $this->count('diagram', array('hash'=>$hash));
  646. }
  647. public function diagramsForUserNative($userId, $level){
  648. $diagrams = array();
  649. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  650. $query ="select diagram.* from diagram, userdiagram
  651. where userdiagram.userId = ${userId}
  652. and userdiagram.diagramId = diagram.id"
  653. . ( isset($level) ? " and userdiagram.level = '$level'" : '' )
  654. . " order by title";
  655. (DEBUG) ? $_SESSION['logs'][] = "&nbsp;&nbsp;&nbsp;&nbsp;" . __CLASS__ .'{#}'. __FUNCTION__ ."{#}{$query}{#}". __LINE__ : '';
  656. #echo $query;
  657. //EXECUTE query
  658. $result = $this->executeSQL($query, $this->con);
  659. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  660. $diagram = new Diagram();
  661. $diagram->loadFromSQL($row);
  662. $diagrams[] = $diagram;
  663. }
  664. return $diagrams;
  665. }
  666. /**This create a cascade delete to diagramdata*/
  667. public function diagramDelete($diagramId){
  668. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  669. return $this->delete('diagram', array('id'=>$diagramId));
  670. }
  671. public function diagramGetAllPublic() {
  672. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  673. return $this->getMultiple('diagram', array('public'=>true), array('createdDate'=>DESC));
  674. }
  675. public function diagramGetPublic($start, $end) {
  676. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  677. return $this->getMultiple('diagram', array('public'=>true), array('createdDate'=>DESC), $start, $end);
  678. }
  679. public function diagramCountGetAllPublic() {
  680. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  681. return $this->count('diagram', array('public'=>true));
  682. }
  683. /**************************************************************************/
  684. /*****************************DIAGRAMDATA**********************************/
  685. /**************************************************************************/
  686. public function diagramdataCreate($entry) {
  687. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  688. //$object, $ids = array('id'), $tableName=null, $nullify=false, $autoincrement=true) {
  689. return $this->create($entry, array('diagramId', 'type'), 'diagramdata', false, false);
  690. }
  691. public function diagramdataGetByDiagramIdAndType($diagramId, $type) {
  692. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  693. return $this->getSingle('diagramdata', array('diagramId'=>$diagramId, 'type'=>$type));
  694. }
  695. /**This create a cascade delete to diagramdata*/
  696. public function diagramdataDeleteByDiagramIdAndType($diagramId, $type){
  697. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  698. return $this->delete('diagramdata', array('diagramId'=>$diagramId, 'type'=>$type));
  699. }
  700. public function diagramdataGetByDiagramId($diagramId) {
  701. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  702. return $this->getMultiple('diagramdata', array('diagramId'=>$diagramId));
  703. }
  704. // public function diagramdataGetByDiagramHashAndType($hash, $type) {
  705. // (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  706. // return $this->getSingle('diagramdata', array('hash'=>$hash, 'type'=>$type));
  707. // }
  708. public function diagramdataUpdate($diagramdata) {
  709. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  710. return $this->update($diagramdata, array('diagramId', 'type'), 'diagramdata'); //do not update the key
  711. }
  712. /**************************************************************************/
  713. /*****************************INVITATION**************************************/
  714. /**************************************************************************/
  715. public function invitationCreate($invitation) {
  716. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  717. return $this->create($invitation);
  718. }
  719. public function invitationGetById($id) {
  720. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  721. return $this->getSingle('invitation', array('id'=>$id));
  722. }
  723. public function invitationGetAllForEmail($email) {
  724. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  725. return $this->getMultiple('invitation', array('email'=>$email), array('createdDate'=>'ASC'));
  726. }
  727. public function invitationGetAllForDiagram($diagramId) {
  728. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  729. return $this->getMultiple('invitation', array('diagramId'=>$diagramId));
  730. }
  731. public function invitationGetByToken($token) {
  732. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  733. return $this->getSingle('invitation', array('token'=>$token));
  734. }
  735. public function invitationDelete($id){
  736. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  737. return $this->delete('invitation', array('id'=>$id));
  738. }
  739. /**************************************************************************/
  740. /*****************************USERDIAGRAMS**************************************/
  741. /**************************************************************************/
  742. public function userdiagramCreate($entry) {
  743. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  744. return $this->create($entry, array('diagramId', 'userId'), 'userdiagram', false, false);
  745. }
  746. public function userdiagramGetByIds($userId, $diagramId) {
  747. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  748. return $this->getSingle('userdiagram', array('userId'=>$userId, 'diagramId'=>$diagramId));
  749. }
  750. public function userdiagramGetByAuthor($diagramId) {
  751. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  752. return $this->getSingle('userdiagram', array('diagramId'=>$diagramId, 'level'=> Userdiagram::LEVEL_AUTHOR));
  753. }
  754. public function userdiagramGetByDiagramId($diagramId) {
  755. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  756. return $this->getMultiple('userdiagram', array('diagramId'=>$diagramId));
  757. }
  758. public function userdiagramDelete($userId, $diagramId){
  759. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  760. return $this->delete('userdiagram', array('userId'=>$userId, 'diagramId'=>$diagramId));
  761. }
  762. public function userdiagramDeleteByUser($userId){
  763. (DEBUG) ? $_SESSION['logs'][] = __CLASS__ .'{#}'. __FUNCTION__ ."{#}{#}". __LINE__ : '';
  764. return $this->delete('userdiagram', array('userId' => $userId));
  765. }
  766. }
  767. ?>