PageRenderTime 28ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/library/XenForo/DataWriter/StylePropertyDefinition.php

https://github.com/daohoangson/DTUI_201105
PHP | 391 lines | 261 code | 41 blank | 89 comment | 44 complexity | 2e2686e38f8a29bdc1e15712a50a0d1e MD5 | raw file
  1. <?php
  2. /**
  3. * Data writer for style property definitions.
  4. *
  5. * @package XenForo_StyleProperty
  6. */
  7. class XenForo_DataWriter_StylePropertyDefinition extends XenForo_DataWriter
  8. {
  9. /**
  10. * Controls whether the value of the master phrase should be updated when
  11. * modifying this definition. Defaults to true.
  12. *
  13. * @var string
  14. */
  15. const OPTION_UPDATE_MASTER_PHRASE = 'updateMasterPhrase';
  16. /**
  17. * Controls whether the development files are updated. Defaults to debug mode value.
  18. *
  19. * @var string
  20. */
  21. const OPTION_UPDATE_DEVELOPMENT = 'updateDevelopment';
  22. /**
  23. * If false, duplicate checking is disabled. An error will occur on dupes. Defaults to true.
  24. *
  25. * @var string
  26. */
  27. const OPTION_CHECK_DUPLICATE = 'checkDuplicate';
  28. /**
  29. * Option to control whether the property cache in the style should be
  30. * rebuilt. Defaults to true.
  31. *
  32. * @var unknown_type
  33. */
  34. const OPTION_REBUILD_CACHE = 'rebuildCache';
  35. /**
  36. * These are banned names for style properties, due to conflicts with other syntax schemes
  37. *
  38. * @var array Banned property names
  39. */
  40. public static $reservedNames = array(
  41. // css reserved @ident - see http://www.w3.org/TR/css3-syntax/#lexical
  42. 'import',
  43. 'page',
  44. 'media',
  45. 'font-face',
  46. 'charset',
  47. 'namespace',
  48. // XenForo reserved @property
  49. 'property',
  50. );
  51. /**
  52. * Title of the phrase that will be created when a call to set the
  53. * existing data fails (when the data doesn't exist).
  54. *
  55. * @var string
  56. */
  57. protected $_existingDataErrorPhrase = 'requested_style_property_definition_not_found';
  58. /**
  59. * Gets the fields that are defined for the table. See parent for explanation.
  60. *
  61. * @return array
  62. */
  63. protected function _getFields()
  64. {
  65. return array(
  66. 'xf_style_property_definition' => array(
  67. 'property_definition_id' => array('type' => self::TYPE_UINT,
  68. 'autoIncrement' => true
  69. ),
  70. 'definition_style_id' => array('type' => self::TYPE_INT,
  71. 'required' => true
  72. ),
  73. 'group_name' => array('type' => self::TYPE_STRING,
  74. 'required' => true,
  75. 'maxLength' => 25,
  76. 'requiredError' => 'please_enter_valid_group_name'
  77. ),
  78. 'property_name' => array('type' => self::TYPE_STRING,
  79. 'required' => true,
  80. 'maxLength' => 100,
  81. 'verification' => array('$this', '_verifyPropertyName'),
  82. 'requiredError' => 'please_enter_valid_property_name'
  83. ),
  84. 'title' => array('type' => self::TYPE_STRING,
  85. 'required' => true,
  86. 'maxLength' => 100,
  87. 'requiredError' => 'please_enter_valid_title'
  88. ),
  89. 'description' => array('type' => self::TYPE_STRING,
  90. 'default' => '',
  91. 'maxLength' => 255
  92. ),
  93. 'property_type' => array('type' => self::TYPE_STRING,
  94. 'required' => true,
  95. 'allowedValues' => array('scalar', 'css')
  96. ),
  97. 'css_components' => array('type' => self::TYPE_UNKNOWN,
  98. 'default' => 'a:0:{}',
  99. 'verification' => array('$this', '_verifyCssComponents')
  100. ),
  101. 'scalar_type' => array('type' => self::TYPE_STRING,
  102. 'default' => '',
  103. 'allowedValues' => array('', 'longstring', 'color', 'number', 'boolean', 'template')
  104. ),
  105. 'scalar_parameters' => array('type' => self::TYPE_STRING,
  106. 'default' => '',
  107. 'maxLength' => 250
  108. ),
  109. 'display_order' => array('type' => self::TYPE_UINT_FORCED,
  110. 'default' => 0
  111. ),
  112. 'addon_id' => array('type' => self::TYPE_STRING,
  113. 'maxLength' => 25,
  114. 'default' => ''
  115. ),
  116. 'sub_group' => array('type' => self::TYPE_STRING,
  117. 'maxLength' => 25,
  118. 'default' => ''
  119. ),
  120. )
  121. );
  122. }
  123. /**
  124. * Gets the actual existing data out of data that was passed in. See parent for explanation.
  125. *
  126. * @param mixed
  127. *
  128. * @return array|false
  129. */
  130. protected function _getExistingData($data)
  131. {
  132. if (!$definitionId = $this->_getExistingPrimaryKey($data))
  133. {
  134. return false;
  135. }
  136. return array(
  137. 'xf_style_property_definition' => $this->_getStylePropertyModel()->getStylePropertyDefinitionById($definitionId)
  138. );
  139. }
  140. /**
  141. * Gets SQL condition to update the existing record.
  142. *
  143. * @return string
  144. */
  145. protected function _getUpdateCondition($tableName)
  146. {
  147. return 'property_definition_id = ' . $this->_db->quote($this->getExisting('property_definition_id'));
  148. }
  149. protected function _getDefaultOptions()
  150. {
  151. return array(
  152. self::OPTION_UPDATE_MASTER_PHRASE => true,
  153. self::OPTION_UPDATE_DEVELOPMENT => XenForo_Application::canWriteDevelopmentFiles(),
  154. self::OPTION_CHECK_DUPLICATE => true,
  155. self::OPTION_REBUILD_CACHE => true
  156. );
  157. }
  158. /**
  159. * Verifies that the property name is valid.
  160. *
  161. * @param string $name
  162. *
  163. * @return boolean
  164. */
  165. protected function _verifyPropertyName($name)
  166. {
  167. if (preg_match('/[^a-zA-Z0-9_]/', $name))
  168. {
  169. $this->error(new XenForo_Phrase('please_enter_property_name_using_only_alphanumeric'), 'property_name');
  170. return false;
  171. }
  172. if (in_array(strtolower($name), self::$reservedNames))
  173. {
  174. $this->error(new XenForo_Phrase('property_name_reserved', array('name' => $name)), 'property_name');
  175. return false;
  176. }
  177. return true;
  178. }
  179. /**
  180. * Verifies the list of CSS components.
  181. *
  182. * @param array|string $components
  183. *
  184. * @return boolean
  185. */
  186. protected function _verifyCssComponents(&$components)
  187. {
  188. if (!is_array($components))
  189. {
  190. $components = array();
  191. }
  192. $firstValue = reset($components);
  193. if (!is_bool($firstValue))
  194. {
  195. $newComponents = array();
  196. foreach ($components AS $component)
  197. {
  198. $newComponents[$component] = true;
  199. }
  200. $components = $newComponents;
  201. }
  202. $components = serialize($components);
  203. return true;
  204. }
  205. /**
  206. * Pre-save handling.
  207. */
  208. protected function _preSave()
  209. {
  210. if ($this->isUpdate() && $this->isChanged('definition_style_id'))
  211. {
  212. throw new XenForo_Exception('Cannot update the style of existing style property definitions.');
  213. }
  214. if ($this->get('property_type') == 'css')
  215. {
  216. $components = $this->get('css_components');
  217. if (is_string($components) && substr($components, 0, 2) == 'a:')
  218. {
  219. $components = unserialize($components);
  220. }
  221. if (!$components)
  222. {
  223. $this->error(new XenForo_Phrase('css_style_property_must_have_at_least_one_css_component'), 'css_components');
  224. }
  225. }
  226. if ($this->isChanged('property_name') && $this->getOption(self::OPTION_CHECK_DUPLICATE))
  227. {
  228. $newName = $this->get('property_name');
  229. $definitions = $this->_getStylePropertyModel()->getEffectiveStylePropertiesInStyle($this->get('definition_style_id'));
  230. foreach ($definitions AS $definition)
  231. {
  232. if ($definition['property_name'] == $newName)
  233. {
  234. $this->error(new XenForo_Phrase('style_property_definitions_must_be_unique_per_style'), 'property_name');
  235. break;
  236. }
  237. }
  238. }
  239. }
  240. /**
  241. * Post-save handling.
  242. */
  243. protected function _postSave()
  244. {
  245. $propertyModel = $this->_getStylePropertyModel();
  246. $definition = $this->getMergedData();
  247. $existingDefinition = reset($this->_existingData);
  248. if ($this->isUpdate() && $this->isChanged('property_type'))
  249. {
  250. // these are all going to be invalid if the type is different
  251. $definitionId = $this->get('property_definition_id');
  252. $this->_db->delete('xf_style_property',
  253. 'property_definition_id = ' . $this->_db->quote($definitionId)
  254. );
  255. }
  256. if ($this->getOption(self::OPTION_UPDATE_MASTER_PHRASE))
  257. {
  258. $titlePhraseName = $this->_getStylePropertyModel()->getStylePropertyTitlePhraseName($definition);
  259. $descriptionPhraseName = $this->_getStylePropertyModel()->getStylePropertyDescriptionPhraseName($definition);
  260. if ($this->get('definition_style_id') < 1 && $this->get('addon_id'))
  261. {
  262. $this->_insertOrUpdateMasterPhrase($titlePhraseName, $this->get('title'), $this->get('addon_id'));
  263. $this->_insertOrUpdateMasterPhrase($descriptionPhraseName, $this->get('description'), $this->get('addon_id'));
  264. }
  265. else if ($this->isUpdate() && $this->getExisting('definition_style_id') < 1 && $this->getExisting('addon_id'))
  266. {
  267. $this->_deleteMasterPhrase($titlePhraseName);
  268. $this->_deleteMasterPhrase($descriptionPhraseName);
  269. }
  270. if ($this->isUpdate() && $this->isChanged('property_name')
  271. && $this->getExisting('definition_style_id') < 1 && $this->getExisting('addon_id')
  272. )
  273. {
  274. $oldTitlePhraseName = $this->_getStylePropertyModel()->getStylePropertyTitlePhraseName($existingDefinition);
  275. $this->_deleteMasterPhrase($oldTitlePhraseName);
  276. $oldDescriptionPhraseName = $this->_getStylePropertyModel()->getStylePropertyDescriptionPhraseName($existingDefinition);
  277. $this->_deleteMasterPhrase($oldDescriptionPhraseName);
  278. }
  279. }
  280. if ($this->isUpdate() && $this->isChanged('property_name'))
  281. {
  282. if ($this->_canWriteToDevelopmentFile())
  283. {
  284. $propertyModel->moveStylePropertyDevelopmentFile(
  285. $existingDefinition, $definition
  286. );
  287. }
  288. if ($this->getOption(self::OPTION_REBUILD_CACHE))
  289. {
  290. $propertyModel->rebuildPropertyCacheInStyleAndChildren($this->get('definition_style_id'));
  291. }
  292. }
  293. if ($this->isUpdate() && $this->_canWriteToDevelopmentFile())
  294. {
  295. $propertyModel->updateStylePropertyDevelopmentFile($definition);
  296. }
  297. }
  298. /**
  299. * Post-delete handling.
  300. */
  301. protected function _postDelete()
  302. {
  303. $propertyModel = $this->_getStylePropertyModel();
  304. $definitionId = $this->get('property_definition_id');
  305. $definition = $this->getMergedData();
  306. $this->_db->delete('xf_style_property',
  307. 'property_definition_id = ' . $this->_db->quote($definitionId)
  308. );
  309. if ($this->_canWriteToDevelopmentFile())
  310. {
  311. $propertyModel->deleteStylePropertyDevelopmentFile(
  312. $this->get('property_name'), 0
  313. );
  314. $propertyModel->deleteStylePropertyDevelopmentFile(
  315. $this->get('property_name'), -1
  316. );
  317. }
  318. if ($this->getOption(self::OPTION_UPDATE_MASTER_PHRASE) && $this->get('definition_style_id') < 1 && $this->get('addon_id'))
  319. {
  320. $titlePhraseName = $this->_getStylePropertyModel()->getStylePropertyTitlePhraseName($definition);
  321. $this->_deleteMasterPhrase($titlePhraseName);
  322. $descPhraseName = $this->_getStylePropertyModel()->getStylePropertyDescriptionPhraseName($definition);
  323. $this->_deleteMasterPhrase($descPhraseName);
  324. }
  325. if ($this->getOption(self::OPTION_REBUILD_CACHE))
  326. {
  327. $propertyModel->rebuildPropertyCacheInStyleAndChildren($this->get('definition_style_id'));
  328. }
  329. }
  330. /**
  331. * Returns true if there is a development file that can be manipulated for this property definition.
  332. *
  333. * @return boolean
  334. */
  335. protected function _canWriteToDevelopmentFile()
  336. {
  337. if (!$this->getOption(self::OPTION_UPDATE_DEVELOPMENT))
  338. {
  339. return false;
  340. }
  341. return ($this->get('definition_style_id') < 1 && $this->get('addon_id') == 'XenForo');
  342. }
  343. /**
  344. * @return XenForo_Model_StyleProperty
  345. */
  346. protected function _getStylePropertyModel()
  347. {
  348. return $this->getModelFromCache('XenForo_Model_StyleProperty');
  349. }
  350. }