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

/application/modules/CMSFactory/PropelBaseModelClass.php

http://github.com/imagecms/ImageCMS
PHP | 343 lines | 226 code | 60 blank | 57 comment | 36 complexity | 57686ff2147b8f9ea204f429245e85ec MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. namespace CMSFactory;
  3. use CMSFactory\DependencyInjection\DependencyInjectionProvider;
  4. use Currency\Currency;
  5. use CustomFieldsData;
  6. use CustomFieldsDataQuery;
  7. use CustomFieldsQuery;
  8. use Doctrine\Common\Cache\CacheProvider;
  9. use MY_Controller;
  10. use MY_Form_validation;
  11. use Propel\Runtime\Connection\ConnectionInterface;
  12. class PropelBaseModelClass implements \Serializable
  13. {
  14. public $customFields;
  15. public $customData;
  16. public $hasCustomData = false;
  17. public $entityId;
  18. private $entities_locale = [
  19. 'product',
  20. 'category',
  21. 'brand',
  22. ];
  23. /**
  24. * @param MY_Form_validation $validator
  25. * @return mixed
  26. */
  27. public function validateCustomData($validator) {
  28. if (!empty($this->entityName)) {
  29. $this->collectCustomData($this->entityName, $this->getId());
  30. if ($this->hasCustomData !== false) {
  31. foreach ($_POST['custom_field'] as $key_post => $value_post) {
  32. foreach ($this->customFields as $key => $value) {
  33. if ((int) $key_post == $value['Id']) {
  34. $validator_str = '';
  35. if ($value['IsRequired'] && $this->curentPostEntitySave($key)) {
  36. $validator_str = 'required';
  37. }
  38. if ($value['Validators'] && $this->curentPostEntitySave($key)) {
  39. $validator_str .= '|' . $value['Validators'];
  40. }
  41. $name = array_shift($value['CustomFieldsI18ns'])['FieldLabel'];
  42. $validator->set_rules("custom_field[$key]", $name, $validator_str);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. return $validator;
  49. }
  50. /**
  51. * @param string $entityName
  52. * @param int $id
  53. * @return bool
  54. */
  55. public function collectCustomData($entityName, $id) {
  56. $this->entityId = $id;
  57. $this->customFields = CustomFieldsQuery::create()
  58. ->joinWithI18n(MY_Controller::defaultLocale())
  59. ->filterByIsActive(1)
  60. ->filterByEntity($entityName)
  61. ->find()
  62. ->toArray($keyColumn = 'id');
  63. if (count($this->customFields)) {
  64. $this->hasCustomData = true;
  65. } else {
  66. return false;
  67. }
  68. }
  69. /**
  70. * @param int $key
  71. * @return bool
  72. */
  73. public function curentPostEntitySave($key) {
  74. $entity = CustomFieldsQuery::create()->findPk($key);
  75. if ($entity) {
  76. return $entity->getEntity() == $this->entityName;
  77. }
  78. }
  79. public function saveCustomData() {
  80. $locale = in_array($this->entityName, $this->entities_locale, true) ? chose_language() : MY_Controller::defaultLocale();
  81. if ($this->hasCustomData === false) {
  82. $this->collectCustomData($this->entityName, $this->getId());
  83. }
  84. $data = $_POST['custom_field'];
  85. if (!$data) {
  86. return;
  87. }
  88. $newFields = [];
  89. foreach ($this->customFields as $fieldObject) {
  90. if (!array_key_exists($fieldObject['Id'], $data)) {
  91. $data[$fieldObject['Id']] = '';
  92. $newFields[] = $fieldObject['Id'];
  93. }
  94. foreach ($data as $key => $value) {
  95. if ((int) $key == $fieldObject['Id']) {
  96. $objCustomData = CustomFieldsDataQuery::create()
  97. ->filterByentityId($this->entityId)
  98. ->filterByfieldId($key)
  99. ->filterByLocale($locale)
  100. ->findOne();
  101. if ($objCustomData) {
  102. if (!in_array($key, $newFields)) {
  103. $objCustomData->setdata($value);
  104. $objCustomData->save();
  105. }
  106. break;
  107. } else {
  108. $fieldObject = new CustomFieldsData();
  109. $fieldObject->setentityId($this->entityId);
  110. $fieldObject->setfieldId($key);
  111. $fieldObject->setdata($value);
  112. $fieldObject->setLocale($locale);
  113. $fieldObject->save();
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * @param $attributeName
  122. * @return mixed
  123. */
  124. public function getLabel($attributeName) {
  125. if (method_exists($this, 'attributeLabels')) {
  126. $labels = $this->attributeLabels();
  127. if (isset($labels[$attributeName])) {
  128. return $labels[$attributeName];
  129. } else {
  130. return $attributeName;
  131. }
  132. }
  133. }
  134. /**
  135. * @param string $column
  136. * @return bool
  137. */
  138. public function getVirtual($column) {
  139. $column = strtolower($column);
  140. return $this->getVirtualColumn($column);
  141. }
  142. /**
  143. * @param string $name
  144. * @return bool
  145. */
  146. public function getVirtualColumn($name) {
  147. $name = strtolower($name);
  148. if (!$this->hasVirtualColumn($name)) {
  149. return false;
  150. }
  151. return parent::getVirtualColumn($name);
  152. }
  153. /**
  154. * Convert model attribute(by default Price). e.g. "99.99 $"
  155. *
  156. * @param string $attributeName Optional. Attribute name to convert.
  157. * @access public
  158. * @return string
  159. */
  160. public function toCurrency($attributeName = 'Price', $cId = null, $convertForTemplate = false) {
  161. $attributeName = strtolower($attributeName);
  162. $get = 'get' . $attributeName;
  163. if (!$convertForTemplate) {
  164. if ($attributeName == 'origprice') {
  165. return Currency::create()->convert($this->getVirtualColumn('origprice'), $cId);
  166. }
  167. return Currency::create()->convert($this->$get(), $cId);
  168. } else {
  169. if ($attributeName == 'origprice') {
  170. return Currency::create()->convertForTemplate($this->getVirtualColumn('origprice'), $cId);
  171. }
  172. return Currency::create()->convertForTemplate($this->$get(), $cId);
  173. }
  174. }
  175. /**
  176. * Simple getter.
  177. *
  178. * @param $name
  179. * @return
  180. */
  181. public function __get($name) {
  182. if (isset($this->$name)) {
  183. return $this->$name;
  184. }
  185. $call = 'get' . $name;
  186. if (method_exists($this, $call)) {
  187. return $this->$call();
  188. }
  189. }
  190. /**
  191. * @return CacheProvider
  192. */
  193. public function getCache() {
  194. return DependencyInjectionProvider::getContainer()->get('cache');
  195. }
  196. public function __call($name, $param) {
  197. if (preg_match('/get(\w+)/', $name, $matches)) {
  198. $virtualColumn = $matches[1];
  199. $virtualColumn = strtolower($virtualColumn);
  200. if ($this->hasVirtualColumn($virtualColumn)) {
  201. return $this->getVirtualColumn($virtualColumn);
  202. }
  203. // no lcfirst in php<5.3...
  204. $virtualColumn[0] = strtolower($virtualColumn[0]);
  205. if ($this->hasVirtualColumn($virtualColumn)) {
  206. return $this->getVirtualColumn($virtualColumn);
  207. }
  208. }
  209. }
  210. public function setVirtualColumn($name, $value) {
  211. $name = strtolower($name);
  212. parent::setVirtualColumn($name, $value);
  213. }
  214. public function preSave(ConnectionInterface $con = null) {
  215. return true;
  216. }
  217. public function postSave(ConnectionInterface $con = null) {
  218. return true;
  219. }
  220. public function preInsert(ConnectionInterface $con = null) {
  221. return true;
  222. }
  223. public function postInsert(ConnectionInterface $con = null) {
  224. return true;
  225. }
  226. public function preUpdate(ConnectionInterface $con = null) {
  227. return true;
  228. }
  229. public function postUpdate(ConnectionInterface $con = null) {
  230. return true;
  231. }
  232. public function postDelete(ConnectionInterface $con = null) {
  233. return true;
  234. }
  235. public function preDelete(ConnectionInterface $con = null) {
  236. return true;
  237. }
  238. /**
  239. * String representation of object
  240. * @link http://php.net/manual/en/serializable.serialize.php
  241. * @return string the string representation of the object or null
  242. * @since 5.1.0
  243. */
  244. public function serialize() {
  245. $this->prepareToSleep();
  246. $cls = new \ReflectionClass($this);
  247. $propertyNames = [];
  248. $serializableProperties = array_diff($cls->getProperties(), $cls->getProperties(\ReflectionProperty::IS_STATIC));
  249. foreach ($serializableProperties as $property) {
  250. $propertyName = $property->getName();
  251. $propertyValue = $this->$propertyName;
  252. if ($propertyValue !== null) {
  253. $propertyNames[$propertyName] = $propertyValue;
  254. }
  255. }
  256. return serialize($propertyNames);
  257. }
  258. private function prepareToSleep() {
  259. $this->collSCategoriesRelatedById = null;
  260. $this->collSCategoryI18ns = null;
  261. $this->collSProductss = null;
  262. $this->collShopProductCategoriess = null;
  263. $this->collShopProductPropertiesCategoriess = null;
  264. $this->collProducts = null;
  265. $this->collProperties = null;
  266. $this->aSCategory = null;
  267. }
  268. /**
  269. * Constructs the object
  270. * @link http://php.net/manual/en/serializable.unserialize.php
  271. * @param string $serialized <p>
  272. * The string representation of the object.
  273. * </p>
  274. * @return void
  275. * @since 5.1.0
  276. */
  277. public function unserialize($serialized) {
  278. $serialized = unserialize($serialized);
  279. foreach ($serialized as $key => $item) {
  280. $this->$key = $item;
  281. }
  282. }
  283. }