/**
  * List of service profiles
  * 
  * Parameters:
  * 
  * - **category:** (optional) "social", "donation" or "all". Default: "all"
  * - **type:**     (optional) Filter services by type. List of all service types: 500px, amazon wishlist, app.net, bandcamp, bitbucket, bitcoin, deviantart, diaspora, dogecoin, dribbble, facebook, flattr, flickr, generic wishlist, github, google+, instagram, jabber, last.fm, linkedin, litecoin, openstreetmap, paypal, miiverse, pinboard, pinterest, playstation network, skype, soundcloud, soup, steam, steam wishlist, thomann wishlist, twitch, tumblr, twitter, website, xbox live, xing, youtube
  *
  * Example:
  *
  * ```html
  * {% for service in podcast.services({category: "social"}) %}
  *   <a target="_blank" title="{{ service.title }}" href="{{ service.profileUrl }}">
  *		{{ service.image.html({width: 20}) }}
  *   </a>
  * {% endfor %}
  * ```
  * 
  * @accessor
  * @dynamicAccessor podcast.services
  */
 public static function accessorPodcastServices($return, $method_name, $podcast, $args = array())
 {
     return $podcast->with_blog_scope(function () use($args) {
         $category = isset($args['category']) && in_array($args['category'], array("social", "donation", "all")) ? $args['category'] : "all";
         if ($category == "all") {
             $services = ShowService::all("ORDER BY position ASC");
         } else {
             $services = ShowService::find_by_category($category);
         }
         if (isset($args["type"]) && $args["type"]) {
             $services = array_filter($services, function ($s) use($args) {
                 return $s->get_service()->type == $args["type"];
             });
         }
         return array_map(function ($service) {
             return new Template\Service($service, $service->get_service());
         }, $services);
     });
 }
 public static function fix_duplicate_services()
 {
     global $wpdb;
     $services = self::find_duplicate_services();
     if (!is_array($services) || empty($services)) {
         Repair::add_to_repair_log(__('Services did not need repair', 'podlove'));
         return;
     }
     foreach ($services as $service) {
         # update contributor services
         $sql = "UPDATE " . ContributorService::table_name() . " SET service_id = " . $service['id'] . " WHERE service_id IN (\n\t\t\t\tSELECT id FROM " . Service::table_name() . " WHERE `type` = \"" . $service['type'] . "\"\n\t\t\t)";
         $wpdb->query($sql);
         # update show services
         $sql = "UPDATE " . Model\ShowService::table_name() . " SET service_id = " . $service['id'] . " WHERE service_id IN (\n\t\t\t\tSELECT id FROM " . Service::table_name() . " WHERE `type` = \"" . $service['type'] . "\"\n\t\t\t)";
         $wpdb->query($sql);
         # delete obsolete services
         $sql = "DELETE FROM " . Service::table_name() . " WHERE id != " . $service['id'] . " AND `type` = \"" . $service['type'] . "\"";
         $wpdb->query($sql);
     }
     Repair::add_to_repair_log(sprintf(__('Consolidated duplicate services (%s)', 'podlove'), implode(', ', array_map(function ($s) {
         return $s['type'];
     }, $services))));
 }
 public function delete_podcast_services()
 {
     $object_id = (int) $_REQUEST['object_id'];
     if (!$object_id) {
         return;
     }
     if ($service = ShowService::find_by_id($object_id)) {
         $service->delete();
     }
 }
 public function save_service_setting($old, $new, $form_key = 'services', $type = 'social')
 {
     foreach (\Podlove\Modules\Social\Model\ShowService::find_by_category($type) as $service) {
         $service->delete();
     }
     if (!isset($new[$form_key])) {
         return;
     }
     $services_appearances = $new[$form_key];
     $position = 0;
     foreach ($services_appearances as $service_appearance) {
         foreach ($service_appearance as $service_id => $service) {
             $c = new \Podlove\Modules\Social\Model\ShowService();
             $c->position = $position;
             $c->service_id = $service_id;
             $c->value = $service['value'];
             $c->title = $service['title'];
             $c->save();
         }
         $position++;
     }
 }
 public static function podcast_form_extension_form()
 {
     $services = \Podlove\Modules\Social\Model\ShowService::find_by_category();
     \Podlove\Modules\Social\Social::services_form_table($services, 'podlove_podcast[services]');
 }
 * A contributor contributes to a podcast/show.
 */
class ShowService extends Base
{
    use \Podlove\Model\KeepsBlogReferenceTrait;
    public function __construct()
    {
        $this->set_blog_id();
    }
    public function get_service()
    {
        return $this->with_blog_scope(function () {
            return Service::find_one_by_id($this->service_id);
        });
    }
    public function get_service_url()
    {
        $service = $this->get_service();
        return str_replace('%account-placeholder%', $this->value, $service->url_scheme);
    }
    public static function find_by_category($category = 'social')
    {
        return self::all("WHERE service_id IN (SELECT id FROM " . Service::table_name() . " WHERE `category` = '" . $category . "' ) ORDER BY position ASC");
    }
}
ShowService::property('id', 'INT NOT NULL AUTO_INCREMENT PRIMARY KEY');
ShowService::property('service_id', 'INT');
ShowService::property('value', 'TEXT');
ShowService::property('title', 'TEXT');
ShowService::property('position', 'FLOAT');