<commit_before><?php 
namespace SabreShare\CalDAV\Backend;
use Sabre\CalDAV\Backend as SabreBackend;

class SabreSharePDO extends SabreBackend\PDO implements SabreBackend\SharingSupport
{
	/**
	 * The table name that will be used for calendar shares
	 *
	 * @var string
	 */
	protected $calendarSharesTableName;
        
        /**
	 * The table name that will be used for principals
	 *
	 * @var string
	 */
	protected $principalsTableName;
	
	/**
	 * List of properties for the calendar shares table
	 * This list maps exactly to the field names in the db table
	 */
	public $sharesProperties = array(
			'calendarId',
			'member',
			'status',
			'readOnly',
			'summary',
// 			'displayName',
// 			'colour'
		);
	
	/**
	 * Creates the backend
	 *
	 * @param \PDO $pdo
	 * @param string $calendarTableName
	 * @param string $calendarObjectTableName
	 */
	public function __construct(\PDO $pdo, $calendarTableName = 'calendars', $principalsTableName = 'principals', $calendarObjectTableName = 'calendarobjects', $calendarSharesTableName = 'calendarShares') {
	
		parent::__construct($pdo, $calendarTableName, $calendarObjectTableName);
		$this->calendarSharesTableName = $calendarSharesTableName;
                $this->principalsTableName = $principalsTableName;
	
	}
        
        /**
         * Setter method for the calendarShares table name
         */
        public function setCalendarSharesTableName($name)
        {
            $this->calendarSharesTableName = $name;
        }
	
	/**
	 * Updates the list of shares.
	 *
	 * The first array is a list of people that are to be added to the
	 * calendar.
	 *
	 * Every element in the add array has the following properties:
	 *   * href - A url. Usually a mailto: address
	 *   * commonName - Usually a first and last name, or false
	 *   * summary - A description of the share, can also be false
	 *   * readOnly - A boolean value
	 *
	 * Every element in the remove array is just the address string.
	 *
	 * Note that if the calendar is currently marked as 'not shared' by and
	 * this method is called, the calendar should be 'upgraded' to a shared
	 * calendar.
	 *
	 * @param mixed $calendarId
	 * @param array $add
	 * @param array $remove
	 * @return void
	 */
	function updateShares($calendarId, array $add, array $remove)
	{ 
		$fields = array();
		$fields[':calendarId'] = $calendarId; 
                
                // get the principal based on the supplied email address
                
                $principal = $this->getPrincipalByEmail($add[0]['href']);
		
		$fields[':member'] = $principal['id'];
		
		$fields[':status'] = \Sabre\CalDAV\SharingPlugin::STATUS_NORESPONSE;
				
		// check we have all the required fields
		foreach($this->sharesProperties as $field) {
			if(isset($add[0][$field])) {
				$fields[':'.$field] = $add[0][$field];
			}
		} 
						
		$stmt = $this->pdo->prepare("INSERT INTO ".$this->calendarSharesTableName." (".implode(', ', $this->sharesProperties).") VALUES (".implode(', ',array_keys($fields)).")");
		
		$stmt->execute($fields);
		
	
// 		are we removing any shares?
		if(count($remove)>0) {
			$r_ids = array();
			foreach($remove as $r_mailto) {
				// get the principalid
                                $r_principal = $this->getPrincipalByEmail($r_mailto);
                                $r_ids[] = $r_principal['id'];
			}	
			$stmt = $this->pdo->prepare("DELETE FROM ".$this->calendarSharesTableName." WHERE MEMBER = ?");
			$stmt->execute($r_ids);
		}
	}
	
