<commit_before><?php
/**
 * ownCloud - Addressbook
 *
 * @author <NAME>
 * @copyright 2011 <NAME> <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// Init owncloud
require_once('../../../lib/base.php');

// Check if we are a user
OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts');
$l=new OC_L10N('contacts');

$id = $_POST['id'];
$vcard = OC_Contacts_App::getContactVCard( $id );

$name = $_POST['name'];
$value = $_POST['value'];
if(!is_array($value)){
	$value = trim($value);
	if(!$value && in_array($name, array('TEL', 'EMAIL'))) {
		OC_JSON::error(array('data' => array('message' => $l->t('Cannot add empty property.'))));
		exit();
	}
}
$parameters = isset($_POST['parameters']) ? $_POST['parameters'] : array();

$property = $vcard->addProperty($name, $value); //, $parameters);

$line = count($vcard->children) - 1;

// Apparently Sabre_VObject_Parameter doesn't do well with multiple values or I don't know how to do it. Tanghus.
foreach ($parameters as $key=>$element) {
	if(is_array($element) && strtoupper($key) == 'TYPE') { 
		// FIXME: Maybe this doesn't only apply for TYPE?
		// And it probably shouldn't be done here anyways :-/
		foreach($element as $e){
			if($e != '' && !is_null($e)){
				$vcard->children[$line]->parameters[] = new Sabre_VObject_Parameter($key,$e);
			}
		}
	} else {
			$vcard->children[$line]->parameters[] = new Sabre_VObject_Parameter($key,$element);
	}<commit_msg>Don't add address if none of the fields are filled out.<commit_after><?php
/**
 * ownCloud - Addressbook
 *
 * @author <NAME>
 * @copyright 2011 <NAME> <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// Init owncloud
require_once('../../../lib/base.php');

// Check if we are a user
OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts');
$l=new OC_L10N('contacts');

$id = $_POST['id'];
$vcard = OC_Contacts_App::getContactVCard( $id );

$name = $_POST['name'];
$value = $_POST['value'];
if(!is_array($value)){
	$value = trim($value);
	if(!$value && in_array($name, array('TEL', 'EMAIL', 'ORG'))) {
		OC_JSON::error(array('data' => array('message' => $l->t('Cannot add empty property.'))));
		exit();
	}
} elseif($name === 'ADR') { // only add if non-empty elements.
	$empty = true;
	foreach($value as $part) {
		if(trim($part) != '') {
			$empty = false;
			break;
		}
	}
	if($empty) {
		OC_JSON::error(array('data' => array('message' => $l->t('At least one of the address fields has to be filled out.'))));
		exit();
	}
}
$parameters = isset($_POST['parameters']) ? $_POST['parameters'] : array();

$property = $vcard->addProperty($name, $value); //, $parameters);

$line = count($vcard->children) - 1;

// Apparently Sabre_VObject_Parameter doesn't do well with multiple values or I don't know how to do it. Tanghus.
foreach ($parameters as $key=>$element) {
	if(is_array($element) && strtoupper($key) == 'TYPE') { 
		// NOTE: Maybe this doesn't only apply for TYPE?
		// And it probably shouldn't be done here anyways :-/
		foreach($element as $e){
			if($e != '' && !is_null($e)){
				$vcard->children[$line]->parameters[] = new Sabre_VObject_Parameter($key,$e);
			}
		}
	} else {
			$vcard->children[$line]->parameters[] = new Sabre_VObject_Parameter($key,$element);
	}
{'fraction_non_alphanumeric': 0.16629314208874943, 'fraction_numerical': 0.003585835948005379, 'mean_word_length': 4.300475059382423, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7130129', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1407, 'n_words': 459}