PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/x2engine/protected/models/AmorphousModel.php

https://gitlab.com/e0/X2CRM
PHP | 134 lines | 64 code | 18 blank | 52 comment | 7 complexity | 2dd5ee21c41faa48d14e33d46a0b2e3d MD5 | raw file
  1. <?php
  2. /***********************************************************************************
  3. * X2CRM is a customer relationship management program developed by
  4. * X2Engine, Inc. Copyright (C) 2011-2016 X2Engine Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU Affero General Public License version 3 as published by the
  8. * Free Software Foundation with the addition of the following permission added
  9. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  10. * IN WHICH THE COPYRIGHT IS OWNED BY X2ENGINE, X2ENGINE DISCLAIMS THE WARRANTY
  11. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  15. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License along with
  19. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  20. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301 USA.
  22. *
  23. * You can contact X2Engine, Inc. P.O. Box 66752, Scotts Valley,
  24. * California 95067, USA. on our website at www.x2crm.com, or at our
  25. * email address: contact@x2engine.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * X2Engine" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by X2Engine".
  36. **********************************************************************************/
  37. /**
  38. * Model that assumes any attributes given to it.
  39. *
  40. * Intended for handling special data validation in Fields input.
  41. *
  42. * @package application.models
  43. * @author Demitri Morgan <demitri@x2engine.com>
  44. */
  45. class AmorphousModel extends CModel {
  46. private $_attributes = array();
  47. private $_mockFields = array();
  48. private $_tableName;
  49. public function __get($name){
  50. if($this->hasAttribute($name)){
  51. return $this->_attributes[$name];
  52. } else
  53. return parent::__get($name);
  54. }
  55. public function __set($name, $value){
  56. if($this->hasAttribute($name, $value)){
  57. $this->setAttribute($name, $value);
  58. } else
  59. parent::__set($name, $value);
  60. }
  61. public function addField(Fields $field,$name=null){
  62. $name = empty($name) ? $field->fieldName : $name;
  63. $this->_mockFields[$name] = $field;
  64. if(!isset($this->_attributes[$name])){
  65. $this->_attributes[$name] = '';
  66. }
  67. }
  68. public function attributeNames(){
  69. return array_keys($this->_attributes);
  70. }
  71. public function getAttribute($name){
  72. return $this->_attributes[$name];
  73. }
  74. public function getAttributeLabel($name){
  75. if(isset($this->_mockFields[$name])){
  76. return $this->_mockFields[$name]->attributeLabel;
  77. } else
  78. return null;
  79. }
  80. public function getAttributes($names = null){
  81. if($names == null)
  82. return $this->_attributes;
  83. else
  84. parent::getAttributes($names);
  85. }
  86. public function hasAttribute($name){
  87. return array_key_exists($name,$this->_attributes);
  88. }
  89. /**
  90. * Automatically generate rules from X2Model.
  91. *
  92. * The "required" validator is excluded because, when validating input for a
  93. * default value for a field that is required, blank should be a valid value
  94. * because otherwise having the field be required would force the user to
  95. * specify a non-blank default value.
  96. * @return array
  97. */
  98. public function rules(){
  99. $rules = X2Model::modelRules($this->_mockFields, $this);
  100. foreach(array_keys($rules) as $ind) {
  101. if(in_array($rules[$ind][1],array('required','unique','application.components.ValidLinkValidator')))
  102. unset($rules[$ind]);
  103. }
  104. return $rules;
  105. }
  106. public function setAttribute($name, $value){
  107. $this->_attributes[$name] = $value;
  108. }
  109. public function setTableName($value){
  110. $this->_tableName = $value;
  111. }
  112. public function tableName(){
  113. return $this->_tableName;
  114. }
  115. }
  116. ?>