Advertisement
TangibleDesign

Untitled

Oct 25th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. <?php
  2.  
  3. namespace MyHomeCore\Components\Listing\Search_Forms;
  4.  
  5.  
  6. use MyHomeCore\Attributes\Attribute;
  7. use MyHomeCore\Attributes\Attribute_Factory;
  8.  
  9. /**
  10. * Class Search_Form
  11. * @package MyHomeCore\Components\Listing\Search_Forms
  12. */
  13. class Search_Form {
  14.  
  15. const SEARCH_FORM_KEY = 'myhome_search_form_';
  16. const SEARCH_FORMS_OPTION = 'myhome_search_forms';
  17. const SEARCH_FORMs_COUNTER = 'myhome_search_form_number';
  18.  
  19. /**
  20. * @var string
  21. */
  22. private $key;
  23.  
  24. /**
  25. * @var string
  26. */
  27. private $label;
  28.  
  29. /**
  30. * @var array
  31. */
  32. private $selected_elements = array();
  33.  
  34. /**
  35. * @var array
  36. */
  37. private $options = array();
  38.  
  39. /**
  40. * Search_Form constructor.
  41. *
  42. * @param $search_form_key
  43. */
  44. public function __construct( $search_form_key ) {
  45. $search_form = get_option( $search_form_key );
  46.  
  47. $this->key = $search_form_key;
  48. $this->label = $search_form['label'];
  49. $this->selected_elements = $search_form['selected_elements'];
  50. $this->options = $search_form['options'];
  51. }
  52.  
  53. /**
  54. * @return array
  55. */
  56. public function get_available_elements() {
  57. $available_elements = array();
  58. $selected_elements = array();
  59.  
  60. foreach ( $this->get_selected_elements() as $element ) {
  61. $selected_elements[] = $element['id'];
  62. }
  63.  
  64. foreach ( Attribute_Factory::get_search() as $attribute ) {
  65. if ( ! in_array( $attribute->get_ID(), $selected_elements ) ) {
  66. $available_elements[] = array(
  67. 'id' => $attribute->get_ID(),
  68. 'name' => $attribute->get_name(),
  69. );
  70. }
  71. }
  72.  
  73. return $available_elements;
  74. }
  75.  
  76. /**
  77. * @return string
  78. */
  79. public function get_label() {
  80. return $this->label;
  81. }
  82.  
  83. /**
  84. * @return string
  85. */
  86. public function get_key() {
  87. return $this->key;
  88. }
  89.  
  90. /**
  91. * @return Attribute[]
  92. */
  93. public function get_attributes() {
  94. $attributes = array();
  95.  
  96. foreach ( $this->get_selected_elements() as $attribute_id ) {
  97. $attributes[] = Attribute::get_by_id( $attribute_id );
  98. }
  99.  
  100. return $attributes;
  101. }
  102.  
  103. /**
  104. * @return array
  105. */
  106. public function get_selected_elements() {
  107. $selected_elements = array();
  108.  
  109. return $selected_elements;
  110.  
  111. if ( empty( $this->selected_elements ) ) {
  112. return $selected_elements;
  113. }
  114.  
  115. foreach ( $this->selected_elements as $attribute_id ) {
  116. $attribute = Attribute::get_by_id( $attribute_id );
  117. $selected_elements[] = array(
  118. 'id' => $attribute->get_ID(),
  119. 'name' => $attribute->get_name(),
  120. );
  121. }
  122.  
  123. return $selected_elements;
  124. }
  125.  
  126. /**
  127. * @return array
  128. */
  129. public function get_data() {
  130. return array(
  131. 'key' => $this->key,
  132. 'label' => $this->label,
  133. 'selected_elements' => $this->get_selected_elements(),
  134. 'available_elements' => $this->get_available_elements(),
  135. 'options' => $this->options
  136. );
  137. }
  138.  
  139. /**
  140. * @param string $label
  141. *
  142. * @return Search_Form
  143. */
  144. public static function create( $label ) {
  145. $counter = intval( get_option( self::SEARCH_FORMs_COUNTER, 1 ) );
  146. $counter ++;
  147. update_option( self::SEARCH_FORMs_COUNTER, $counter );
  148. $key = self::SEARCH_FORM_KEY . $counter;
  149. $search_forms = get_option( self::SEARCH_FORMS_OPTION, array() );
  150. $search_forms[] = $key;
  151. update_option( self::SEARCH_FORMS_OPTION, $search_forms );
  152.  
  153. $search_form = array(
  154. 'key' => $key,
  155. 'label' => $label,
  156. 'selected_elements' => array(),
  157. 'available_elements' => array(),
  158. 'options' => array()
  159. );
  160.  
  161. update_option( $key, $search_form );
  162.  
  163. return new Search_Form( $key );
  164. }
  165.  
  166. /**
  167. * @param array $search_form_data
  168. */
  169. public static function update( $search_form_data ) {
  170. $search_form = array(
  171. 'key' => $search_form_data['key'],
  172. 'label' => $search_form_data['label'],
  173. 'options' => array(),
  174. 'selected_elements' => array()
  175. );
  176.  
  177. foreach ( $search_form_data['selected_elements'] as $element ) {
  178. $search_form['selected_elements'][] = $element['id'];
  179. }
  180.  
  181. update_option( $search_form_data['key'], $search_form );
  182. }
  183.  
  184. /**
  185. * @param $elements
  186. */
  187. public function update_elements( $elements ) {
  188. $search_form = array(
  189. 'key' => $this->get_key(),
  190. 'label' => $this->get_label(),
  191. 'options' => array(),
  192. 'selected_elements' => $elements
  193. );
  194.  
  195. update_option( $this->get_key(), $search_form );
  196. }
  197.  
  198. /**
  199. * @param string $search_form_key
  200. */
  201. public static function delete( $search_form_key ) {
  202. $search_forms = get_option( self::SEARCH_FORMS_OPTION, [] );
  203. if ( ( $key = array_search( $search_form_key, $search_forms ) ) !== false ) {
  204. unset( $search_forms[$key] );
  205. }
  206. update_option( self::SEARCH_FORMS_OPTION, $search_forms );
  207. delete_option( $search_form_key );
  208. }
  209.  
  210. /**
  211. * @return array
  212. */
  213. public static function get_all_search_forms_data() {
  214. $search_forms_data = array();
  215.  
  216. foreach ( Search_Form::get_all_search_forms() as $search_form ) {
  217. $search_forms_data[] = $search_form->get_data();
  218. }
  219.  
  220. return $search_forms_data;
  221. }
  222.  
  223. /**
  224. * @return Search_Form[]
  225. */
  226. public static function get_all_search_forms() {
  227. $search_form_keys = get_option( self::SEARCH_FORMS_OPTION, array() );
  228. $search_forms = array();
  229.  
  230. foreach ( $search_form_keys as $search_form_key ) {
  231. $search_forms[] = new Search_Form( $search_form_key );
  232. }
  233.  
  234. return $search_forms;
  235. }
  236.  
  237. /**
  238. * @return array
  239. */
  240. public static function get_vc_search_form_list() {
  241. $search_forms = Search_Form::get_all_search_forms();
  242. $search_forms_list = array( esc_html__( 'Default', 'myhome-core' ) => 'default' );
  243. foreach ( $search_forms as $search_form ) {
  244. $search_forms_list[$search_form->get_label()] = $search_form->get_key();
  245. }
  246.  
  247. return $search_forms_list;
  248. }
  249.  
  250. /**
  251. * @param $search_form_key
  252. *
  253. * @return bool
  254. */
  255. public static function exists( $search_form_key ) {
  256. $search_form = get_option( $search_form_key );
  257.  
  258. return ! empty( $search_form );
  259. }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement