--- /dev/null
+<?php
+
+/**
+ * GLM Member-DB WordPress Plugin
+ * Categories data class
+ *
+ * PHP version 5.3
+ *
+ * @category Data
+ * @package GLM Member-DB
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @license http://www.gaslightmedia.com Gaslightmedia
+ * @release SVN: $Id: dataMemberType.php,v 1.0 2011/01/25 19:31:47 cscott Exp $
+ */
+
+/**
+ * EventManagementDataCategories class
+ *
+ * PHP version 5
+ *
+ * @category Data
+ * @package EventManagement
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @license http://www.gaslightmedia.com Gaslightmedia
+ * @release SVN: $Id: dataMembers.php,v 1.0 2011/01/25 19:31:47 cscott
+ * Exp $
+ */
+class GlmDataAccommodations extends GlmDataAbstract
+{
+
+ /**
+ * WordPress Database Object
+ *
+ * @var $wpdb
+ * @access public
+ */
+ public $wpdb;
+ /**
+ * Plugin Configuration Data
+ *
+ * @var $config
+ * @access public
+ */
+ public $config;
+ /**
+ * Field definitions
+ *
+ * @var $ini
+ * @access public
+ */
+ public $table;
+
+ /**
+ * Field definitions
+ *
+ * 'type' is type of field as defined by the application
+ * text Regular text field
+ * pointer Pointer to an entry in another table
+ * 'filters' is the filter name for a particular filter ID in PHP filter
+ * functions
+ * See PHP filter_id()
+ *
+ * 'use' is when to use the field
+ * l = List
+ * g = Get
+ * n = New
+ * i = Insert
+ * e = Edit
+ * u = Update
+ * d = Delete
+ * a = All
+ *
+ * @var $ini
+ * @access public
+ */
+ public $fields = false;
+
+ /**
+ * Constructor
+ *
+ * @param object $d
+ * database connection
+ *
+ * @return void
+ * @access public
+ */
+ function __construct ($wpdb, $config)
+ {
+
+ // If this class is not being extended along with existing $wpdb and $config
+ if (!$this->wpdb) {
+
+ // Save WordPress Database object
+ $this->wpdb = $wpdb;
+
+ // Save plugin configuration object
+ $this->config = $config;
+
+ }
+
+ /*
+ * Table Name
+ */
+ $this->table = GLM_MEMBERS_PLUGIN_DB_PREFIX . 'accommodations';
+
+ /*
+ * Table Data Fields
+ */
+ $this->fields = array(
+
+ 'id' => array(
+ 'field' => 'id',
+ 'type' => 'integer',
+ 'view_only' => true,
+ 'use' => 'a'
+ ),
+
+ // Rooms/Cabins
+ 'num_rooms' => array(
+ 'field' => 'num_rooms',
+ 'type' => 'text',
+ 'required' => true,
+ 'unique' => true,
+ 'use' => 'a'
+ ),
+
+ // Year Round
+ 'year_round' => array(
+ 'field' => 'year_round',
+ 'type' => 'checkbox',
+ 'use' => 'a'
+ ),
+
+ // Short Description
+ 'ref_dest' => array(
+ 'field' => 'ref_dest',
+ 'type' => 'pointer',
+ 'p_table' => GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
+ 'p_field' => 'member_name',
+ 'use' => 'a'
+ )
+
+
+ );
+
+ }
+
+ /**
+ * Add a category using supplied parameters
+ *
+ * @param string $name Category Name
+ * @param mixed $parent Existing Parent ID or name of parent to add
+ *
+ * @return integer ID of new category or false if failed
+ *
+ * @access public
+ */
+
+ public function addCategory($name, $parent) {
+
+ $categoryID = false;
+ $parentID = false;
+
+ // Filter new category name
+ $name = filter_var($name);
+ if ($name == false || trim($name) == '') {
+ return false;
+ }
+
+ // Determine parent supplied as an ID
+
+ $parentInt = $parent - 0;
+ if (is_int($parent) && $parentInt > 0) {
+
+ // If it's not positive, then fail
+ if ($parent <= 0) {
+ return false;
+ }
+
+ // Check that parent exists
+ $sql = "
+ SELECT COUNT(id) AS count
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+ WHERE id = $parent
+ ;";
+ $test = $this->wpdb->get_row($sql, ARRAY_A);
+
+ // Should be only one entry if the parent category exists
+ if ($test['count'] != 1) {
+ return false;
+ }
+
+ $parentID = $parent;
+
+ // Else check to see if parent was supplied as a name
+ } elseif ($parent != '') {
+
+ $parent = filter_var($parent);
+
+ // If the name didn't pass the filer then fail
+ if ($parent == false) {
+ return false;
+ }
+
+ // Check if the name already exists
+ $sql = "
+ SELECT id
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+ WHERE name = '$parent'
+ ;";
+ $parentCategory = $this->wpdb->get_row($sql, ARRAY_A);
+
+ // If we found the parent ID
+ if ($parentCategory['id']) {
+
+ // Set that as the parent ID for the new category
+ $parentID = $parentCategory['id'];
+
+ // Otherwise, try to add the parent category
+ } else {
+
+ // Add the parent as a new category
+ $sql = "
+ INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+ ( name, parent )
+ VALUES
+ ( '".addslashes($parent)."', 0 )
+ ;";
+ $this->wpdb->query($sql);
+ $parentID = $this->wpdb->insert_id;
+
+ // Check if it didn't store properly
+ if (!$parentID) {
+ return false;
+ }
+
+ }
+
+ // Otherwise the categroy being added has no parent
+ } else {
+ $parentID = 0;
+ }
+
+ // Check if the new category name/parent already exists
+ $sql = "
+ SELECT id
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+ WHERE name = '$name'
+ AND parent = $parentID
+ ;";
+ $existing = $this->wpdb->get_row($sql, ARRAY_A);
+
+ if ($existing['id']) {
+
+ $categoryID = $existing['id'];
+
+ } else {
+
+ // Try to add the new category
+ $sql = "
+ INSERT INTO ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+ ( name, parent )
+ VALUES
+ ( '".addslashes($name)."', $parentID )
+ ;";
+ $this->wpdb->query($sql);
+ $categoryID = $this->wpdb->insert_id;
+
+ }
+
+ return $categoryID;
+
+ }
+
+ /**
+ * Get categories list sorted by parent/child then alpha
+ *
+ * @param boolean $forEdit If true include list of options for each parent field
+ * @param boolean $forActiveMembers Return only categories that are referenced in active members
+ *
+ * @return array Array of categories
+ * @access public
+ */
+
+ public function getListSortedParentChild($forEdit = true, $forActiveMembers = false, $regionList = false, $countyList = false) {
+
+ $regionWhere = '';
+ if ($regionList !== false && trim($regionList) != '') {
+ $regionWhere = " AND MI.region in ($regionList)";
+ }
+ $countyWhere = '';
+ if ( $countyList !== false && trim( $countyList ) != '' ) {
+ $countyWhere = " AND MI.county in ($countyList) ";
+ }
+ $where = '';
+
+ // If we only want categories for active and visible members - and we want any of their parents (i know it's messy)
+ if ($forActiveMembers) {
+ $where = "
+ (
+ T.id in (
+ SELECT DISTINCT(C.parent)
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories C
+ WHERE C.id in (
+ SELECT DISTINCT(CMI.category)
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."category_member_info CMI,
+ ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_info MI,
+ ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members M
+ WHERE MI.id = CMI.member_info
+ AND MI.status = 10
+ AND M.id = MI.member
+ $regionWhere
+ $countyWhere
+ AND M.access IN (
+ ".$this->config['access_numb']['NoAccess'].",
+ ".$this->config['access_numb']['Moderated'].",
+ ".$this->config['access_numb']['Full']."
+ )
+ )
+ )
+ OR
+ T.id in (
+ SELECT DISTINCT(CMI.category)
+ FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."category_member_info CMI,
+ ".GLM_MEMBERS_PLUGIN_DB_PREFIX."member_info MI,
+ ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members M
+ WHERE MI.id = CMI.member_info
+ AND MI.status = 10
+ AND M.id = MI.member
+ $regionWhere
+ $countyWhere
+ AND M.access IN (
+ ".$this->config['access_numb']['NoAccess'].",
+ ".$this->config['access_numb']['Moderated'].",
+ ".$this->config['access_numb']['Full']."
+ )
+ )
+
+ )
+ ";
+ }
+
+ // Get a list of all categories (optionally for active members only)
+ $categories = $this->getList($where);
+ $categoriesSorted = $this->sortParentChild($categories);
+
+ if (!$forEdit && is_array($categoriesSorted)) {
+ foreach ( $categoriesSorted as $k => $v ) {
+ $categoriesSorted[$k]['parent_id'] = $v['parent']['value'];
+ $categoriesSorted[$k]['parent'] = $v['parent']['name'];
+ }
+ }
+
+ return $categoriesSorted;
+
+ }
+
+ /*
+ * Check other requirements
+ *
+ * When deleteing categories, check for references in sub-categories and set them to no parent.
+ *
+ */
+ function checkOther($r, $a)
+ {
+ // Required
+ parent::checkOther($r, $a);
+
+ // If there's a valid category ID
+ if (isset($r['id']) && $r['id'] > 0) {
+
+ // Set any parent references with this ID to 0
+ $sql = "
+ UPDATE ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories
+ SET parent = 0
+ WHERE parent = ".$r['id']."
+ ;";
+ $this->wpdb->query($sql);
+
+ }
+
+ return $r;
+ }
+
+ /*
+ * Sort a database result array (from DataAbstract.php) by parent, child relationships
+ *
+ * @param array $array Array to be sorted
+ * @param string $name Name of array element that represents the text to sort on
+ * @param string $parent Name of the array element that represents the parent ID to sort on
+ *
+ * @return array Sorted array
+ */
+ public static function sortParentChild($array, $name = 'name', $parent = 'parent')
+ {
+
+ if (!is_array($array) || count($array) == 0) {
+ return false;
+ }
+
+ // Do a sort by custom function with it included in the call
+ // This lets me directly use $name and $parent in the sort function
+ uasort($array, function ($a, $b) use ($name, $parent) {
+
+ // If there's a parent, append the name of this entry to the parent
+ // The '~~~' simply keeps appended strings from any chance of a match with something else
+ if ($a[$parent]['value']) {
+ $aVal = $a[$parent]['name'].'~~~'.$a['name'];
+ } else {
+ // Otheriwse just use the name.
+ $aVal = $a['name'].'~~~';
+ }
+
+ // Same for b value
+ if ($b[$parent]['value']) {
+ $bVal = $b[$parent]['name'].'~~~'.$b['name'];
+ } else {
+ $bVal = $b['name'].'~~~';
+ }
+
+ if ($aVal > $bVal) {
+ return 1;
+ }
+
+ if ($aVal < $bVal) {
+ return -1;
+ }
+
+ return 0;
+
+ });
+
+ return $array;
+ }
+
+}
+
+?>
// Update submitted categories
$this->updateCategories();
+ // Update submitted accommodations
+ $this->updateAccommodations();
+
// Update submitted amenities
$this->updateAmenities();
}
+ public function updateAccommodations()
+ {
+ if ( !$this->config['settings']['profile_accommodation'] ) {
+ return false;
+ }
+ if ( isset( $_REQUEST['accommodations'] ) && $hasAccommodations = filter_var( $_REQUEST['accommodations'], FILTER_VALIDATE_BOOLEAN ) ) {
+ $accommodationId = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "accommodations
+ WHERE ref_dest = %d",
+ $this->memberInfoID
+ )
+ );
+ $yearRound = isset( $_REQUEST['year_round'] ) ? filter_var( $_REQUEST['year_round'], FILTER_VALIDATE_BOOLEAN ) : false;
+ $numRooms = isset( $_REQUEST['num_rooms'] ) ? filter_var( $_REQUEST['num_rooms'], FILTER_VALIDATE_INT ) : false;
+ if ( $accommodationId ) {
+ $this->wpdb->update(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX . 'accommodations',
+ array(
+ 'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+ 'ref_dest' => $this->memberInfoID,
+ 'num_rooms' => $numRooms,
+ 'year_round' => $yearRound
+ ),
+ array( 'id' => $accommodationId ),
+ array(
+ '%d',
+ '%d',
+ '%d',
+ '%s'
+ ),
+ array( '%d' )
+ );
+ } else {
+ $this->wpdb->insert(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX . 'accommodations',
+ array(
+ 'ref_type' => $this->config['ref_type_numb']['MemberInfo'],
+ 'ref_dest' => $this->memberInfoID,
+ 'num_rooms' => $numRooms,
+ 'year_round' => $yearRound
+ ),
+ array(
+ '%d',
+ '%d',
+ '%d',
+ '%s'
+ )
+ );
+ }
+ } else {
+ // Delete Accommodation record
+ $this->wpdb->query(
+ "DELETE FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "accommodations
+ WHERE ref_dest = " . $this->memberInfoID
+ );
+ }
+
+ // echo '<pre>' . print_r( $accommodation, true ) . '</pre>';
+ // exit;
+ }
/**
* Check for new Cities being submitted