| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Webling |
| 4 |
Plugin URI: https://www.webling.eu |
| 5 |
Description: Mitgliederdaten aus der Vereinssoftware webling.eu auf deiner Webseite anzeigen. |
| 6 |
Version: 2.0 |
| 7 |
Author: uSystems GmbH |
| 8 |
Author URI: http://www.usystems.ch |
| 9 |
*/ |
| 10 |
|
| 11 |
/* |
| 12 |
Webling (Wordpress Plugin) |
| 13 |
Copyright (C) 2014 uSystems GmbH |
| 14 |
Contact me at http://www.usystems.ch |
| 15 |
*/ |
| 16 |
|
| 17 |
// Make sure we don't expose any info if called directly |
| 18 |
defined('ABSPATH') or die('.'); |
| 19 |
|
| 20 |
DEFINE("WEBLING_OPTIONS", "webling-options"); |
| 21 |
DEFINE("WEBLING_OPTIONS_GROUP", "webling-options-group"); |
| 22 |
DEFINE("WEBLING_MENU_SLUG", "webling-menu"); |
| 23 |
|
| 24 |
include_once('WeblingAPI.php'); |
| 25 |
|
| 26 |
if (is_admin()){ |
| 27 |
require_once dirname(__FILE__) . '/admin.php'; |
| 28 |
} |
| 29 |
|
| 30 |
//tell wordpress to register the memberlist shortcode |
| 31 |
add_shortcode('webling_memberlist', 'webling_memberlist_handler'); |
| 32 |
add_action('wp_head', 'webling_memberlist_css'); |
| 33 |
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'webling_memberlist_add_action_links'); |
| 34 |
|
| 35 |
function webling_memberlist_add_action_links($links) { |
| 36 |
$mylinks = array( |
| 37 |
'<a href="' . admin_url( 'options-general.php?page='. WEBLING_MENU_SLUG ) . '">Einstellungen</a>', |
| 38 |
); |
| 39 |
return array_merge($links, $mylinks); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Shortcode Handler for [webling_memberlist] |
| 44 |
* |
| 45 |
* @param $atts array shortcode attributes |
| 46 |
* @return string HTML code for the memberlist |
| 47 |
*/ |
| 48 |
function webling_memberlist_handler($atts) { |
| 49 |
|
| 50 |
$options = webling_memberlist_get_config(); |
| 51 |
|
| 52 |
// setup webling connection |
| 53 |
$weblingApi = new WeblingAPI($options['host'], $options['apikey']); |
| 54 |
|
| 55 |
if($weblingApi->isConnected()){ |
| 56 |
|
| 57 |
// allow shortcode attribute to filter for groups="12,56,34" |
| 58 |
$webling_memberlist_atts = shortcode_atts( array( |
| 59 |
'groups' => null |
| 60 |
), $atts ); |
| 61 |
|
| 62 |
$output = webling_memberlist_html($weblingApi, $webling_memberlist_atts); |
| 63 |
|
| 64 |
return '<div class="webling_memberlist">'.$output.'<div>'; |
| 65 |
} else { |
| 66 |
return ''; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param $weblingApi WeblingAPI |
| 72 |
* @param $attributes array Shortcode Attributes |
| 73 |
* @return string HTML Markup |
| 74 |
*/ |
| 75 |
function webling_memberlist_html($weblingApi, $attributes) { |
| 76 |
|
| 77 |
$options = webling_memberlist_get_config(); |
| 78 |
|
| 79 |
// load field definitions |
| 80 |
$definitions = $weblingApi->get('definition'); |
| 81 |
$memberfields = $definitions['member']['properties']; |
| 82 |
|
| 83 |
try { |
| 84 |
if(count($options['fieldarray']) == 0){ |
| 85 |
throw new Exception('Keine Felder angegeben'); |
| 86 |
} |
| 87 |
|
| 88 |
$output = '<table class="webling_memberlist-table">'; |
| 89 |
$output .= '<tr>'; |
| 90 |
foreach ($options['fieldarray'] as $field) { |
| 91 |
$output .= '<th>' . $field . '</th>'; |
| 92 |
} |
| 93 |
$output .= '</tr>'; |
| 94 |
|
| 95 |
if($attributes['groups']){ |
| 96 |
// filter groups |
| 97 |
$groupIds = explode(',',$attributes['groups']); |
| 98 |
$memberIds = array(); |
| 99 |
if(is_array($groupIds) && count($groupIds)){ |
| 100 |
foreach ($groupIds as $groupId){ |
| 101 |
if(trim($groupId) != ''){ |
| 102 |
$data = $weblingApi->get("membergroup/" . intval(trim($groupId))); |
| 103 |
if($weblingApi->getLastRequestStatusCode() == 200){ |
| 104 |
if (isset($data["children"]['member']) && is_array($data["children"]['member'])){ |
| 105 |
$memberIds = array_merge($memberIds, $data["children"]['member']); |
| 106 |
} |
| 107 |
} else { |
| 108 |
throw new Exception('Gruppe nicht gefunden: '.$groupId); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
$memberIds = array_unique($memberIds); |
| 113 |
} |
| 114 |
} else { |
| 115 |
$response = $weblingApi->get("member"); |
| 116 |
$memberIds = $response["objects"]; |
| 117 |
if (is_array($memberIds) == false) { |
| 118 |
$memberIds = []; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if(count($memberIds) == 0){ |
| 123 |
$output = '<p class="webling_memberlist-empty">Keine Mitglieder.</p>'; |
| 124 |
} else { |
| 125 |
foreach ($memberIds as $memberId) { |
| 126 |
$member = $weblingApi->get("member/" . $memberId); |
| 127 |
$output .= '<tr>'; |
| 128 |
foreach ($options['fieldarray'] as $field) { |
| 129 |
$output .= "<td>"; |
| 130 |
$type = (isset($memberfields[$field]['type']) ? $memberfields[$field]['type'] : null); |
| 131 |
$output .= webling_field_formatter($member["properties"][$field], $memberfields[$field]['datatype'], $type); |
| 132 |
$output .= "</td>"; |
| 133 |
} |
| 134 |
$output .= '</tr>'; |
| 135 |
} |
| 136 |
$output .= '</table>'; |
| 137 |
} |
| 138 |
|
| 139 |
return $output; |
| 140 |
} catch (Exception $e) { |
| 141 |
return '<p class="webling_memberlist-error">[webling_memberlist] Fehler: '.$e->getMessage().'</p>'; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Formats a value according to it's type |
| 147 |
* |
| 148 |
* @param $value mixed raw value |
| 149 |
* @param $datatype string the datatype of the value |
| 150 |
* @param $type string type of the value |
| 151 |
* @return string formatted string |
| 152 |
*/ |
| 153 |
function webling_field_formatter($value, $datatype, $type){ |
| 154 |
switch ($datatype){ |
| 155 |
case 'numeric': |
| 156 |
return number_format((float)$value, 2); |
| 157 |
break; |
| 158 |
case 'bool': |
| 159 |
return ($value == true ? 'Ja' : 'Nein'); |
| 160 |
break; |
| 161 |
case 'date': |
| 162 |
$time = strtotime($value); |
| 163 |
if($time > 0){ |
| 164 |
return date('d.m.Y', $time); |
| 165 |
} else { |
| 166 |
return ''; |
| 167 |
} |
| 168 |
break; |
| 169 |
case 'multienum': |
| 170 |
if(is_array($value)){ |
| 171 |
return implode(', ', $value); |
| 172 |
} |
| 173 |
return ''; |
| 174 |
break; |
| 175 |
case 'image': |
| 176 |
case 'file': |
| 177 |
// TODO: image/file datafields - implement image cache or proxy |
| 178 |
return ''; |
| 179 |
break; |
| 180 |
case 'text': |
| 181 |
switch ($type){ |
| 182 |
case 'url': |
| 183 |
if($value){ |
| 184 |
$prefixed_url = $value; |
| 185 |
if ($ret = parse_url($prefixed_url)) { |
| 186 |
if (!isset($ret["scheme"])) { |
| 187 |
$prefixed_url = 'http://'.$prefixed_url; |
| 188 |
} |
| 189 |
} |
| 190 |
return '<a href="'.$prefixed_url.'" target="_blank">'.$value.'</a>'; |
| 191 |
} else { |
| 192 |
return $value; |
| 193 |
} |
| 194 |
break; |
| 195 |
default: |
| 196 |
return $value; |
| 197 |
} |
| 198 |
break; |
| 199 |
default: |
| 200 |
if(is_array($value)) { |
| 201 |
return " "; |
| 202 |
} |
| 203 |
return $value; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
/** |
| 209 |
* Get the webling config from WordPress |
| 210 |
* |
| 211 |
* @return array Webling Plugin config |
| 212 |
*/ |
| 213 |
function webling_memberlist_get_config() { |
| 214 |
|
| 215 |
$options = get_option(WEBLING_OPTIONS); |
| 216 |
|
| 217 |
$options['fieldarray'] = array(); |
| 218 |
foreach (explode(',', $options['fields']) as $field) { |
| 219 |
if (strlen(trim($field)) > 0 ) { |
| 220 |
$options['fieldarray'][] = trim($field); |
| 221 |
} |
| 222 |
} |
| 223 |
return $options; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Handler to add custom css to the page head |
| 228 |
* |
| 229 |
* @return string Custom Webling Plugin CSS |
| 230 |
*/ |
| 231 |
function webling_memberlist_css(){ |
| 232 |
$options = webling_memberlist_get_config(); |
| 233 |
|
| 234 |
$css = file_get_contents(dirname( __FILE__ ) . '/css/memberlist.css'); |
| 235 |
echo '<style type="text/css"><!--' . $css; |
| 236 |
if (isset($options['css'])) { |
| 237 |
echo "\n" . $options['css'] . "\n"; |
| 238 |
} |
| 239 |
echo '--></style>'; |
| 240 |
} |
| 241 |
|