	/**
	 * Returns the list of people whom this calendar is shared with.
	 *
	 * Every element in this array should have the following properties:
	 *   * href - Often a mailto: address
	 *   * commonName - Optional, for example a first + last name
	 *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
	 *   * readOnly - boolean
	 *   * summary - Optional, a description for the share
	 *
	 * @return array
	 */
	public function getShares($calendarId) {
		
//		$fields = implode(', ', $this->sharesProperties);
		$stmt = $this->pdo->prepare("SELECT * FROM ".$this->calendarSharesTableName." AS calendarShares LEFT JOIN ".$this->principalsTableName."  AS principals ON calendarShares.member = principals.id WHERE calendarShares.calendarId = ? ORDER BY calendarShares.calendarId ASC");
		$stmt->execute(array($calendarId));

		$shares = array();
		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 
			$share = array(	'calendarId'=>$row['calendarId'],
							'readonly'=>$row['readonly'],
							'summary'=>$row['summary'],
// 							'displayName'=>$row['displayName'],
// 							'colour'=>$row['colour']
                                        'href'=>$row['email'],
                                        'commonName' => $row['displayname'],
                                        'principalPath' => $row['uri']
					);
			
			// map the status integer to a predefined constant
			switch($row['status']) {
				case 1: 
					$share['status'] = 'STATUS_ACCEPTED'; 
					break;
				case 2: 
					$share['status'] = 'STATUS_DECLINED'; 
					break;
				case 3: 
					$share['status'] = 'STATUS_DELETED'; 
					break;
				case 4: 
					$share['status'] = 'STATUS_NORESPONSE'; 
					break;
				case 5: 
					$share['status'] = 'STATUS_INVALID'; 
					break;
			}
			
			// get the member principal
//			$memberPrincipal = $this->getPrincipalBackend()->getPrincipalById($row['member']);
//			$share['href'] = $memberPrincipal['{http://sabredav.org/ns}email-address'];
//			$share['commonName'] = $memberPrincipal['{DAV:}displayname'];
			
			// add it to main array
			$shares[] = $share;
		}
	
