1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
<?php
namespace hji\recentsales\widgets;
use \hji\membership\Membership;
class RecentSales extends \WP_Widget
{
static $supportedFields = array(
'title',
'market',
'address',
'coords',
'radius',
'hide_search_form',
'polygon',
'polynames',
'price',
'beds',
'baths',
'propertytype',
'yearbuilt',
'size',
'lotsize',
'days',
'sortfield',
'sortorder'
);
function __construct()
{
parent::__construct(
'recent-sales',
__('HJI Recent Sales', 'hji_recent-sales'),
array( 'description' => __('Recently sold properties.', 'hji_recent-sales' ))
);
}
public function widget($args, $instance)
{
$title = apply_filters('widget_title', $instance['title']);
echo $args['before_widget'];
if (!empty($title))
{
echo $args['before_title'] . $title . $args['after_title'];
}
if ((!isset($instance['market']) || empty($instance['market']))
|| (!isset ($instance['coords']) && isset($instance['hide_search_form'])))
{
return;
}
$shortcode = "[recent_sales ";
foreach(self::$supportedFields as $field)
{
if (isset($instance[$field]) && !empty($instance[$field]))
{
$shortcode .= " {$field}=\"{$instance[$field]}\"";
}
}
$shortcode .= "]";
echo do_shortcode($shortcode);
}
private function _setDefaultFieldValues($instance)
{
$instance['search_form'] = (!isset($instance['search_form'])) ? 1 : $instance['search_form'];
$instance['radius'] = (!isset($instance['radius'])) ? 3 : $instance['radius'];
$instance['days'] = (!isset($instance['days'])) ? 60 : $instance['days'];
return $instance;
}
public function form($instance)
{
$instance = $this->_setDefaultFieldValues($instance);
foreach (self::$supportedFields as $name)
{
$this->addField($name, $instance);
}
$markets = Membership::getInstance()->getMarkets();
$markets = (!$markets || empty($markets)) ? array() : $markets;
require_once(Membership::$dir . '/common/utils/View.php');
$view = new \hji\common\utils\View();
echo $view->render(
\hji\recentsales\RecentSales::$dir . '/classes/views/admin-recent-sales-widget.phtml',
array('fields' => $this->getFields(), 'instance' => $instance, 'widget' => $this, 'markets' => $markets));
}
function addField($fieldName, $instance)
{
$this->fieldsArray[$fieldName]['id'] = $this->get_field_id($fieldName);
$this->fieldsArray[$fieldName]['name'] = $this->get_field_name($fieldName);
$this->fieldsArray[$fieldName]['value'] = (isset($instance[$fieldName])) ? $instance[$fieldName] : false;
}
function getFields()
{
return (!isset($this->fieldsArray)) ? false : $this->fieldsArray;
}
public function update($new_instance, $old_instance)
{
$instance = $new_instance;
return $instance;
}
}