SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/LinkSpecExternal.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. class LinkSpecExternal extends LinkSpec
  17. {
  18.  
  19. /**
  20.   * Get the URL for a given link
  21.   **/
  22. public function getUrl($specdata)
  23. {
  24. return $specdata;
  25. }
  26.  
  27.  
  28. /**
  29.   * Get any extra html attributes to use for a given link
  30.   * @return array
  31.   **/
  32. public function getAttrs($specdata)
  33. {
  34. return array(
  35. 'target' => '_blank',
  36. );
  37. }
  38.  
  39.  
  40. /**
  41.   * Get the HTML to use for editing a given linkspec
  42.   *
  43.   * The HTML should create a HTML field with the name $field_name
  44.   * If there is a spec currently being edited, the specdata will
  45.   * be provided in $curr_specdata
  46.   **/
  47. public function getEditForm($field_name, $curr_specdata)
  48. {
  49. Form::setData([$field_name => $curr_specdata]);
  50. Form::nextFieldDetails('External URL', true);
  51. return Form::text($field_name, ['placeholder' => 'e.g. http://www.example.com']);
  52. }
  53.  
  54.  
  55. /**
  56.   * Validate the submission, for instances where certain constraints apply
  57.   **/
  58. public function isValid($specdata)
  59. {
  60. if (!preg_match('%^(https?://)([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $specdata)) {
  61. return new AdminError('External links must begin with "http://" or "https://"');
  62. }
  63.  
  64. return true;
  65. }
  66.  
  67. }
  68.