/workflow/engine/services/rest/crud/Content.php

https://bitbucket.org/ferOnti/processmaker · PHP · 142 lines · 92 code · 18 blank · 32 comment · 9 complexity · ddaf8bc73019f6249d6c3c2bd80eb600 MD5 · raw file

  1. <?php
  2. class Services_Rest_Content
  3. {
  4. /**
  5. * Implementation for 'GET' method for Rest API
  6. *
  7. * @param mixed $conCategory, $conParent, $conId, $conLang Primary key
  8. *
  9. * @return array $result Returns array within multiple records or a single record depending if
  10. * a single selection was requested passing id(s) as param
  11. */
  12. protected function get($conCategory=null, $conParent=null, $conId=null, $conLang=null)
  13. {
  14. $result = array();
  15. try {
  16. $noArguments = true;
  17. $argumentList = func_get_args();
  18. foreach ($argumentList as $arg) {
  19. if (!is_null($arg)) {
  20. $noArguments = false;
  21. }
  22. }
  23. if ($noArguments) {
  24. $criteria = new Criteria('workflow');
  25. $criteria->addSelectColumn(ContentPeer::CON_CATEGORY);
  26. $criteria->addSelectColumn(ContentPeer::CON_PARENT);
  27. $criteria->addSelectColumn(ContentPeer::CON_ID);
  28. $criteria->addSelectColumn(ContentPeer::CON_LANG);
  29. $criteria->addSelectColumn(ContentPeer::CON_VALUE);
  30. $dataset = AppEventPeer::doSelectRS($criteria);
  31. $dataset->setFetchmode(ResultSet::FETCHMODE_ASSOC);
  32. while ($dataset->next()) {
  33. $result[] = $dataset->getRow();
  34. }
  35. } else {
  36. $record = ContentPeer::retrieveByPK($conCategory, $conParent, $conId, $conLang);
  37. if ($record) {
  38. $result = $record->toArray(BasePeer::TYPE_FIELDNAME);
  39. } else {
  40. $paramValues = "";
  41. foreach ($argumentList as $arg) {
  42. $paramValues .= (strlen($paramValues) ) ? ', ' : '';
  43. if (!is_null($arg)) {
  44. $paramValues .= "$arg";
  45. } else {
  46. $paramValues .= "NULL";
  47. }
  48. }
  49. throw new RestException(417, "table Content ($paramValues)" );
  50. }
  51. }
  52. } catch (RestException $e) {
  53. throw new RestException($e->getCode(), $e->getMessage());
  54. } catch (Exception $e) {
  55. throw new RestException(412, $e->getMessage());
  56. }
  57. return $result;
  58. }
  59. /**
  60. * Implementation for 'POST' method for Rest API
  61. *
  62. * @param mixed $conCategory, $conParent, $conId, $conLang Primary key
  63. *
  64. * @return array $result Returns array within multiple records or a single record depending if
  65. * a single selection was requested passing id(s) as param
  66. */
  67. protected function post($conCategory, $conParent, $conId, $conLang, $conValue)
  68. {
  69. try {
  70. $result = array();
  71. $obj = new Content();
  72. $obj->setConCategory($conCategory);
  73. $obj->setConParent($conParent);
  74. $obj->setConId($conId);
  75. $obj->setConLang($conLang);
  76. $obj->setConValue($conValue);
  77. $obj->save();
  78. } catch (Exception $e) {
  79. throw new RestException(412, $e->getMessage());
  80. }
  81. }
  82. /**
  83. * Implementation for 'PUT' method for Rest API
  84. *
  85. * @param mixed $conCategory, $conParent, $conId, $conLang Primary key
  86. *
  87. * @return array $result Returns array within multiple records or a single record depending if
  88. * a single selection was requested passing id(s) as param
  89. */
  90. protected function put($conCategory, $conParent, $conId, $conLang, $conValue)
  91. {
  92. try {
  93. $obj = ContentPeer::retrieveByPK($conCategory, $conParent, $conId, $conLang);
  94. $obj->setConValue($conValue);
  95. $obj->save();
  96. } catch (Exception $e) {
  97. throw new RestException(412, $e->getMessage());
  98. }
  99. }
  100. /**
  101. * Implementation for 'DELETE' method for Rest API
  102. *
  103. * @param mixed $conCategory, $conParent, $conId, $conLang Primary key
  104. *
  105. * @return array $result Returns array within multiple records or a single record depending if
  106. * a single selection was requested passing id(s) as param
  107. */
  108. protected function delete($conCategory, $conParent, $conId, $conLang)
  109. {
  110. $conn = Propel::getConnection(ContentPeer::DATABASE_NAME);
  111. try {
  112. $conn->begin();
  113. $obj = ContentPeer::retrieveByPK($conCategory, $conParent, $conId, $conLang);
  114. if (! is_object($obj)) {
  115. throw new RestException(412, 'Record does not exist.');
  116. }
  117. $obj->delete();
  118. $conn->commit();
  119. } catch (Exception $e) {
  120. $conn->rollback();
  121. throw new RestException(412, $e->getMessage());
  122. }
  123. }
  124. }