CakePHP 2.0 LinkedIn Plugin - Post a share? - php

I am using this LinkedIn plugins for CakePHP 2.0.4: https://github.com/ProLoser/CakePHP-LinkedIn
I setup everything to work fine with login and get user's profiles like first name, last name,... using this syntax: $this->Linkedin->find("all",...). I renamed the default name,"Linkedin", of Linkedin datasource to "LinkedinSource", hence I can call my model "Linkedin" just for my convenience.
I followed the file /Plugin/Linkedin/Model/LinkedinMessage.php to create this function in my Linkedin model:
function updateStatus($message) {
$request = $this->request;
$request['uri']['path'] = 'people/~/shares';
$this->request = $request;
//Above 3 lines are used to bypass the error "changing value of overloaded object property has no effect if I use $this->request["uri"]["path"] = "..."
$data = array(
'comment' => $message
, 'visibility' => array('code' => 'anyone')
);
//Above settings follow this: https://developer.linkedin.com/documents/share-api
$saved = $this->save($data);
}
When I run above code using my own LinkedIn account (connected & authorized), the value of $saved is just TRUE but NO status/share is posted to my LinkedIn account when I open my account on a browser (Google Chrome)
I tried to change the URI Path to
$request['uri']['path'] = 'people/~/person-activities';
and request data to:
$data = array(
'content-type' => "linkedin-html"
, 'body' => $message
);
as in https://developer.linkedin.com/documents/post-network-update but still no better result.
I also change these lines in /Plugin/Linkedin/Config/LinkedinSource.php:
$config['Apis']['LinkedinSource']['write'] = array(
// http://developer.linkedin.com/docs/DOC-1044
'mailbox' => array(
'people/~/mailbox' => array(
'subject',
'body',
'recipients',
),
),
);
to
$config['Apis']['LinkedinSource']['write'] = array(
// http://developer.linkedin.com/docs/DOC-1044
'mailbox' => array(
'people/~/mailbox' => array(
'subject',
'body',
'recipients',
),
),
//https://developer.linkedin.com/documents/share-api
'shares' => array(
'people/~/shares' => array(
'comment',
'content',
'visibility',
),
),
);
but still no better result.
Just be noted I added these lines in /Plugin/Linkedin/Model/LinkedinMessage.php
public $startQuote;
public $endQuote;
to avoid these errors:
Undefined property: LinkedinSource::$startQuote [CORE/Cake/Model/Model.php, line 1269]
Undefined property: LinkedinSource::$endQuote [CORE/Cake/Model/Model.php, line 1269]
I dont know if that can cause my problem or not but I want to list all details here.
Please help as I spent almost one day to make this work but still can not :(

Found the problem! The Content-Type MUST be application/json in order to work! I thought x-li-format: json is enough but it is NOT.

Related

SQS SendMessage & $_POST Issue

If I put $author='Steven King'; it works without issue, however it does not work with a post variable.
To be "clear" if I hard code the the author in the JSON it will in fact post the message to the SQS queue. This is the expected result, however if I pass the string from the Post Variable e.g. $author=$_POST['author], the message is never delivered.
$message = array(
// Associative array of custom 'String' key names
'Author' => array(
'StringValue' =>$author,
'DataType' => 'String'
),
);
Any thoughts or help on this I would be grateful.
<?php
$author =$_POST["author"];
require 'vendor/autoload.php';
use Aws\Common\Aws;
use Aws\Sqs\SqsClient;
use Aws\Exception\AwsException;
// Get the client from the builder by namespace
$client = SqsClient::factory(array(
'profile' => 'default',
'region' => 'us-west-2',
'version' => '2012-11-05'
));
$queueUrl ='https://sqs.us-west-2.amazonaws.com/blahblahblah';
$message = array(
// Associative array of custom 'String' key names
'Author' => array(
'StringValue' =>$author,
'DataType' => 'String'
),
);
var_dump($message);
$result = $client->sendMessage(array(
'QueueUrl' => $queueUrl,
'MessageBody' => 'An awesome message!',
'MessageAttributes' =>$message,
));
So the issue was caused by the credential provider, which is why it worked in the cli e.g. php posts.php and not in the browser. Because in the CLI it has the correct environment and permissions.
Note: However on AWS SDK example does not include the credential provider only a reference to 'profile' => 'default' which would be thought to grab your default credentials, however that is not the case.
Thank you apache logs!
The fix was to set the right permissions on the /.aws/credentials and ensure
that your $HOME path is set correctly.
On to the next piece. Thanks community.

Upload a File to a Task on Cerb API

At the moment I am adding a task to Cerb using the API (Docs: https://cerb.ai/docs/api/). I can add and edit comments on the task.
An extension is to add file uploads. The API documentation seems to be extremely limited in terms of file uploads and isn't helping much. I have based my current code on https://cerb.ai/docs/api/endpoints/records/#update, using a custom field linked to the task and posting directly to the API.
The process is as follows:
1 - User inserts information on a Laravel form
2 - Form is submitted, task is created
3 - If the user entered a comment or description, the controller then uses the ID from the newly created task toa dd the comment
4 - If the user adds a file, the controller should then use the same ID from the task to attach the file
Task and comment adding has been done through models, which were previously done directly through the API and reformatted once they worked.
Add Task:
$params = [
'title' => $request->input('title'),
'links' => ['project:'.$request->input('project')],
'priority' => $request->input('priority'),
'status_id' => 2
];
$due_date = $request->input('due');
if(!empty($due_date)){
$params['due'] = strtotime($due_date);
}
$success_task = $task->addUpdateRecord('tasks.add', $params);
Add Comment:
$params = [
'author__context' => 'bot',
'author_id' => 3,
'comment' => empty($comment_input) ? request()->comment : $comment_input,
'target__context' => 'task',
'target_id' => $task_id,
'author' => Auth::user()->name // custom field
];
$success = $comment->addUpdateRecord('comments.add', $params);
The addUpdateRecord() essentially does the same thing as the file upload as below:
$cerb = new CerbApi();
$out = $cerb->put($root.'records/file_bundle/1054.json?expand=custom_', $putfields);
$putfields contains a normal file upload object.
I'm getting the following error on request:
array:2 [▼
"__status" => "error"
"message" => "Unknown command (root/records/task/1054.json)"
]
(root removed for confidentiality)
Has anyone worked with this? Anyone able to assist?
Ended up getting it right by using the attachments record type https://cerb.ai/docs/records/types/attachment/ and creating a model for it with its own addUpdateRecord() method.
I then used the records API to do the post for the file as the first step instead of the last one:
$file_upload = new File;
// Get file contents
$data = file_get_contents($file);
// Base64 encode contents
$data = base64_encode($data);
$file_params = [
'name' => $file->getClientOriginalName(),
'content' => $data
];
// Do upload
$file_success = $file_upload->addUpdateRecord('files.add', $file_params);
// Only need to return the file ID to link it to the task
return $file_success["data"]["id"];
And then using the ID that was being returned to link it to the task:
$task = new Task();
$params = [
'title' => $request->input('title'),
'links' => [
'project:'.$request->input('project'),
'file:'.$file_id
],
'priority' => $request->input('priority'),
'status_id' => 2
];
I hope this helps someone in the future.
Note: Cerb API Version 9.2.0

Changing display logic for a field in sugarcrm

I have the following situation: Contacts without a first or last name, in fact, they only have a email address.
I can work with these contacts fine, but when I use the listview anywhere (for instance to show all contacts from a company) there now is no way to click through to the contact (normally you would click on the name).
I'm looking for a way to solve this, for instance by showing a clickable text like 'name not known', but can't figure out how to do this. I've been looking at the manual and in the files in the modules directory and the sugarfields dir, but can't quite figure it out.
The closest I got was in /sugarcrm/modules/Contacts/metadata/listviewdefs.php
where this piece of code resides:
$listViewDefs['Contacts'] = array(
'NAME' => array(
'width' => '20%',
'label' => 'LBL_LIST_NAME',
'link' => true,
'contextMenu' => array('objectType' => 'sugarPerson',
'metaData' => array('contact_id' => '{$ID}',
'module' => 'Contacts',
'return_action' => 'ListView',
'contact_name' => '{$FULL_NAME}',
'parent_id' => '{$ACCOUNT_ID}',
'parent_name' => '{$ACCOUNT_NAME}',
'return_module' => 'Contacts',
'return_action' => 'ListView',
'parent_type' => 'Account',
'notes_parent_type' => 'Account')
),
'orderBy' => 'name',
'default' => true,
'related_fields' => array('first_name', 'last_name', 'salutation', 'account_name', 'account_id'),
),
Somewhere there has to be a function that joins the first and lastname together...
Edit: I found a solution:
The actual concatenation function is in /sugarcrm/include/SugarObjects/templates/person/person.php and is called _create_proper_name_field()
I can modify the output for my specific case by adding something like this to the end of the function:
if (empty(trim($full_name))){
$full_name = 'Name unknown';
}
However, I would rather have a upgrade safe solution, so that will be the next challenge.
Don't edit the core because the next upgrade will break your SugarCRM instance. Use logic hooks to be upgrade safe:
create a file 'logic_hooks.php' in /custom/modules/Contacts/
In that file, add the followin code:
<?php
$hook_array['before_save'][] = Array(1,'logic_fill_name','custom/modules/Contacts/logic_hooks/logics.php','ContactLogics','logic_fill_name');
After you have done this. create the file 'logics.php' in /custom/modules/Contacts/logic_hooks.
In the logics.php file, add something like:
<?php
require_once 'include/SugarQuery/SugarQuery.php';
/**
* Class ContactLogics
*/
class ContactLogics {
/**
* #param $bean
* #param $event
* #param $arguments
*/
public function logic_fill_name($bean, $event, $arguments) {
if (empty(trim($bean->first_name)) && empty(trim($bean->last_name))){
$bean->last_name = 'Name unknown';
}
}
}
Now some explanation. When you edited a recordview and pressed the save button, the logic hook 'before_save' will be triggered. This code will change the full name to 'Name unknown' when the full name is empty. When the 'before_save' is executed, the actual save will take place.

Cannot use 'email' field instead of 'username' in OAuth2 Plugin for CakePHP

Explanation:
Attempting to use this OAuth2 Plugin for CakePHP:
https://github.com/thomseddon/cakephp-oauth-server
Have followed the instructions, and am now going to this URL:
http://mysite/oauth/login?response_type=code&client_id=NGYcZDRjODcxYzFkY2Rk&
redirect_url=http%3A%2F%2Fwww.return_url.com
(We had made a client in the database with the same info he used in the example)
It brings up a log-in box for Email and Password, but fails authentication every time. I believe it's failing because by the time it gets to Cake's FormAuthenticate->authenticate() method, the settings have reverted to 'username'=>'username' and 'passwordHasher'=>'Simple'.
If we add these lines to the FormAuthenticate (above $fields = ...):
$this->settings['fields']['username'] = 'email';
$this->settings['passwordHasher'] = 'Blowfish';
Then the log-in works successfully.
Things We've tried:
Putting this in our AppController, the OAuthAppController, the OAuthController (all in beforeFilter):
$this->OAuth->authenticate = array(
'userModel' => 'Members',
'fields' => array(
'username' => 'email'
)
);
We've tried changing it to the new format like 2.3 in all of those places, as well as in the initial $components array in my AppModel:
$this->OAuth->authenticate = array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username'=>'email', 'password'=>'password'),
)
);
Closing:
At this point, I'm looking for any way (other than modifying the actual CakePHP core) to get it to be able to log-in with email instead of username (and hopefully that will solve the same issue with having it revert from Blowfish to Simple as well.
We've already tried heavily modifying the OAuth Plugin (to no avail) and aren't opposed to trying that again, but we can't figure out what to change.
Instead of using this in the OAuthController:
$this->OAuth->authenticate = array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username'=>'email', 'password'=>'password'),
)
);
Change it to this (notice removal of the "O" so it calls the regular "Auth"):
$this->Auth->authenticate = array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'fields' => array('username'=>'email', 'password'=>'password'),
)
);
Or, take it a step further, and set your $this->OAuth->authenticate array in your own AppController, then, in the OAuthController do this (instead of the above):
$this->Auth->authenticate = $this->OAuth->authenticate;

