Check if data exists - php

I'm working with an API.
With an array I collect data like this:
$org_payload = array(
'name' => $_POST['billing_company'],
'phone' => $_POST['billing_phone'],
'email' => $_POST['billing_email'],
'note' => $_POST['order_comments'],
'relation_type' => array(
'id'=>'relationtype:c1ec3ae77036842d' //provide the relationtypeid, f.e. relationtype:796ce0d318a2f5db515efc18bba82b90
),
'visiting_address' => array(
'country_code' => 'NL',
'line_1' => $_POST['billing_address_1'],
'postal_code' => $_POST['billing_postcode'],
'locality' => $_POST['billing_city'],
'country' => $_POST['billing_country']
), // can be extented with other address data
'postal_address' => array(
'country_code' => 'NL'
) // can be extented with other address data
);
At one point i send this data to the program i'm working with. I achieve this with this code:
$organization = $SimplicateApi->makeApiCall('POST','/crm/organization',json_encode($org_payload));
I gather this data from a form on my website. This data gets posted in the program.
I am trying to achieve that when data gathered from my form matches existing data in the program then don't add it. I would like a hint in the right direction for this, been looking on the internet without any luck.

What I would suggest is to have one extra call to the API.
Like you said in the comments - the company name and the phone number is unique.
If there is some call to get a user by those values and check what you got from the form, would be enough.
If they are unique - send them,
if not - show to the user or whatever you want to do here.
No need to keep one more database on your system as well.

Related

Odoo PHP API and Laradoo - how to save many2many many2one and selection fields

Could someone please provide a simple example of the usage for dealing with Odoo's one2many, many2many and selection fields when using Laradoo (or ripcord)?
Specifically how one would use them with create() and update(). In Python, it seems as if these are dealt with using special tuple commands however for PHP documentation seems very hard to find for these types of things and it would be extremely helpful.
For illustrative purposes in my particular project, I haven't been able to figure out how to relate a CRM lead tag to a lead during the creation process using Laradoo:
$id = $odoo->create('crm.lead', [
'type' => 'lead',
'priority' => 0, <-- what do we pass here for this selection field?
'name' => 'Example',
'contact_name' => 'John Doe',
'phone' => '555-555-5555',
'email_from' => 'example#domain.com',
'description' => 'Just some text.',
'tag_ids' => [1], <-- What do we pass here for this one2many field?
]);
In the example above when trying to set the priority selection field to an int other than 0 fails and when trying to pass an array of tag_ids (1 is valid tag id in my project), the lead remains untagged.
First of all selection field values are just string values that need to be part of the field defined selection values.
The values for relational fields like Onetomany and Many2many are ruled by the command formated values that you could read at:
https://github.com/odoo/odoo/blob/11.0/odoo/models.py#L3020-L3055
For the php api usage with ripcord you could set the tag_ids field value like:
$id = $odoo->create('crm.lead', [
'type' => 'lead',
'priority' => '0',
'name' => 'Example',
'contact_name' => 'John Doe',
'phone' => '555-555-5555',
'email_from' => 'example#domain.com',
'description' => 'Just some text.',
'tag_ids' => array(array(4,1)),
]);
This translate as that 1 is the id of a known and already existing crm.lead.tag that you could link to the m2m tag_ids field using the command 4. This could also be expressed using command 6 to link multiple ids on the same command value:
'tag_ids' => array(array(6,0,array(1,2,3))),
where using command 4 it will be:
'tag_ids' => array(array(4,1), array(4,2), array(4,3)),

Getting JSON data

