A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
5.3 KiB

  1. <?php
  2. namespace SparkLib\Application;
  3. use \SparkLib\HTML;
  4. use \SparkLib\Application\Action;
  5. /**
  6. * A class to model links within Applications. Used by
  7. * Application&link(), etc.
  8. */
  9. class Link extends HTML implements Action {
  10. protected $_controller;
  11. protected $_id;
  12. protected $_action;
  13. protected $_url;
  14. protected $_anchor;
  15. protected $_params;
  16. protected $_title;
  17. protected $_type = '';
  18. protected $_target = '';
  19. protected $_redirectStatus = 302;
  20. /**
  21. * Make a new link.
  22. *
  23. * @param string base URL to link
  24. * @param string controller to link
  25. */
  26. public function __construct ($url, $controller)
  27. {
  28. $this->_url = $url;
  29. $this->_controller = $controller;
  30. }
  31. /**
  32. * get the controller
  33. *
  34. * @return string controller
  35. */
  36. public function getController ()
  37. {
  38. return $this->_controller;
  39. }
  40. /**
  41. * Set an id
  42. *
  43. * @param integer id
  44. * @return Link
  45. */
  46. public function id ($id)
  47. {
  48. $this->_id = $id;
  49. return $this;
  50. }
  51. /**
  52. * Set an action
  53. *
  54. * @param string action
  55. * @return Link
  56. */
  57. public function action ($action)
  58. {
  59. $this->_action = $action;
  60. return $this;
  61. }
  62. /**
  63. * get the action
  64. *
  65. * @return string action
  66. */
  67. public function getAction ()
  68. {
  69. return $this->_action;
  70. }
  71. /**
  72. * Set a target anchor.
  73. *
  74. * @param string name of target anchor
  75. * @return Link
  76. */
  77. public function anchor ($target)
  78. {
  79. $this->_anchor = (0 === strpos($target, '#') ? $target : '#' . $target);
  80. return $this;
  81. }
  82. /**
  83. * Set the target attribute.
  84. *
  85. * @param string name of the target type
  86. * @return Link
  87. */
  88. public function target ($target)
  89. {
  90. $this->_target = $target;
  91. return $this;
  92. }
  93. /**
  94. * Set a target type (html, json, csv, etc.)
  95. *
  96. * @param string name of type
  97. * @return Link
  98. */
  99. public function type ($type)
  100. {
  101. $this->_type = $type;
  102. return $this;
  103. }
  104. protected function renderType ()
  105. {
  106. if ($this->_type)
  107. return '.' . $this->_type;
  108. else
  109. return '';
  110. }
  111. /**
  112. * Set request parameters, eg: link/id?foo=bar&baz=bat
  113. *
  114. * @param mixed params can be an array or string of parameters
  115. * @return Link
  116. */
  117. public function params ($params)
  118. {
  119. if(is_array($params))
  120. $this->_params = '?' . http_build_query($params);
  121. elseif(is_string($params))
  122. $this->_params = (0 === strpos($params, '?') ? $params : '?' . $params);
  123. else
  124. throw new \Exception('Unknown param type in Link');
  125. return $this;
  126. }
  127. /**
  128. * Set a human-readable title.
  129. *
  130. * @param string $title
  131. * @return Link
  132. */
  133. public function title ($title)
  134. {
  135. $this->_title = $title;
  136. return $this;
  137. }
  138. /**
  139. * Set an HTTP status code in case we generate a Redirect later in
  140. * redirect().
  141. *
  142. * @param integer $status
  143. * @return Link
  144. */
  145. public function redirectStatus ($status)
  146. {
  147. $this->_redirectStatus = $status;
  148. return $this;
  149. }
  150. /**
  151. * Get the currently set title.
  152. *
  153. * @return string title, null if not set
  154. */
  155. public function getTitle ()
  156. {
  157. return $this->_title;
  158. }
  159. /**
  160. * Stringify the current link
  161. *
  162. * @return string current path
  163. */
  164. public function __toString ()
  165. {
  166. return $this->path();
  167. }
  168. /**
  169. * Return a link tag for the current link.
  170. *
  171. * @param string optional text for link
  172. * @param array optional attributes for a tag
  173. * @return string link tag
  174. */
  175. public function a ($linktext = null, array $attribs = array())
  176. {
  177. // If we didn't get any link text, come up with a default
  178. if (! isset($linktext))
  179. $linktext = $this->_controller . ' ' . $this->_action . ' ' . $this->_id;
  180. $attribs['href'] = $this->path();
  181. if (isset($this->_title))
  182. $attribs['title'] = $this->_title;
  183. if( $this->_target != '' )
  184. $attribs['target'] = $this->_target;
  185. return $this->makeTag('a', $attribs, $linktext);
  186. }
  187. /**
  188. * Kind of a silly utility function. Maybe we should make this more magical
  189. * and shiny in future.
  190. */
  191. public function a_titled ($linktext = null, array $attribs = array())
  192. {
  193. $title = mb_strtolower($linktext ? $linktext : $this->path());
  194. return $this->a($linktext, array('title' => $title));
  195. }
  196. /**
  197. * @return string path for current link
  198. */
  199. public function path ()
  200. {
  201. $path = array();
  202. $target = '';
  203. if (isset($this->_controller)) $path[] = $this->_controller;
  204. if (isset($this->_id)) $path[] = $this->_id;
  205. if (isset($this->_action)) $path[] = $this->_action;
  206. $base = rtrim($this->_url, '/');
  207. return $base . '/' . implode('/', $path) . $this->renderType() . $this->_params . $this->_anchor;
  208. }
  209. /**
  210. * @return \SparkLib\Application\Redirect for current link
  211. */
  212. public function redirect ($status = null)
  213. {
  214. if (isset($status))
  215. $this->redirectStatus($status);
  216. return new Redirect($this->path(), $this->_redirectStatus);
  217. }
  218. /**
  219. * If this is used as an Action, returned from a Controller action method
  220. * (yes I know the terminology overlaps confusingly), redirect.
  221. *
  222. * So basically you can say:
  223. *
  224. * $this->respondTo()->html = function () {
  225. * return $this->app()->link('orders');
  226. * };
  227. *
  228. * ...and it'll infer the ->redirect() call.
  229. */
  230. public function fire ()
  231. {
  232. $this->redirect()->fire();
  233. }
  234. }