How to add a Contact in SugarCRM to a TargetList/ProspectList using API?

I have a script in PHP that runs a few things automatically on SugarCRM using RestAPI.
I'm using this PHP Class to manage the RestAPI:
http://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class/
Now I want to link a specific Contact to a ProspectList (Target List). I guess this must be made with a set_relationship call, but the PHPClass I am using does not have that.
I have tried writing the function myself like this:
public function set_relationship($module_ids, $module_names, $related_ids, $link_field_names){
$call_arguments = array(
'session' => $this->session,
'module_names' => $module_names,
'module_ids' => $module_ids,
'link_field_names' => $link_field_names,
'related_ids' => array($related_ids)
);
$result = $this->rest_request(
'set_relationship',
$call_arguments
);
return $result;
}
And then calling it like this:
$c->set_relationship(
$target_list['id'],
'ProspectLists',
$data['id'],
'Contacts'
);
But it does not work. Does anyone know how to make a REST/Soap call to connect a contact to a target list ?
Thank you
I can think of two issues based on your information.
Module might not be public, which is required for REST/SOAP API.
The argument names seem wrong according to the documentation.
Method with updated arguments:
public function set_relationship($module_name, $module_id, $link_field_name, $related_ids){
$call_arguments = array(
'session' => $this->session,
'module_name' => $module_name,
'module_id' => $module_id,
'link_field_name' => $link_field_name,
'related_ids' => array($related_ids)
);
$result = $this->rest_request(
'set_relationship',
$call_arguments
);
return $result;
}
And the call is
$c->set_relationship(
'ProspectLists',
$target_list['id'],
'contacts',
$data['id']
);
So I've finally figured it out, I've used the function from Kare's answer:
public function set_relationship($module_name, $module_id, $link_field_name, $related_ids){
$call_arguments = array(
'session' => $this->session,
'module_name' => $module_name,
'module_id' => $module_id,
'link_field_name' => $link_field_name,
'related_ids' => array($related_ids)
);
$result = $this->rest_request(
'set_relationship',
$call_arguments
);
return $result;
}
But I had to make the call differently:
$c->set_relationship(
'Contacts',
$contact_id,
'prospect_lists',
$target_list_id
);
I hope this will help someone

Categories