		return $shares;
	}
	
	/**
	 * Returns a list of calendars for a principal.
	 *
	 * Every project is an array with the following keys:
	 *  * id, a unique id that will be used by other functions to modify the
	 *    calendar. This can be the same as the uri or a database key.
	 *  * uri, which the basename of the uri with which the calendar is<commit_msg>Some tidying up. Removed switch for setting the status property.<commit_after><?php 
namespace SabreShare\CalDAV\Backend;
use Sabre\CalDAV\Backend as SabreBackend;

class SabreSharePDO extends SabreBackend\PDO implements SabreBackend\SharingSupport
{
	/**
	 * The table name that will be used for calendar shares
	 *
	 * @var string
	 */
	protected $calendarSharesTableName;
        
        /**
	 * The table name that will be used for principals
	 *
	 * @var string
	 */
	protected $principalsTableName;
	
	/**
	 * List of properties for the calendar shares table
	 * This list maps exactly to the field names in the db table
	 */
	public $sharesProperties = array(
			'calendarId',
			'member',
			'status',
			'readOnly',
			'summary',
// 			'displayName',
// 			'colour'
		);
	
	/**
	 * Creates the backend
	 *
	 * @param \PDO $pdo
	 * @param string $calendarTableName
	 * @param string $calendarObjectTableName
	 */
	public function __construct(\PDO $pdo, $calendarTableName = 'calendars', $principalsTableName = 'principals', $calendarObjectTableName = 'calendarobjects', $calendarSharesTableName = 'calendarShares') {
	
		parent::__construct($pdo, $calendarTableName, $calendarObjectTableName);
		$this->calendarSharesTableName = $calendarSharesTableName;
                $this->principalsTableName = $principalsTableName;
	
	}
        
        /**
         * Setter method for the calendarShares table name
         */
        public function setCalendarSharesTableName($name)
        {
            $this->calendarSharesTableName = $name;
        }
	
	/**
	 * Updates the list of shares.
	 *
	 * The first array is a list of people that are to be added to the
	 * calendar.
	 *
	 * Every element in the add array has the following properties:
	 *   * href - A url. Usually a mailto: address
	 *   * commonName - Usually a first and last name, or false
	 *   * summary - A description of the share, can also be false
	 *   * readOnly - A boolean value
	 *
	 * Every element in the remove array is just the address string.
	 *
	 * Note that if the calendar is currently marked as 'not shared' by and
	 * this method is called, the calendar should be 'upgraded' to a shared
	 * calendar.
	 *
	 * @param mixed $calendarId
	 * @param array $add
	 * @param array $remove
	 * @return void
	 */
	function updateShares($calendarId, array $add, array $remove)
	{ 
		$fields = array();
		$fields[':calendarId'] = $calendarId; 
                
                // get the principal based on the supplied email address
                
                $principal = $this->getPrincipalByEmail($add[0]['href']);
		
		$fields[':member'] = $principal['id'];
		
		$fields[':status'] = \Sabre\CalDAV\SharingPlugin::STATUS_NORESPONSE;
				
		// check we have all the required fields
		foreach($this->sharesProperties as $field) {
			if(isset($add[0][$field])) {
				$fields[':'.$field] = $add[0][$field];
			}
		} 
						
		$stmt = $this->pdo->prepare("INSERT INTO ".$this->calendarSharesTableName." (".implode(', ', $this->sharesProperties).") VALUES (".implode(', ',array_keys($fields)).")");
		
		$stmt->execute($fields);
		
	
// 		are we removing any shares?
		if(count($remove)>0) {
			$r_ids = array();
			foreach($remove as $r_mailto) {
				// get the principalid
                                $r_principal = $this->getPrincipalByEmail($r_mailto);
                                $r_ids[] = $r_principal['id'];
			}	
			$stmt = $this->pdo->prepare("DELETE FROM ".$this->calendarSharesTableName." WHERE MEMBER = ?");
			$stmt->execute($r_ids);
		}
	}
	
	/**
	 * Returns the list of people whom this calendar is shared with.
	 *
	 * Every element in this array should have the following properties:
	 *   * href - Often a mailto: address
	 *   * commonName - Optional, for example a first + last name
	 *   * status - See the Sabre\CalDAV\SharingPlugin::STATUS_ constants.
	 *   * readOnly - boolean
	 *   * summary - Optional, a description for the share
	 *
	 * @return array
	 */
	public function getShares($calendarId) {
		
//		$fields = implode(', ', $this->sharesProperties);
		$stmt = $this->pdo->prepare("SELECT * FROM ".$this->calendarSharesTableName." AS calendarShares LEFT JOIN ".$this->principalsTableName."  AS principals ON calendarShares.member = principals.id WHERE calendarShares.calendarId = ? ORDER BY calendarShares.calendarId ASC");
		$stmt->execute(array($calendarId));

		$shares = array();
		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { 
			$share = array(	'calendarId'=>$row['calendarId'],
                                        'principalPath' => $row['uri'],
                                        'readOnly'=>$row['readonly'],
                                        'summary'=>$row['summary'],
                                        'href'=>$row['email'],
                                        'commonName' => $row['displayname'],
                                        'displayName'=>$row['displayName'],
                                        'status'=>$row['status']
//                                        'colour'=>$row['colour'],
//                                        'displayName'=>$row['displayName'],
					);
			
//			// map the status integer to a predefined constant
//			switch($row['status']) {
//				case 1: 
//					$share['status'] = 'STATUS_ACCEPTED'; 
//					break;
//				case 2: 
//					$share['status'] = 'STATUS_DECLINED'; 
//					break;
//				case 3: 
//					$share['status'] = 'STATUS_DELETED'; 
//					break;
//				case 4: 
//					$share['status'] = 'STATUS_NORESPONSE'; 
//					break;
//				case 5: 
//					$share['status'] = 'STATUS_INVALID'; 
//					break;
//			}
			
			// add it to main array
			$shares[] = $share;
		}
	
		return $shares;
	}
	
	/**
	 * Returns a list of calendars for a principal.
	 *
	 * Every project is an array with the following keys:
	 *  * id, a unique id that will be used by other functions to modify the
	 *    calendar. This can be the same as the uri or a database key.
	 *  * uri, which the basename of the uri with which the calendar is
{'fraction_non_alphanumeric': 0.1330608158838742, 'fraction_numerical': 0.001501626762325853, 'mean_word_length': 2.526919682259488, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3741050', 'n_tokens_mistral': 3985, 'n_tokens_neox': 3483, 'n_words': 994}