Im working with an api which stores data into a JSON file. This data is gathered from a form that the users fill in my website. The way its inserted goes as follow:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
And then:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
Now instead of post i want to get the data. I tried getting data like this:
$SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=*my name*');
I dont know if this is the right way, well it didn't work so obviously its not.
Anyways what im trying to achieve is with PHP i want to gather the name value of an existing person. this data is stored in /api/v2/crm/person.json
Api documentation (which i read but didn't understand to well) http://api.simplicate.nl/
It's been a while but i'm trying to answer all my open questions without an answer which i ended up solving on my own.
So for this.
You have to create a variable which makes the get request like this:
$test = $SimplicateApi->makeApiCall('GET','/crm/organization?q[name]=My name');
Now you can for example do a var_dump($test);
And as output you will get all the data inside
/crm/organization?q[name]=My name

Codeigniter adding data to database and then process it

I am using codeigniter to built a type of online shop.
I want to create a process order function in order to verify the details of the order the clients puts in.
I am stuck though because on the last page i have the data to submit and when i click i go to main/process_order where i insert the data in the table and then use curl to comunicate with another server.
My question is: when i hit submit and then stop on the process_order page if i reload it 1000 times, the table will be filled with the same 1000 lines, so this can be a security issue. Also if i make a function to add the data to db and then redirect to process_order it will be another issue because i still need my data that was posted.
What's the best way to solve this. I hope i made it as clear as i can.
Code:
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
$this->db->insert('orders_temp', $data);
Maybe something like this can help. Do a check on the userid and the time before you insert the data. You would need make sure to do a search on database fields that are unique (unless the time field is the full time)
$data=array(
'userid' => $userid,
'email' => $email_data,
'phone' => $this->input->post('phone'),
'discount' => $this->input->post('discount'),
'price' => $this->input->post('price'),
'final_price' => $this->input->post('final_price'),
'client_data' => $this->input->post('client_info'),
'client_ip' => $this->input->post('client_ip'),
'time' => $date
);
if (<!--CONDITIONAL STATEMENT-->)
{
$this->db->insert('orders_temp', $data);
}

MongoDB - PHP manual references approach suitability

MongoDB newbie here. I'm having the first approach on references, and my main doubt is if i'am using the appropriate strategy(manual references) for my situation.
Working on 2 collections(user, message) in the same db, lets make an example of a document stored in user collection:
array (
'_id' => new MongoId("5231817277758e660c7202c4"),
'uname' => 'pluto',
'pwd' => new MongoInt32(1234567),
'email' => 'pluto1#gmail.com',
'phone_home' => new MongoInt64(23409238),
'phone_work' => new MongoInt64(54389724),
'phone_mobile' => new MongoInt64(9823422),
'visible' => new MongoInt32(1),
)
and an example of a document stored in message collection (sent FROM an other user TO the user above 'pluto'):
array (
'_id' => new MongoId("524358102660b2c70b8b4567"),
'from_uid' => '5231817277758e660c7202d7',
'to_uid' => '5231817277758e660c7202c4',
'object' => 'mongo manual Ref',
'content' => 'is that the correct approach?',
'datetime' => '2013-09-25 23:39:28',
)
The user logged in ('pluto') can see all the messages he received from other users but, i don't wat to print the 'from_uid' value, i want to replace it with the username of the sender.
My main doubt is if manual references is the right approach for this scenario, because with this technique(if i havn't miss understood everything), print a list of message would involve:
the 'query' for print the list of messages
an other 'query' for retrieve the username from the other collection, for each messages. Also if a user have received 1000 messages, this approach will have to run 1001 query??
My secondary doubt is if there is a way for directly merge or replace the result of two cursors
Given your use case it would probably be ok to duplicate a little bit of data and store some of the necessary fields about the sending user as an embedded document - in this case, the username.
array (
'_id' => new MongoId("524358102660b2c70b8b4567"),
'from_user' => array(
'uid' => '5231817277758e660c7202d7',
'uname' => 'mars'
),
'to_uid' => '5231817277758e660c7202c4',
'object' => 'mongo manual Ref',
'content' => 'is that the correct approach?',
'datetime' => '2013-09-25 23:39:28',
)
This approach means that when you query for messages to a user ( in which you already know the to_uid), you get all of the messages with the correct id to the from_user, but also already have their username to display.
Now, instead of querying 1000+ times, you only need to query when there is more information needed about a user - such as fetching their profile, etc...

Dynamically add fields to http_build_query

I have built an API that I want to test. By that reason I'm building a simple client to try out the different features (CRUD). Below is the function for updating a producer, which works fine. However, I also want to be able to update parts of a producer, e.g. address (/producers/8?method=put&address=milkyway).
The array producer always contains the same elements (name, address, zipcode etc) but I only want to update the producer with the elements in the array which contains of anything. What I mean with that is that if for example the name element in the array is empty then name shouldn't be included in *http_build_query*. If only the name element contains of anything then only name should be updated.
So, let's say that the array (except for id that of course is mandatory) contains of address. How can I dynamically add only that to *http_build_query* ?
Thanks in advance!
public function UpdateProducer($producer) {
$url = 'http://localhost/webbteknik2/Labb2/api/v1/producers/ . $producer['id'] . '?method=put';
$data = http_build_query(array(
'name' => $producer['name'],
'address' => $producer['address'],
'zipcode' => $producer['zipcode'],
'town' => $producer['town'],
'url' => $producer['url'],
'imgurl' => $producer['imgurl'],
'latitude' => $producer['latitude'],
'longitude' => $producer['longitude'],
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
...
the rest of the curl code
}
Note: I know this is bad coding in many ways, but as I said I only, asap want to be able to test the CRUD functionality through the client.
use array_filter to remove the empty elements....
$params = array(
'name' => $producer['name'],
'address' => $producer['address'],
'zipcode' => $producer['zipcode'],
'town' => $producer['town'],
'url' => $producer['url'],
'imgurl' => $producer['imgurl'],
'latitude' => $producer['latitude'],
'longitude' => $producer['longitude'],
);
$data = http_build_query(array_filter($params, 'is_null'));

Categories