PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/src/Application/Service/UserInterface.php

http://github.com/concrete5/concrete5
PHP | 417 lines | 259 code | 45 blank | 113 comment | 41 complexity | e8bdea0b7ac25aea5127a2bd9a7ef116 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Core\Application\Service;
  3. use Concrete\Core\Http\Response;
  4. use HtmlObject\Traits\Tag;
  5. use PermissionKey;
  6. use Concrete\Core\User\User as ConcreteUser;
  7. use Concrete\Core\Support\Facade\Application;
  8. use Loader;
  9. use Core;
  10. use Page;
  11. use Config;
  12. use Session;
  13. use Concrete\Core\View\ErrorView;
  14. use stdClass;
  15. /**
  16. * Useful functions for generating elements on the Concrete interface.
  17. *
  18. * @subpackage Concrete
  19. * \@package Helpers
  20. *
  21. * @author Andrew Embler <andrew@concrete5.org>
  22. * @copyright Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
  23. * @license http://www.concrete5.org/license/ MIT License
  24. */
  25. class UserInterface
  26. {
  27. public static $menuItems = [];
  28. /**
  29. * Generates a submit button in the Concrete style.
  30. *
  31. * @param string $text The text of the button
  32. * @param bool|string $formID The form this button will submit
  33. * @param string $buttonAlign
  34. * @param string $innerClass
  35. * @param array $args Extra args passed to the link
  36. *
  37. * @return string
  38. */
  39. public function submit($text, $formID = false, $buttonAlign = 'right', $innerClass = null, $args = [])
  40. {
  41. if ('right' == $buttonAlign) {
  42. $innerClass .= ' pull-right';
  43. } elseif ('left' == $buttonAlign) {
  44. $innerClass .= ' pull-left';
  45. }
  46. if (!$formID) {
  47. $formID = 'button';
  48. }
  49. $argsstr = '';
  50. foreach ($args as $k => $v) {
  51. $argsstr .= $k . '="' . $v . '" ';
  52. }
  53. return '<input type="submit" class="btn ' . $innerClass . '" value="' . $text . '" id="ccm-submit-' . $formID . '" name="ccm-submit-' . $formID . '" ' . $argsstr . ' />';
  54. }
  55. /**
  56. * Generates a simple link button in the Concrete style.
  57. *
  58. * @param string $text The text of the button
  59. * @param string $href
  60. * @param string $buttonAlign
  61. * @param string $innerClass
  62. * @param array $args Extra args passed to the link
  63. *
  64. * @return string
  65. */
  66. public function button($text, $href, $buttonAlign = 'right', $innerClass = null, $args = [])
  67. {
  68. if ('right' == $buttonAlign) {
  69. $innerClass .= ' pull-right';
  70. } elseif ('left' == $buttonAlign) {
  71. $innerClass .= ' pull-left';
  72. }
  73. $argsstr = '';
  74. foreach ($args as $k => $v) {
  75. $argsstr .= $k . '="' . $v . '" ';
  76. }
  77. return '<a href="' . $href . '" class="btn btn-default ' . $innerClass . '" ' . $argsstr . '>' . $text . '</a>';
  78. }
  79. /**
  80. * Generates a JavaScript function button in the Concrete style.
  81. *
  82. * @param string $text The text of the button
  83. * @param string $onclick
  84. * @param string $buttonAlign
  85. * @param string $innerClass - no longer used
  86. * @param array $args Extra args passed to the link
  87. *
  88. * @return string
  89. */
  90. public function buttonJs($text, $onclick, $buttonAlign = 'right', $innerClass = null, $args = [])
  91. {
  92. if ('right' == $buttonAlign) {
  93. $innerClass .= ' pull-right';
  94. } elseif ('left' == $buttonAlign) {
  95. $innerClass .= ' pull-left';
  96. }
  97. $argsstr = '';
  98. foreach ($args as $k => $v) {
  99. $argsstr .= $k . '="' . $v . '" ';
  100. }
  101. return '<input type="button" class="btn btn-default ' . $innerClass . '" value="' . $text . '" onclick="' . $onclick . '" ' . $buttonAlign . ' ' . $argsstr . ' />';
  102. }
  103. /**
  104. * @deprecated
  105. */
  106. public function button_js($text, $onclick, $buttonAlign = 'right', $innerClass = null, $args = [])
  107. {
  108. return self::buttonJs($text, $onclick, $buttonAlign, $innerClass, $args);
  109. }
  110. /**
  111. * Outputs button text passed as arguments with a special Concrete wrapper for positioning
  112. * <code>
  113. * $bh->buttons($myButton1, $myButton2, $myButton3);
  114. * </code>.
  115. *
  116. * @param string $buttons
  117. *
  118. * @return string
  119. */
  120. public function buttons($buttons = null)
  121. {
  122. if (!is_array($buttons)) {
  123. $buttons = func_get_args();
  124. }
  125. $html = '<div class="ccm-buttons well">';
  126. foreach ($buttons as $_html) {
  127. $html .= $_html . ' ';
  128. }
  129. $html .= '</div>';
  130. return $html;
  131. }
  132. /**
  133. * @param \Concrete\Core\Page\Page $c
  134. *
  135. * @return string
  136. */
  137. public function getQuickNavigationLinkHTML($c)
  138. {
  139. $cnt = Loader::controller($c);
  140. if (method_exists($cnt, 'getQuickNavigationLinkHTML')) {
  141. return $cnt->getQuickNavigationLinkHTML();
  142. } else {
  143. return '<a href="' . Core::make('helper/navigation')->getLinkToCollection($c) . '">' . $c->getCollectionName() . '</a>';
  144. }
  145. }
  146. /**
  147. * @return bool
  148. */
  149. public function showWhiteLabelMessage()
  150. {
  151. return Config::get('concrete.white_label.logo') || file_exists(DIR_APPLICATION . '/' . DIRNAME_IMAGES . '/logo_menu.png');
  152. }
  153. /**
  154. * @return string
  155. */
  156. public function getToolbarLogoSRC()
  157. {
  158. $alt = false;
  159. $src = false;
  160. if (Config::get('concrete.white_label.name')) {
  161. $alt = Config::get('concrete.white_label.name');
  162. }
  163. if (!$alt) {
  164. $alt = 'concrete5';
  165. }
  166. if (Config::get('concrete.white_label.logo')) {
  167. $src = Config::get('concrete.white_label.logo');
  168. }
  169. if (!$src) {
  170. $filename = 'logo';
  171. if (file_exists(DIR_APPLICATION . '/' . DIRNAME_IMAGES . '/' . $filename . '.svg')) {
  172. $src = REL_DIR_APPLICATION . '/' . DIRNAME_IMAGES . '/' . $filename . '.svg';
  173. } elseif (file_exists(DIR_APPLICATION . '/' . DIRNAME_IMAGES . '/' . $filename . '.png')) {
  174. $src = REL_DIR_APPLICATION . '/' . DIRNAME_IMAGES . '/' . $filename . '.png';
  175. } else {
  176. $src = ASSETS_URL_IMAGES . '/' . $filename . '.svg';
  177. }
  178. }
  179. return '<img id="ccm-logo" src="' . $src . '" alt="' . $alt . '" title="' . $alt . '">';
  180. }
  181. /**
  182. * @deprecated The Newsflow Overlay feature has been removed
  183. *
  184. * @return bool
  185. */
  186. public function showNewsflowOverlay()
  187. {
  188. return false;
  189. }
  190. /**
  191. * Shall we show the introductive help overlay?
  192. *
  193. * @return bool
  194. */
  195. public function showHelpOverlay()
  196. {
  197. $result = false;
  198. if (Config::get('concrete.misc.help_overlay')) {
  199. $app = Application::getFacadeApplication();
  200. $u = $app->make(ConcreteUser::class);
  201. $timestamp = $u->config('MAIN_HELP_LAST_VIEWED');
  202. if (!$timestamp) {
  203. $result = true;
  204. }
  205. }
  206. return $result;
  207. }
  208. public function trackHelpOverlayDisplayed()
  209. {
  210. $app = Application::getFacadeApplication();
  211. $u = $app->make(ConcreteUser::class);
  212. $u->saveConfig('MAIN_HELP_LAST_VIEWED', time());
  213. }
  214. /**
  215. * Clears the Interface Items Cache (clears the session).
  216. */
  217. public function clearInterfaceItemsCache()
  218. {
  219. $app = Application::getFacadeApplication();
  220. $u = $app->make(ConcreteUser::class);
  221. if ($u->isRegistered()) {
  222. Session::remove('dashboardMenus');
  223. }
  224. }
  225. /**
  226. * Cache the interface items.
  227. */
  228. public function cacheInterfaceItems()
  229. {
  230. $app = Application::getFacadeApplication();
  231. $u = $app->make(ConcreteUser::class);
  232. if ($u->isRegistered()) {
  233. Core::make('helper/concrete/dashboard')->getIntelligentSearchMenu();
  234. }
  235. }
  236. /**
  237. * @param \Concrete\Core\Page\Page[] $tabs
  238. *
  239. * @return string
  240. */
  241. public function pagetabs($tabs)
  242. {
  243. $tcn = rand(0, getrandmax());
  244. $html = '<ul class="nav-tabs nav" id="ccm-tabs-' . $tcn . '">';
  245. $c = Page::getCurrentPage();
  246. foreach ($tabs as $t) {
  247. if (is_array($t)) {
  248. $name = $t[1];
  249. $_c = $t[0];
  250. } else {
  251. $_c = $t;
  252. $name = $t->getCollectionName();
  253. }
  254. $href = Core::make('helper/navigation')->getLinkToCollection($_c);
  255. $active = false;
  256. if (is_object($c) && $c->getCollectionID() == $_c->getCollectionID()) {
  257. $active = true;
  258. }
  259. $html .= '<li class="' . (($active) ? 'active' : '') . '"><a href="' . $href . '">' . $name . '</a></li>';
  260. }
  261. $html .= '</ul>';
  262. return $html;
  263. }
  264. /**
  265. * @param \Concrete\Core\Page\Page[] $tabs
  266. * @param bool $jstabs
  267. * @param string $callback
  268. *
  269. * @return string
  270. */
  271. public function tabs($tabs, $jstabs = true, $callback = 'ccm_activateTabBar')
  272. {
  273. $tcn = rand(0, getrandmax());
  274. $html = '<ul class="nav-tabs nav" id="ccm-tabs-' . $tcn . '">';
  275. foreach ($tabs as $t) {
  276. $dt = $t[0];
  277. $href = '#';
  278. if (!$jstabs) {
  279. $dt = '';
  280. $href = $t[0];
  281. }
  282. $html .= '<li class="' . ((isset($t[2]) && true == $t[2]) ? 'active' : '') . '"><a href="' . $href . '" data-tab="' . $dt . '">' . $t[1] . '</a></li>';
  283. }
  284. $html .= '</ul>';
  285. if ($jstabs) {
  286. $html .= '<script type="text/javascript">$(function() { ' . $callback . '($(\'#ccm-tabs-' . $tcn . '\'));});</script>';
  287. }
  288. return $html;
  289. }
  290. /**
  291. * @param string $title
  292. * @param string $error
  293. * @param bool|\Exception $exception
  294. */
  295. public function renderError($title, $error, $exception = false)
  296. {
  297. Response::closeOutputBuffers(1, false);
  298. $this->buildErrorResponse($title, $error, $exception)->send();
  299. }
  300. /**
  301. * @param string $title
  302. * @param string $error
  303. * @param bool|\Exception $exception
  304. *
  305. * @return Response;
  306. */
  307. public function buildErrorResponse($title, $error, $exception = false)
  308. {
  309. $o = new stdClass();
  310. $o->title = $title;
  311. $o->content = $error;
  312. if ($exception) {
  313. $o->content .= $exception->getTraceAsString();
  314. }
  315. $ve = new ErrorView($o);
  316. $contents = $ve->render($o);
  317. return Response::create($contents, Response::HTTP_INTERNAL_SERVER_ERROR);
  318. }
  319. /**
  320. * @param array $arguments
  321. *
  322. * @return string
  323. */
  324. public function notify($arguments)
  325. {
  326. $defaults = [
  327. 'type' => 'success',
  328. 'icon' => 'fa fa-check-mark',
  329. 'title' => false,
  330. 'text' => false,
  331. 'form' => false,
  332. 'hide' => false,
  333. 'addclass' => 'ccm-notification-page-alert',
  334. 'buttons' => [],
  335. ];
  336. // overwrite all the defaults with the arguments
  337. $arguments = array_merge($defaults, $arguments);
  338. $arguments['addclass'] .= ' ccm-ui';
  339. $text = '';
  340. if ($arguments['form']) {
  341. $text .= $arguments['form'];
  342. }
  343. $text .= $arguments['text'];
  344. if (count($arguments['buttons']) > 0) {
  345. $text .= '<div class="ccm-notification-inner-buttons">';
  346. if (1 == count($arguments['buttons'])) {
  347. $singleButton = $arguments['buttons'][0];
  348. if ($singleButton instanceof Tag) {
  349. $singleButton->addClass('btn btn-xs btn-default');
  350. }
  351. $text .= '<div>' . $singleButton . '</div>';
  352. } else {
  353. $text .= '<div class="btn-group"><button type="button" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . t('Action') . ' <span class="caret"></span></button><ul class="dropdown-menu">';
  354. foreach ($arguments['buttons'] as $button) {
  355. $text .= '<li>' . $button . '</li>';
  356. }
  357. $text .= '</ul></div>';
  358. }
  359. $text .= '</div>';
  360. }
  361. if ($arguments['form']) {
  362. $text .= '</form>';
  363. }
  364. $arguments['text'] = $text;
  365. unset($arguments['buttons']);
  366. $string = json_encode($arguments);
  367. $content = '<script type="text/javascript">$(function() {';
  368. $content .= 'new PNotify(' . $string . ');';
  369. $content .= '});</script>';
  370. return $content;
  371. }
  372. }