PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/contrib/civicrm/CRM/Utils/API/AbstractFieldCoder.php

https://gitlab.com/virtualrealms/d7civicrm
PHP | 158 lines | 62 code | 13 blank | 83 comment | 23 complexity | 3830b1a0f74bf6bafe3fab777b2cd09f MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------+
  4. | CiviCRM version 5 |
  5. +--------------------------------------------------------------------+
  6. | Copyright CiviCRM LLC (c) 2004-2019 |
  7. +--------------------------------------------------------------------+
  8. | This file is a part of CiviCRM. |
  9. | |
  10. | CiviCRM is free software; you can copy, modify, and distribute it |
  11. | under the terms of the GNU Affero General Public License |
  12. | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
  13. | |
  14. | CiviCRM is distributed in the hope that it will be useful, but |
  15. | WITHOUT ANY WARRANTY; without even the implied warranty of |
  16. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
  17. | See the GNU Affero General Public License for more details. |
  18. | |
  19. | You should have received a copy of the GNU Affero General Public |
  20. | License and the CiviCRM Licensing Exception along |
  21. | with this program; if not, contact CiviCRM LLC |
  22. | at info[AT]civicrm[DOT]org. If you have questions about the |
  23. | GNU Affero General Public License or the licensing of CiviCRM, |
  24. | see the CiviCRM license FAQ at http://civicrm.org/licensing |
  25. +--------------------------------------------------------------------+
  26. */
  27. /**
  28. * Base class for writing API_Wrappers which generically manipulate the content
  29. * of all fields (except for some black-listed skip-fields).
  30. *
  31. * @package CRM
  32. * @copyright CiviCRM LLC (c) 2004-2019
  33. */
  34. require_once 'api/Wrapper.php';
  35. /**
  36. * Class CRM_Utils_API_AbstractFieldCoder.
  37. */
  38. abstract class CRM_Utils_API_AbstractFieldCoder implements API_Wrapper {
  39. /**
  40. * Get skipped fields.
  41. *
  42. * @return array<string>
  43. * List of field names
  44. */
  45. public function getSkipFields() {
  46. return NULL;
  47. }
  48. /**
  49. * Is field skipped.
  50. *
  51. * @param string $fldName
  52. *
  53. * @return bool
  54. * TRUE if encoding should be skipped for this field
  55. */
  56. public function isSkippedField($fldName) {
  57. $skipFields = $this->getSkipFields();
  58. if ($skipFields === NULL) {
  59. return FALSE;
  60. }
  61. // Strip extra numbers from custom fields e.g. custom_32_1 should be custom_32
  62. if (strpos($fldName, 'custom_') === 0) {
  63. list($fldName, $customId) = explode('_', $fldName);
  64. $fldName .= '_' . $customId;
  65. }
  66. // Field should be skipped
  67. if (in_array($fldName, $skipFields)) {
  68. return TRUE;
  69. }
  70. // Field is multilingual and after cutting off _xx_YY should be skipped (CRM-7230)…
  71. if ((preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $fldName) && in_array(substr($fldName, 0, -6), $skipFields))) {
  72. return TRUE;
  73. }
  74. // Field can take multiple entries, eg. fieldName[1], fieldName[2], etc.
  75. // We remove the index and check again if the fieldName in the list of skipped fields.
  76. $matches = [];
  77. if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) {
  78. return TRUE;
  79. }
  80. return FALSE;
  81. }
  82. /**
  83. * Going to filter the submitted values.
  84. *
  85. * @param array|string $values the field value from the API
  86. */
  87. abstract public function encodeInput(&$values);
  88. /**
  89. * Decode output.
  90. *
  91. * @param string $values
  92. *
  93. * @return mixed
  94. */
  95. abstract public function decodeOutput(&$values);
  96. /**
  97. * @inheritDoc
  98. */
  99. public function fromApiInput($apiRequest) {
  100. $lowerAction = strtolower($apiRequest['action']);
  101. if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create'])) {
  102. // note: 'getsingle', 'replace', 'update', and chaining all build on top of 'get'/'create'
  103. foreach ($apiRequest['params'] as $key => $value) {
  104. // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
  105. // and don't apply to other skippable fields
  106. if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
  107. $this->encodeInput($apiRequest['params'][$key]);
  108. }
  109. }
  110. }
  111. elseif ($apiRequest['version'] == 3 && $lowerAction == 'setvalue') {
  112. if (isset($apiRequest['params']['field']) && isset($apiRequest['params']['value'])) {
  113. if (!$this->isSkippedField($apiRequest['params']['field'])) {
  114. $this->encodeInput($apiRequest['params']['value']);
  115. }
  116. }
  117. }
  118. return $apiRequest;
  119. }
  120. /**
  121. * @inheritDoc
  122. */
  123. public function toApiOutput($apiRequest, $result) {
  124. $lowerAction = strtolower($apiRequest['action']);
  125. if ($apiRequest['version'] == 3 && in_array($lowerAction, ['get', 'create', 'setvalue', 'getquick'])) {
  126. foreach ($result as $key => $value) {
  127. // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo')
  128. // and don't apply to other skippable fields
  129. if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) {
  130. $this->decodeOutput($result[$key]);
  131. }
  132. }
  133. }
  134. // setvalue?
  135. return $result;
  136. }
  137. /**
  138. * @param $key
  139. *
  140. * @return bool
  141. */
  142. protected function isApiControlField($key) {
  143. return (FALSE !== strpos($key, '.'));
  144. }
  145. }