<?php
#-------------------------------------------------------------------------
# LISE - List It Special Edition
# Version 1.2
# A fork of ListI2
# maintained by Fernando Morgado AKA Jo Morg
# since 2015
#-------------------------------------------------------------------------
#
# Original Author: Ben Malen, <ben@conceptfactory.com.au>
# Co-Maintainer: Simon Radford, <simon@conceptfactory.com.au>
# Web: www.conceptfactory.com.au
#
#-------------------------------------------------------------------------
#
# Maintainer since 2011 up to 2014: Jonathan Schmid, <hi@jonathanschmid.de>
# Web: www.jonathanschmid.de
#
#-------------------------------------------------------------------------
#
# Some wackos started destroying stuff since 2012 and stopped at 2014:
#
# Tapio Löytty, <tapsa@orange-media.fi>
# Web: www.orange-media.fi
#
# Goran Ilic, <uniqu3e@gmail.com>
# Web: www.ich-mach-das.at
#
#-------------------------------------------------------------------------
#
# LISE is a CMS Made Simple module that enables the web developer to create
# multiple lists throughout a site. It can be duplicated and given friendly
# names for easier client maintenance.
#
#-------------------------------------------------------------------------
# BEGIN_LICENSE
#-------------------------------------------------------------------------
# This file is part of LISE
# LISE program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LISE program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
# END_LICENSE
#-------------------------------------------------------------------------
class lisefd_MAMSGroupsSingleSelect extends LISEFielddefBase
{
  private $_opts = [];
  
  public function __construct(&$db_info, $caller_obj = NULL)
  {
    parent::__construct($db_info, $caller_obj);
    $this->SetFriendlyType( $this->ModLang( 'fielddef_' . $this->GetType() ) );
    \cms_utils::get_smarty()->assign('lise', \cms_utils::get_module('LISE'));
  }
  
  static public function GetModuleDeps() 
  { 
    return [
                  'MAMS' => '1.0.b'
    ];
  }
  
  public function GetHeaderHTML()
  {
    $tmpl = '';
    if( $this->GetOptionValue('type', 'Dropdown') == 'MultiSelect' )
    {
        $tmpl = <<<EOT
<link type="text/css" rel="stylesheet" href="{$this->GetURLPath()}/jquery.multi-select.css" />
<script type="text/javascript" src="{$this->GetURLPath()}/jquery.multi-select.js"></script>
EOT;
    }
    return $tmpl;
  }
  
  // get available groups from MAMS module
  public function GetMAMSGroups()
  {
    $mams = \cms_utils::get_module('MAMS');
    
    if(is_object($mams))
    {
      $groups = $mams->GetGroupListFull();
      
      if($groups) 
      {
        $options = array();
        
        foreach($groups as $option) 
        {
          $options[$option['id']] = $option['groupname'];
        }
      }
      
      return $options;
    }
  }
  
  public function GetMAMSProperties()
  {
    $mams = \cms_utils::get_module('MAMS');
    $mams_props = $mams->GetPropertyDefns();
    
    // remove selfreg prop
    if( isset($mams_props['__MAMSREG']) ) unset($mams_props['__MAMSREG']);
    
    $mams_options = array();
    $mams_optionsb = array();
    foreach($mams_props as $one)$mams_options[ $one['name'] ] = $one['prompt'];
    asort($mams_options);
    $mams_optionsb[-1] = lang('none');
    $mams_optionsb[0] = $mams->Lang('prompt_username');
    $mams_options = array_merge($mams_optionsb, $mams_options);
    
    return $mams_options;
  } 
     
  public function GetOptions() 
  {
    $lise = \cms_utils::get_module('LISE');
  
    $this->_opts['type'] = $this->GetOptionValue('type', 'MultiSelect');
    $this->_opts['types'] = array(
                                    'Dropdown'          => $lise->ModLang('fielddef_Dropdown'),
                                    'RadioGroup'        => $lise->ModLang('fielddef_RadioGroup'),
                                    'JQueryMultiSelect' => $lise->ModLang('fielddef_JQueryMultiSelect')
                                  );
    
    return $this->_opts;
  }
  
  // get list of users from MAMS module based on selected group
  public function GetMAMSUserList()
  {    
    $mams = \cms_utils::get_module('MAMS');
    
    if(is_object($mams))
    {
      // GetUsersInGroup() is deprecated but GetFullUsersInGroup() seems to have a bug, 
      // change this after MAMS BR #8753 http://dev.cmsmadesimple.org/bug/view/8753 is confirmed and fixed
      $groups       = explode(',', $this->GetOptionValue('mams_options'));
      $mams_prop    = $this->GetOptionValue('mams_field');
      $mams_prop2   = $this->GetOptionValue('mams_field2');
      $use_username = $this->GetOptionValue('use_username');
      $use_uid      = $this->GetOptionValue('use_uid');
      $separator    = $this->GetOptionValue('separator');
      $users        = array();
      
      foreach($groups as $group)
      {
        $tmpusers = $mams->GetFullUsersInGroup($group);
        
        foreach($tmpusers as $one)
        {
          if( array_key_exists($one['id'], $users) ) continue;
          
          $uid = (bool)$use_uid ? '[' . $one['id'] . '] ' : ''; 
          
          #now lets build the select          
          if($use_username)
          {
            $users[$one['id']] = $uid . $one['username'];
          }
          else
          {
            if( $mams_prop == NULL || (is_numeric($mams_prop) && $mams_prop == -1) )
            {
              $users[$one['id']] = $uid . $one['username'];
            }
            else
            {
              $users[$one['id']] = $uid;
              
              if(is_numeric($mams_prop) && $mams_prop == 0)
              {
                $users[$one['id']] .= $one['username'];
              }
              else
              {
                $users[$one['id']] .= $mams->GetUserPropertyFull($mams_prop, $one['id']);
              }
     
              if( is_numeric($mams_prop2) && $mams_prop2 == 0 && !(is_int($mams_prop) && $mams_prop < 1) )
              {
                $users[$one['id']] .= $separator . $one['username'];
              }
              elseif( !(is_int($mams_prop2) && $mams_prop2 == -1) )
              {
                $users[$one['id']] .= $separator . $mams->GetUserPropertyFull($mams_prop2, $one['id']);
              } 
            }
          }
        }
        
      }
      
      asort($users);
      
      return $users;
    }
  }
  
  public function RenderForAdminListing($id, $returnid)
  {
    $user = '';
    $mams = \cms_utils::get_module('MAMS');
    
    if(is_object($mams))
    {
      $value = $this->GetValue();
      $user = $mams->GetUserName($value[0]);
    }
    
    $smarty = cmsms()->GetSmarty();
    $smarty->assign('user', $user);
    return parent::RenderForAdminListing($id, $returnid);
  }
  
}// end of class
?>