Exemple #1
0
function handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE)
{
    global $beanList, $beanFiles, $app_list_strings, $current_user;
    $error = new SoapError();
    $ret_values = array();
    if (empty($beanList[$module_name])) {
        $error->set_error('no_module');
        return array('ids' => array(), 'error' => $error->get_soap_array());
    }
    if (!check_modules_access($current_user, $module_name, 'write')) {
        $error->set_error('no_access');
        return array('ids' => -1, 'error' => $error->get_soap_array());
    }
    $class_name = $beanList[$module_name];
    require_once $beanFiles[$class_name];
    $ids = array();
    $count = 1;
    $total = sizeof($name_value_lists);
    foreach ($name_value_lists as $name_value_list) {
        $seed = new $class_name();
        $seed->update_vcal = false;
        //See if we can retrieve the seed by a given id value
        foreach ($name_value_list as $value) {
            if ($value['name'] == 'id') {
                $seed->retrieve($value['value']);
                break;
            }
        }
        $dataValues = array();
        foreach ($name_value_list as $value) {
            $val = $value['value'];
            if ($seed->field_name_map[$value['name']]['type'] == 'enum' || $seed->field_name_map[$value['name']]['type'] == 'radioenum') {
                $vardef = $seed->field_name_map[$value['name']];
                if (isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$val])) {
                    if (in_array($val, $app_list_strings[$vardef['options']])) {
                        $val = array_search($val, $app_list_strings[$vardef['options']]);
                    }
                }
            } else {
                if ($seed->field_name_map[$value['name']]['type'] == 'multienum') {
                    $vardef = $seed->field_name_map[$value['name']];
                    if (isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value])) {
                        $items = explode(",", $val);
                        $parsedItems = array();
                        foreach ($items as $item) {
                            if (in_array($item, $app_list_strings[$vardef['options']])) {
                                $keyVal = array_search($item, $app_list_strings[$vardef['options']]);
                                array_push($parsedItems, $keyVal);
                            }
                        }
                        if (!empty($parsedItems)) {
                            $val = encodeMultienumValue($parsedItems);
                        }
                    }
                }
            }
            //Apply the non-empty values now since this will be used for duplicate checks
            //allow string or int of 0 to be updated if set.
            if (!empty($val) || ($val === '0' || $val === 0)) {
                $seed->{$value}['name'] = $val;
            }
            //Store all the values in dataValues Array to apply later
            $dataValues[$value['name']] = $val;
        }
        if ($count == $total) {
            $seed->update_vcal = false;
        }
        $count++;
        //Add the account to a contact
        if ($module_name == 'Contacts') {
            $GLOBALS['log']->debug('Creating Contact Account');
            add_create_account($seed);
            $duplicate_id = check_for_duplicate_contacts($seed);
            if ($duplicate_id == null) {
                if ($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))) {
                    //Now apply the values, since this is not a duplicate we can just pass false for the $firstSync argument
                    apply_values($seed, $dataValues, false);
                    $seed->save();
                    if ($seed->deleted == 1) {
                        $seed->mark_deleted($seed->id);
                    }
                    $ids[] = $seed->id;
                }
            } else {
                //since we found a duplicate we should set the sync flag
                if ($seed->ACLAccess('Save')) {
                    //Determine if this is a first time sync.  We find out based on whether or not a contacts_users relationship exists
                    $seed->id = $duplicate_id;
                    $seed->load_relationship("user_sync");
                    $beans = $seed->user_sync->getBeans();
                    $first_sync = empty($beans);
                    //Now apply the values and indicate whether or not this is a first time sync
                    apply_values($seed, $dataValues, $first_sync);
                    $seed->contacts_users_id = $current_user->id;
                    $seed->save();
                    $ids[] = $duplicate_id;
                    //we have a conflict
                }
            }
        } else {
            if ($module_name == 'Meetings' || $module_name == 'Calls') {
                //we are going to check if we have a meeting in the system
                //with the same outlook_id. If we do find one then we will grab that
                //id and save it
                if ($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))) {
                    if (empty($seed->id) && !isset($seed->id)) {
                        if (!empty($seed->outlook_id) && isset($seed->outlook_id)) {
                            //at this point we have an object that does not have
                            //the id set, but does have the outlook_id set
                            //so we need to query the db to find if we already
                            //have an object with this outlook_id, if we do
                            //then we can set the id, otherwise this is a new object
                            $order_by = "";
                            $query = $seed->table_name . ".outlook_id = '" . $seed->outlook_id . "'";
                            $response = $seed->get_list($order_by, $query, 0, -1, -1, 0);
                            $list = $response['list'];
                            if (count($list) > 0) {
                                foreach ($list as $value) {
                                    $seed->id = $value->id;
                                    break;
                                }
                            }
                            //fi
                        }
                        //fi
                    }
                    //fi
                    if (empty($seed->reminder_time)) {
                        $seed->reminder_time = -1;
                    }
                    if ($seed->reminder_time == -1) {
                        $defaultRemindrTime = $current_user->getPreference('reminder_time');
                        if ($defaultRemindrTime != -1) {
                            $seed->reminder_checked = '1';
                            $seed->reminder_time = $defaultRemindrTime;
                        }
                    }
                    $seed->save();
                    if ($seed->deleted == 1) {
                        $seed->mark_deleted($seed->id);
                    }
                    $ids[] = $seed->id;
                }
                //fi
            } else {
                if ($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))) {
                    $seed->save();
                    $ids[] = $seed->id;
                }
            }
        }
        // if somebody is calling set_entries_detail() and wants fields returned...
        if ($select_fields !== FALSE) {
            $ret_values[$count] = array();
            foreach ($select_fields as $select_field) {
                if (isset($seed->{$select_field})) {
                    $ret_values[$count][] = get_name_value($select_field, $seed->{$select_field});
                }
            }
        }
    }
    // handle returns for set_entries_detail() and set_entries()
    if ($select_fields !== FALSE) {
        return array('name_value_lists' => $ret_values, 'error' => $error->get_soap_array());
    } else {
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
}
function handle_set_entries($module_name, $name_value_lists, $select_fields = FALSE)
{
    global $beanList, $beanFiles, $app_list_strings, $current_user;
    $error = new SoapError();
    $ret_values = array();
    if (empty($beanList[$module_name])) {
        $error->set_error('no_module');
        return array('ids' => array(), 'error' => $error->get_soap_array());
    }
    if (!check_modules_access($current_user, $module_name, 'write')) {
        $error->set_error('no_access');
        return array('ids' => -1, 'error' => $error->get_soap_array());
    }
    $class_name = $beanList[$module_name];
    require_once $beanFiles[$class_name];
    $ids = array();
    $count = 1;
    $total = sizeof($name_value_lists);
    foreach ($name_value_lists as $name_value_list) {
        $seed = new $class_name();
        $seed->update_vcal = false;
        //See if we can retrieve the seed by a given id value
        foreach ($name_value_list as $value) {
            if ($value['name'] == 'id') {
                $seed->retrieve($value['value']);
                break;
            }
        }
        $dataValues = array();
        foreach ($name_value_list as $value) {
            $val = $value['value'];
            if ($seed->field_name_map[$value['name']]['type'] == 'enum' || $seed->field_name_map[$value['name']]['type'] == 'radioenum') {
                $vardef = $seed->field_name_map[$value['name']];
                if (isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$val])) {
                    if (in_array($val, $app_list_strings[$vardef['options']])) {
                        $val = array_search($val, $app_list_strings[$vardef['options']]);
                    }
                }
            } else {
                if ($seed->field_name_map[$value['name']]['type'] == 'multienum') {
                    $vardef = $seed->field_name_map[$value['name']];
                    if (isset($app_list_strings[$vardef['options']]) && !isset($app_list_strings[$vardef['options']][$value])) {
                        $items = explode(",", $val);
                        $parsedItems = array();
                        foreach ($items as $item) {
                            if (in_array($item, $app_list_strings[$vardef['options']])) {
                                $keyVal = array_search($item, $app_list_strings[$vardef['options']]);
                                array_push($parsedItems, $keyVal);
                            }
                        }
                        if (!empty($parsedItems)) {
                            $val = encodeMultienumValue($parsedItems);
                        }
                    }
                }
            }
            //Apply the non-empty values now since this will be used for duplicate checks
            //allow string or int of 0 to be updated if set.
            if (!empty($val) || ($val === '0' || $val === 0)) {
                $seed->{$value['name']} = $val;
            }
            //Store all the values in dataValues Array to apply later
            $dataValues[$value['name']] = $val;
        }
        if ($count == $total) {
            $seed->update_vcal = false;
        }
        $count++;
        //Add the account to a contact
        if ($module_name == 'Contacts') {
            $GLOBALS['log']->debug('Creating Contact Account');
            add_create_account($seed);
            $duplicate_id = check_for_duplicate_contacts($seed);
            if ($duplicate_id == null) {
                if ($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))) {
                    //Now apply the values, since this is not a duplicate we can just pass false for the $firstSync argument
                    apply_values($seed, $dataValues, false);
                    $seed->save();
                    if ($seed->deleted == 1) {
                        $seed->mark_deleted($seed->id);
                    }
                    $ids[] = $seed->id;
                }
            } else {
                //since we found a duplicate we should set the sync flag
                if ($seed->ACLAccess('Save')) {
                    //Determine if this is a first time sync.  We find out based on whether or not a contacts_users relationship exists
                    $seed->id = $duplicate_id;
                    $seed->load_relationship("user_sync");
                    $beans = $seed->user_sync->getBeans();
                    $first_sync = empty($beans);
                    //Now apply the values and indicate whether or not this is a first time sync
                    apply_values($seed, $dataValues, $first_sync);
                    $seed->contacts_users_id = $current_user->id;
                    $seed->save();
                    $ids[] = $duplicate_id;
                    //we have a conflict
                }
            }
        } else {
            if ($module_name == 'Meetings' || $module_name == 'Calls') {
                //we are going to check if we have a meeting in the system
                //with the same outlook_id. If we do find one then we will grab that
                //id and save it
                if ($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))) {
                    // Check if we're updating an old record, or creating a new
                    if (empty($seed->id)) {
                        // If it's a new one, and we have outlook_id set
                        // which means we're syncing from OPI check if it already exists
                        if (!empty($seed->outlook_id)) {
                            $GLOBALS['log']->debug('Looking for ' . $module_name . ' with outlook_id ' . $seed->outlook_id);
                            $fields = array('outlook_id' => $seed->outlook_id);
                            // Try to fetch a bean with this outlook_id
                            $temp = BeanFactory::getBean($module_name);
                            $temp = $temp->retrieve_by_string_fields($fields);
                            // If we fetched one, just copy the ID to the one we're syncing
                            if (!empty($temp)) {
                                $seed->id = $temp->id;
                            } else {
                                $GLOBALS['log']->debug('Looking for ' . $module_name . ' with name/date_start/duration_hours/duration_minutes ' . $seed->name . '/' . $seed->date_start . '/' . $seed->duration_hours . '/' . $seed->duration_minutes);
                                // If we didn't, try to find the meeting by comparing the passed
                                // Subject, start date and duration
                                $fields = array('name' => $seed->name, 'date_start' => $seed->date_start, 'duration_hours' => $seed->duration_hours, 'duration_minutes' => $seed->duration_minutes);
                                $temp = BeanFactory::getBean($module_name);
                                $temp = $temp->retrieve_by_string_fields($fields);
                                if (!empty($temp)) {
                                    $seed->id = $temp->id;
                                }
                            }
                            $GLOBALS['log']->debug($module_name . ' found: ' . !empty($seed->id));
                        }
                    }
                    if (empty($seed->reminder_time)) {
                        $seed->reminder_time = -1;
                    }
                    if ($seed->reminder_time == -1) {
                        $defaultRemindrTime = $current_user->getPreference('reminder_time');
                        if ($defaultRemindrTime != -1) {
                            $seed->reminder_checked = '1';
                            $seed->reminder_time = $defaultRemindrTime;
                        }
                    }
                    $seed->save();
                    if ($seed->deleted == 1) {
                        $seed->mark_deleted($seed->id);
                    }
                    $ids[] = $seed->id;
                }
                //fi
            } else {
                if ($seed->ACLAccess('Save') && ($seed->deleted != 1 || $seed->ACLAccess('Delete'))) {
                    $seed->save();
                    $ids[] = $seed->id;
                }
            }
        }
        // if somebody is calling set_entries_detail() and wants fields returned...
        if ($select_fields !== FALSE) {
            $ret_values[$count] = array();
            foreach ($select_fields as $select_field) {
                if (isset($seed->{$select_field})) {
                    $ret_values[$count][] = get_name_value($select_field, $seed->{$select_field});
                }
            }
        }
    }
    // handle returns for set_entries_detail() and set_entries()
    if ($select_fields !== FALSE) {
        return array('name_value_lists' => $ret_values, 'error' => $error->get_soap_array());
    } else {
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
}