Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. 'router' => array(
  2. 'routes' => array(
  3. 'user-management.rest.projects' => array(
  4. 'type' => 'Segment',
  5. 'options' => array(
  6. 'route' => '/projects[/:projects_name]',
  7. 'defaults' => array(
  8. 'controller' => 'UserManagement\\V1\\Rest\\Projects\\Controller',
  9. ),
  10. ),
  11. ),
  12. 'user-management.rest.rightscontexts' => array(
  13. 'type' => 'Segment',
  14. 'options' => array(
  15. 'route' => '/rightscontexts[/:rightscontexts_name]',
  16. 'defaults' => array(
  17. 'controller' => 'UserManagement\\V1\\Rest\\Rightscontexts\\Controller',
  18. ),
  19. ),
  20. ),
  21. ),
  22. ),
  23.  
  24. <?php
  25. namespace UserManagement\V1\Rest\Projects;
  26.  
  27. use Zend\Db\Adapter\AdapterInterface;
  28. use Zend\Db\TableGateway\TableGateway;
  29. use ZF\ApiProblem\ApiProblem;
  30. use ZF\Rest\AbstractResourceListener;
  31. use BQS\Codepage\Conversion\InputToOutput;
  32. use Zend\Db\Sql\Select;
  33. use ZF\Hal\Link\Link;
  34. use UserManagement\V1\Rest\Rightscontexts\RightscontextsEntity;
  35. use UserManagement\V1\Rest\Rightscontexts\RightscontextsCollection;
  36. use Zend\Paginator\Paginator;
  37.  
  38. class ProjectsResource extends AbstractResourceListener
  39. {
  40. protected $adapter;
  41. protected $cpcClientToDb;
  42. protected $cpcDbToClient;
  43.  
  44. public function __construct(AdapterInterface $adapter, InputToOutput $cpcClientToDb, InputToOutput $cpcDbToClient)
  45. {
  46. $this->adapter = $adapter;
  47. $this->cpcClientToDb = $cpcClientToDb;
  48. $this->cpcDbToClient = $cpcDbToClient;
  49. }
  50.  
  51. /**
  52. * Create a resource
  53. *
  54. * @param mixed $data
  55. * @return ApiProblem|mixed
  56. */
  57. public function create($data)
  58. {
  59. return new ApiProblem(405, 'The POST method has not been defined');
  60. }
  61.  
  62. /**
  63. * Delete a resource
  64. *
  65. * @param mixed $id
  66. * @return ApiProblem|mixed
  67. */
  68. public function delete($id)
  69. {
  70. return new ApiProblem(405, 'The DELETE method has not been defined for individual resources');
  71. }
  72.  
  73. /**
  74. * Delete a collection, or members of a collection
  75. *
  76. * @param mixed $data
  77. * @return ApiProblem|mixed
  78. */
  79. public function deleteList($data)
  80. {
  81. return new ApiProblem(405, 'The DELETE method has not been defined for collections');
  82. }
  83.  
  84. /**
  85. * Fetch a resource
  86. *
  87. * @param mixed $id
  88. * @return ApiProblem|mixed
  89. */
  90. public function fetch($id)
  91. {
  92. $table = new TableGateway('web_project', $this->adapter);
  93.  
  94. $rowset = $table->select(function($select) use ($id) {
  95. $select->columns(array('projectname' => 'projectname', 'projectdescription' => 'description', 'projectcreationdate' => 'creationdate'));
  96. $select->join(
  97. array('t1' => 'web_prjcontext'),
  98. 't1.fk_web_project = web_project.id',
  99. array('contextname' => 'name', 'contextdescription' => 'description'),
  100. Select::JOIN_LEFT
  101. );
  102. $select->join(
  103. array('t2' => 'web_prjright'),
  104. 't2.fk_web_prjcontext = t1.id',
  105. array('rightname' => 'name', 'rightmatrix' => 'matrix', 'rightdescription' => 'description'),
  106. Select::JOIN_LEFT
  107. );
  108. $select->where(array('projectname' => $id));
  109. $select->order(array('t1.name asc'));
  110. $select->order(array('t2.name asc'));
  111. });
  112.  
  113. $dataEntity = array();
  114. $dataEmbedded = array();
  115.  
  116. foreach ($rowset as $row) {
  117. if (!isset($data['name'])) {
  118. $dataEntity['name'] = $this->cpcDbToClient->convert($row->projectname);
  119. $dataEntity['description'] = $this->cpcDbToClient->convert(stream_get_contents($row->projectdescription));
  120. $dataEntity['creationDate'] = $row->projectcreationdate;
  121. }
  122.  
  123. if ($row->contextname) {
  124. $contextName = $row->contextname;
  125.  
  126. if (!isset($dataEmbedded[$contextName])) {
  127. $dataEmbedded[$contextName] = array();
  128. }
  129.  
  130. if (!isset($dataEmbedded[$contextName]['name'])) {
  131. $dataEmbedded[$contextName]['name'] = $this->cpcDbToClient->convert($contextName);
  132. $dataEmbedded[$contextName]['description'] = $this->cpcDbToClient->convert($row->contextdescription);
  133. $dataEmbedded[$contextName]['rights'] = array();
  134. }
  135.  
  136. if ($row->rightname) {
  137. $rightName = $row->rightname;
  138.  
  139. $dataEmbedded[$contextName]['rights'][$rightName]['name'] = $this->cpcDbToClient->convert($rightName);
  140. $dataEmbedded[$contextName]['rights'][$rightName]['description'] = $this->cpcDbToClient->convert($row->rightdescription);
  141. $dataEmbedded[$contextName]['rights'][$rightName]['matrix']['e'] = $row->rightmatrix[0];
  142. $dataEmbedded[$contextName]['rights'][$rightName]['matrix']['c'] = $row->rightmatrix[1];
  143. $dataEmbedded[$contextName]['rights'][$rightName]['matrix']['r'] = $row->rightmatrix[2];
  144. $dataEmbedded[$contextName]['rights'][$rightName]['matrix']['u'] = $row->rightmatrix[3];
  145. $dataEmbedded[$contextName]['rights'][$rightName]['matrix']['d'] = $row->rightmatrix[4];
  146. }
  147. }
  148. }
  149.  
  150. $project = new ProjectsEntity($dataEntity);
  151.  
  152. $project->rightsContexts = new RightscontextsCollection(
  153. new \Zend\Paginator\Adapter\ArrayAdapter($dataEmbedded)
  154. );
  155.  
  156. return $project;
  157. }
  158.  
  159. /**
  160. * Fetch all or a subset of resources
  161. *
  162. * @param array $params
  163. * @return ApiProblem|mixed
  164. */
  165. public function fetchAll($params = array())
  166. {
  167. return new ApiProblem(405, 'The GET method has not been defined for collections');
  168. }
  169.  
  170. /**
  171. * Patch (partial in-place update) a resource
  172. *
  173. * @param mixed $id
  174. * @param mixed $data
  175. * @return ApiProblem|mixed
  176. */
  177. public function patch($id, $data)
  178. {
  179. return new ApiProblem(405, 'The PATCH method has not been defined for individual resources');
  180. }
  181.  
  182. /**
  183. * Replace a collection or members of a collection
  184. *
  185. * @param mixed $data
  186. * @return ApiProblem|mixed
  187. */
  188. public function replaceList($data)
  189. {
  190. return new ApiProblem(405, 'The PUT method has not been defined for collections');
  191. }
  192.  
  193. /**
  194. * Update a resource
  195. *
  196. * @param mixed $id
  197. * @param mixed $data
  198. * @return ApiProblem|mixed
  199. */
  200. public function update($id, $data)
  201. {
  202. return new ApiProblem(405, 'The PUT method has not been defined for individual resources');
  203. }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement