From 2b275a9dbaf8250fd530d8aa9e12486d7479b4b2 Mon Sep 17 00:00:00 2001 From: Michael Russell Date: Wed, 10 Jul 2019 14:48:05 -0400 Subject: [PATCH 1/3] Update Inline Code Copywrite/License Message + Add in new Filter Operators --- package.json | 2 +- src/Auth/SugarOAuthController.php | 2 +- src/Client/Sugar7API.php | 6 +- .../Abstracts/AbstractSmartSugarEndpoint.php | 2 +- .../AbstractSugarBeanCollectionEndpoint.php | 2 +- .../Abstracts/AbstractSugarBeanEndpoint.php | 2 +- .../AbstractSugarCollectionEndpoint.php | 2 +- .../Abstracts/AbstractSugarEndpoint.php | 2 +- src/Endpoint/Bulk.php | 2 +- src/Endpoint/Data/BulkRequest.php | 2 +- src/Endpoint/Data/FilterData.php | 2 +- .../Filters/Expression/AbstractExpression.php | 10 +- .../Data/Filters/Expression/AndExpression.php | 2 +- .../Filters/Expression/DateExpression.php | 146 ++++++++++++++++++ .../Expression/ExpressionInterface.php | 2 +- .../Data/Filters/Expression/OrExpression.php | 2 +- src/Endpoint/Data/Filters/FilterInterface.php | 2 +- .../Filters/Operator/AbstractOperator.php | 2 +- .../Data/Filters/Operator/Between.php | 15 ++ .../Data/Filters/Operator/Contains.php | 2 +- .../Data/Filters/Operator/DateBetween.php | 15 ++ .../Data/Filters/Operator/DateRange.php | 15 ++ src/Endpoint/Data/Filters/Operator/Ends.php | 2 +- src/Endpoint/Data/Filters/Operator/Equals.php | 2 +- .../Data/Filters/Operator/GreaterThan.php | 2 +- .../Filters/Operator/GreaterThanOrEqual.php | 2 +- src/Endpoint/Data/Filters/Operator/In.php | 2 +- src/Endpoint/Data/Filters/Operator/IsNull.php | 2 +- .../Data/Filters/Operator/LessThan.php | 2 +- .../Data/Filters/Operator/LessThanOrEqual.php | 2 +- .../Data/Filters/Operator/NotEquals.php | 2 +- src/Endpoint/Data/Filters/Operator/NotIn.php | 2 +- .../Data/Filters/Operator/NotNull.php | 2 +- src/Endpoint/Data/Filters/Operator/Starts.php | 2 +- src/Endpoint/Enum.php | 2 +- src/Endpoint/Me.php | 2 +- src/Endpoint/Metadata.php | 2 +- src/Endpoint/Module.php | 2 +- src/Endpoint/ModuleFilter.php | 2 +- src/Endpoint/OAuth2Logout.php | 2 +- src/Endpoint/OAuth2Refresh.php | 2 +- src/Endpoint/OAuth2Sudo.php | 2 +- src/Endpoint/OAuth2Token.php | 2 +- src/Endpoint/Ping.php | 2 +- .../Provider/SugarEndpointProvider.php | 2 +- src/Endpoint/Search.php | 2 +- src/Endpoint/SugarEndpointInterface.php | 2 +- .../Filter/MissingFieldForDateExpression.php | 14 ++ .../Filter/UnknownFilterOperator.php | 3 + src/Helpers/Helper.php | 10 +- tests/Client/Sugar7APITest.php | 4 + ...bstractSugarBeanCollectionEndpointTest.php | 2 +- .../AbstractSugarBeanEndpointTest.php | 2 +- .../AbstractSugarCollectionEndpointTest.php | 2 +- tests/Endpoint/Data/BulkRequestTest.php | 2 +- .../Data/Filters/AbstractExpressionTest.php | 66 ++++++-- .../Data/Filters/DateExpressionTest.php | 116 ++++++++++++++ tests/Endpoint/Data/SearchTest.php | 2 +- tests/Endpoint/MeTest.php | 2 +- tests/Endpoint/MetadataTest.php | 2 +- tests/Endpoint/ModuleFilterTest.php | 2 +- tests/Endpoint/PingTest.php | 2 +- tests/bootstrap.php | 2 +- 63 files changed, 452 insertions(+), 70 deletions(-) create mode 100644 src/Endpoint/Data/Filters/Expression/DateExpression.php create mode 100644 src/Endpoint/Data/Filters/Operator/Between.php create mode 100644 src/Endpoint/Data/Filters/Operator/DateBetween.php create mode 100644 src/Endpoint/Data/Filters/Operator/DateRange.php create mode 100644 src/Exception/Filter/MissingFieldForDateExpression.php create mode 100644 tests/Endpoint/Data/Filters/DateExpressionTest.php diff --git a/package.json b/package.json index edc4f1d..78c1ee4 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": false, "name": "rest-php-client", - "version": "1.2", + "version": "2.0", "description": "An object oriented framework for interacting with Sugar 7's REST API.", "directories": { "test": "tests" diff --git a/src/Auth/SugarOAuthController.php b/src/Auth/SugarOAuthController.php index 1fc8a0f..9f24cde 100644 --- a/src/Auth/SugarOAuthController.php +++ b/src/Auth/SugarOAuthController.php @@ -1,6 +1,6 @@ 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual', 'greaterThanOrEqualTo' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual', 'greaterThanOrEquals' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual', + 'between' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\Between', + 'dateBetween' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\DateBetween' ); /** @@ -75,6 +80,7 @@ abstract class AbstractExpression implements FilterInterface, ExpressionInterfac protected $expressions = array( 'and' => 'Sugarcrm\REST\Endpoint\Data\Filters\Expression\AndExpression', 'or' => 'Sugarcrm\REST\Endpoint\Data\Filters\Expression\OrExpression', + 'date' => 'Sugarcrm\REST\Endpoint\Data\Filters\Expression\DateExpression', ); /** diff --git a/src/Endpoint/Data/Filters/Expression/AndExpression.php b/src/Endpoint/Data/Filters/Expression/AndExpression.php index fa09f32..b03fc82 100644 --- a/src/Endpoint/Data/Filters/Expression/AndExpression.php +++ b/src/Endpoint/Data/Filters/Expression/AndExpression.php @@ -1,6 +1,6 @@ 'yesterday', + 'today' => 'today', + 'tomorrow' => 'tomorrow', + 'last7Days' => 'last_7_days', + 'next7Days' => 'next_7_days', + 'last30days' => 'last_30_days', + 'next30Days' => 'next_30_days', + 'lastMonth' => 'last_month', + 'thisMonth' => 'this_month', + 'nextMonth' => 'next_month', + 'lastYear' => 'last_year', + 'thisYear' => 'this_year', + 'nextYear' => 'next_year', + ); + + /** + * @var array + */ + protected $operators = array( + 'equals' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\Equals', + 'notEquals' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\NotEquals', + 'isNull' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\IsNull', + 'notNull' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\NotNull', + 'lt' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\LessThan', + 'lessThan' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\LessThan', + 'lte' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\LessThanOrEqual', + 'lessThanOrEqualTo' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\LessThanOrEqual', + 'lessThanOrEquals' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\LessThanOrEqual', + 'gt' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThan', + 'greaterThan' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThan', + 'gte' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual', + 'greaterThanOrEqualTo' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual', + 'greaterThanOrEquals' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\GreaterThanOrEqual', + 'dateBetween' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\DateBetween', + 'between' => 'Sugarcrm\REST\Endpoint\Data\Filters\Operator\DateBetween' + ); + + /** + * @var array + */ + protected $expressions = array(); + + /** + * DateExpression constructor. + * @param array $arguments + */ + public function __construct($arguments = array()) + { + if (isset($arguments[0])){ + $this->field($arguments[0]); + } + } + + /** + * Set the field that date expression is against + * @param $field + * @return $this + */ + public function field($field) + { + $this->dateField = $field; + return $this; + } + + public function __call($name, $arguments) + { + if (empty($this->dateField)){ + throw new MissingFieldForDateExpression(); + } + $args = array($this->dateField); + if (array_key_exists($name,$this->ranges)){ + $range = $this->ranges[$name]; + $args[] = $range; + $Op = new DateRange($args); + $this->filters[] = $Op; + return $this; + } + if (array_key_exists($name,$this->operators)){ + $args = array_merge($args,$arguments); + $Operator = $this->operators[$name]; + $O = new $Operator($args); + $this->filters[] = $O; + return $this; + } + throw new UnknownFilterOperator(array($name)); + } + + /** + * Human Friendly Expression End, allow you to traverse back up the Filter expression + * @return AbstractExpression + * @codeCoverageIgnore + */ + public function endDate(){ + return $this->getParentExpression(); + } +} \ No newline at end of file diff --git a/src/Endpoint/Data/Filters/Expression/ExpressionInterface.php b/src/Endpoint/Data/Filters/Expression/ExpressionInterface.php index c5e60fd..801e45f 100644 --- a/src/Endpoint/Data/Filters/Expression/ExpressionInterface.php +++ b/src/Endpoint/Data/Filters/Expression/ExpressionInterface.php @@ -1,6 +1,6 @@ assertEquals(10,$Client->getVersion()); $this->assertEquals('localhost',$Client->getServer()); $this->assertEquals('http://localhost/rest/v10/',$Client->getAPIUrl()); + + $Client->setVersion("11_4"); + $this->assertEquals("11_4",$Client->getVersion()); + $this->assertEquals('http://localhost/rest/v11_4/',$Client->getAPIUrl()); } /** diff --git a/tests/Endpoint/AbstractSugarBeanCollectionEndpointTest.php b/tests/Endpoint/AbstractSugarBeanCollectionEndpointTest.php index 892d706..7e4653c 100644 --- a/tests/Endpoint/AbstractSugarBeanCollectionEndpointTest.php +++ b/tests/Endpoint/AbstractSugarBeanCollectionEndpointTest.php @@ -1,6 +1,6 @@ foo(); - } - /** * @covers ::getParentExpression * @covers ::setParentExpression @@ -62,8 +54,64 @@ public function testGetParentExpression(){ /** * @covers ::__call */ - public function testCall(){ + public function testCall() + { + $Expression = new AndExpression(); + $this->assertEquals($Expression,$Expression->equals('foo','bar')); + $this->assertEquals($Expression,$Expression->notEquals('foo','bar')); + $this->assertEquals($Expression,$Expression->starts('foo','bar')); + $this->assertEquals($Expression,$Expression->ends('foo','bar')); + $this->assertEquals($Expression,$Expression->contains('foo','bar')); + $this->assertEquals($Expression,$Expression->in('foo',array('bar'))); + $this->assertEquals($Expression,$Expression->notIn('foo',array("bar"))); + $this->assertEquals($Expression,$Expression->isNull('foo')); + $this->assertEquals($Expression,$Expression->notNull('foo')); + $this->assertEquals($Expression,$Expression->lt('foo','bar')); + $this->assertEquals($Expression,$Expression->lessThan('foo','bar')); + $this->assertEquals($Expression,$Expression->lte('foo','bar')); + $this->assertEquals($Expression,$Expression->lessThanOrEqualTo('foo','bar')); + $this->assertEquals($Expression,$Expression->lessThanOrEquals('foo','bar')); + $this->assertEquals($Expression,$Expression->gte('foo','bar')); + $this->assertEquals($Expression,$Expression->greaterThanOrEqualTo('foo','bar')); + $this->assertEquals($Expression,$Expression->greaterThanOrEquals('foo','bar')); + $this->assertEquals($Expression,$Expression->between('foo','bar')); + $this->assertEquals($Expression,$Expression->dateBetween('foo','bar')); + $this->assertInstanceOf("Sugarcrm\REST\Endpoint\Data\Filters\Expression\AndExpression",$Expression->and()); + $this->assertInstanceOf("Sugarcrm\REST\Endpoint\Data\Filters\Expression\OrExpression",$Expression->or()); + $this->assertInstanceOf("Sugarcrm\REST\Endpoint\Data\Filters\Expression\DateExpression",$Expression->date('test')); + $Expression = new OrExpression(); + $this->assertEquals($Expression,$Expression->equals('foo','bar')); + $this->assertEquals($Expression,$Expression->notEquals('foo','bar')); + $this->assertEquals($Expression,$Expression->starts('foo','bar')); + $this->assertEquals($Expression,$Expression->ends('foo','bar')); + $this->assertEquals($Expression,$Expression->contains('foo','bar')); + $this->assertEquals($Expression,$Expression->in('foo',array('bar'))); + $this->assertEquals($Expression,$Expression->notIn('foo',array("bar"))); + $this->assertEquals($Expression,$Expression->isNull('foo')); + $this->assertEquals($Expression,$Expression->notNull('foo')); + $this->assertEquals($Expression,$Expression->lt('foo','bar')); + $this->assertEquals($Expression,$Expression->lessThan('foo','bar')); + $this->assertEquals($Expression,$Expression->lte('foo','bar')); + $this->assertEquals($Expression,$Expression->lessThanOrEqualTo('foo','bar')); + $this->assertEquals($Expression,$Expression->lessThanOrEquals('foo','bar')); + $this->assertEquals($Expression,$Expression->gte('foo','bar')); + $this->assertEquals($Expression,$Expression->greaterThanOrEqualTo('foo','bar')); + $this->assertEquals($Expression,$Expression->greaterThanOrEquals('foo','bar')); + $this->assertEquals($Expression,$Expression->between('foo','bar')); + $this->assertEquals($Expression,$Expression->dateBetween('foo','bar')); + $this->assertInstanceOf("Sugarcrm\REST\Endpoint\Data\Filters\Expression\AndExpression",$Expression->and()); + $this->assertInstanceOf("Sugarcrm\REST\Endpoint\Data\Filters\Expression\OrExpression",$Expression->or()); + $this->assertInstanceOf("Sugarcrm\REST\Endpoint\Data\Filters\Expression\DateExpression",$Expression->date('test')); + } + + /** + * @covers ::__call + * @expectedException Sugarcrm\REST\Exception\Filter\UnknownFilterOperator + */ + public function testUnknownFilterOperatorException(){ + $Expression = new AndExpression(); + $Expression->foo(); } } diff --git a/tests/Endpoint/Data/Filters/DateExpressionTest.php b/tests/Endpoint/Data/Filters/DateExpressionTest.php new file mode 100644 index 0000000..1d473e8 --- /dev/null +++ b/tests/Endpoint/Data/Filters/DateExpressionTest.php @@ -0,0 +1,116 @@ +getProperty('dateField'); + + $dateField->setAccessible(true); + + $this->assertEquals('test',$dateField->getValue($Date)); + $Date = new DateExpression(); + $this->assertEmpty($dateField->getValue($Date)); + $this->assertEquals($Date,$Date->field('test')); + $this->assertEquals('test',$dateField->getValue($Date)); + + } + + /** + * @covers ::__call + */ + public function testCall() + { + $Expression = new DateExpression(array('foobar')); + $this->assertEquals($Expression,$Expression->equals('bar')); + $this->assertEquals($Expression,$Expression->notEquals('foo')); + $this->assertEquals($Expression,$Expression->isNull()); + $this->assertEquals($Expression,$Expression->notNull()); + $this->assertEquals($Expression,$Expression->lt('foo')); + $this->assertEquals($Expression,$Expression->lessThan('foo')); + $this->assertEquals($Expression,$Expression->lte('foo')); + $this->assertEquals($Expression,$Expression->lessThanOrEqualTo('foo')); + $this->assertEquals($Expression,$Expression->lessThanOrEquals('foo')); + $this->assertEquals($Expression,$Expression->gte('foo')); + $this->assertEquals($Expression,$Expression->greaterThanOrEqualTo('foo')); + $this->assertEquals($Expression,$Expression->greaterThanOrEquals('foo')); + $this->assertEquals($Expression,$Expression->between('foo')); + + $this->assertEquals($Expression,$Expression->yesterday()); + $this->assertEquals($Expression,$Expression->today()); + $this->assertEquals($Expression,$Expression->tomorrow()); + $this->assertEquals($Expression,$Expression->last7Days()); + $this->assertEquals($Expression,$Expression->next7Days()); + $this->assertEquals($Expression,$Expression->last30days()); + $this->assertEquals($Expression,$Expression->next30Days()); + $this->assertEquals($Expression,$Expression->lastMonth()); + $this->assertEquals($Expression,$Expression->thisMonth()); + $this->assertEquals($Expression,$Expression->nextMonth()); + $this->assertEquals($Expression,$Expression->lastYear()); + $this->assertEquals($Expression,$Expression->thisYear()); + $this->assertEquals($Expression,$Expression->nextYear()); + } + + /** + * @covers ::__call + * @expectedException Sugarcrm\REST\Exception\Filter\UnknownFilterOperator + */ + public function testUnknownFilterOperatorException(){ + $Expression = new DateExpression(); + $Expression->field("foo"); + $Expression->foobar(); + } + + /** + * @covers::__call + * @expectedException Sugarcrm\REST\Exception\Filter\MissingFieldForDateExpression + */ + public function testMissingFieldException() + { + $Expression = new DateExpression(); + $Expression->yesterday(); + } + +} diff --git a/tests/Endpoint/Data/SearchTest.php b/tests/Endpoint/Data/SearchTest.php index 352857f..db89c62 100644 --- a/tests/Endpoint/Data/SearchTest.php +++ b/tests/Endpoint/Data/SearchTest.php @@ -1,6 +1,6 @@ Date: Sat, 13 Jul 2019 15:59:27 -0400 Subject: [PATCH 2/3] SUPP-1256, SUPP-1255 Cleaned up Examples and modified them to test out the Date filter setup, the duplicateCheck request, and the count endpoints on Filter API. --- examples/AuditLog.php | 43 +++++++------ examples/CRUD.php | 52 +++++++++------ examples/DuplicateCheck.php | 30 +++++++++ examples/Favorite.php | 31 +++++---- examples/FileManipulation.php | 57 +++++++++-------- examples/FilterAPI.php | 64 +++++++++++++------ examples/FilterRelated.php | 40 ++++++------ examples/ModuleEnum.php | 27 ++++---- examples/RelateRecords.php | 48 ++++++++------ examples/Sudo.php | 27 +++++--- examples/include.php | 17 ++++- .../Abstracts/AbstractSugarBeanEndpoint.php | 24 +++++++ .../Filters/Expression/AbstractExpression.php | 2 +- .../Filters/Expression/DateExpression.php | 12 +++- .../Data/Filters/Operator/DateRange.php | 2 +- src/Endpoint/ModuleFilter.php | 4 +- 16 files changed, 317 insertions(+), 163 deletions(-) create mode 100644 examples/DuplicateCheck.php diff --git a/examples/AuditLog.php b/examples/AuditLog.php index 97838a3..4044012 100644 --- a/examples/AuditLog.php +++ b/examples/AuditLog.php @@ -1,26 +1,33 @@ login(); - echo "Logged In:
";
-    print_r($SugarAPI->getAuth()->getToken()->access_token);
-    echo "
"; - $Account = $SugarAPI->module('Accounts')->set("name","Audit Log Test"); - $Account->save(); - echo "
Created Account: {$Account['id']}

"; - $Account->set('phone_office','555-555-5555'); - $Account['name'] = 'Audit Log Test - Updated'; - $Account['assigned_user_id'] = 'seed_max_id'; - $Account->save(); - echo "
 Account Updated: 
".print_r($Account->asArray(),true)."

"; - $Account->audit(); - echo "
 Audit Log:".print_r($Account->getResponse()->getBody(),true)."

"; + if ($SugarAPI->login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + $Account = $SugarAPI->module('Accounts')->set("name","Audit Log Test"); + $Account->save(); + pre("Created Account: {$Account['id']}"); + $Account->set('phone_office','555-555-5555'); + $Account['name'] = 'Audit Log Test - Updated'; + $Account['assigned_user_id'] = 'seed_max_id'; + $Account->save(); + echo "Account Updated:"; + pre($Account->asArray()); + $Account->audit(); + echo "Audit Log: "; + pre($Account->getResponse()->getBody()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); + pre($ex->getTraceAsString()); } \ No newline at end of file diff --git a/examples/CRUD.php b/examples/CRUD.php index f37aeb3..feefbec 100644 --- a/examples/CRUD.php +++ b/examples/CRUD.php @@ -1,29 +1,39 @@ login(); - echo "
";
-    print_r($SugarAPI->getAuth()->getToken()->access_token);
-    echo "
"; - $Account = $SugarAPI->module('Accounts')->set("name","Test")->set("phone_office","555-555-5555"); - echo "
 Account:".print_r($Account->asArray(),true)."

"; - $Account->save(); - echo "
 Saved Account ID: {$Account['id']}

"; - $Account->set('employees','100'); - $Account['shipping_address_city'] = 'Indianapolis'; - $Account->save(); - echo "
 Account Updated: 
".print_r($Account->asArray(),true)."
"; - $Account2 = $SugarAPI->module('Accounts',$Account['id']); - $Account2->retrieve(); - echo "
 Account2:".print_r($Account2->asArray(),true)."

"; - $Account2->delete(); - echo "Account Deleted.
".print_r($Account2->getResponse(),true)."
"; + if ($SugarAPI->login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + $Account = $SugarAPI->module('Accounts')->set("name","Test")->set("phone_office","555-555-5555"); + echo "Creating Account: "; + pre($Account->asArray()); + $Account->save(); + pre("Saved Account ID: {$Account['id']}"); + $Account->set('employees','100'); + $Account['shipping_address_city'] = 'Indianapolis'; + echo "Changing fields employees and shipping address...
"; + $Account->save(); + echo "Account Updated: "; + pre($Account->asArray()); + $Account2 = $SugarAPI->module('Accounts',$Account['id']); + $Account2->retrieve(); + echo "Retrieving the Account Again...
"; + pre($Account2->asArray()); + $Account2->delete(); + echo "Account Deleted. Response: "; + pre($Account2->getResponse()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); + pre($ex->getTraceAsString()); } diff --git a/examples/DuplicateCheck.php b/examples/DuplicateCheck.php new file mode 100644 index 0000000..858d746 --- /dev/null +++ b/examples/DuplicateCheck.php @@ -0,0 +1,30 @@ +login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + $Account = $SugarAPI->module('Accounts')->set("name","DuplicateCheck Test"); + $Account->save(); + pre("Account Created: {$Account['id']}"); + $a = $Account->asArray(); + unset($a['id']); + echo "Running duplicateCheck for Account: "; + pre($a); + $Account->duplicateCheck(); + pre($Account->getResponse()->getBody()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } +}catch (Exception $ex){ + echo "Error Occurred: "; + pre($ex->getMessage()); +} \ No newline at end of file diff --git a/examples/Favorite.php b/examples/Favorite.php index 3a12bef..2a254e1 100644 --- a/examples/Favorite.php +++ b/examples/Favorite.php @@ -1,22 +1,27 @@ login(); - echo "Logged In:
";
-    print_r($SugarAPI->getAuth()->getToken()->access_token);
-    echo "
"; - $Account = $SugarAPI->module('Accounts')->set("name","Favorite Test"); - $Account->save(); - echo "
 Account Created: {$Account['id']}

"; - $Account->favorite(); - echo "
 Account added to Favorites: 
".print_r($Account,true)."
"; + if ($SugarAPI->login()){ + echo "Logged In:
";
+        print_r($SugarAPI->getAuth()->getToken()->access_token);
+        echo "
"; + $Account = $SugarAPI->module('Accounts')->set("name","Favorite Test"); + $Account->save(); + echo "
 Account Created: {$Account['id']}

"; + $Account->favorite(); + echo "
 Account added to Favorites: 
".print_r($Account,true)."
"; + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); } \ No newline at end of file diff --git a/examples/FileManipulation.php b/examples/FileManipulation.php index 8155576..7eb6b5f 100644 --- a/examples/FileManipulation.php +++ b/examples/FileManipulation.php @@ -6,34 +6,39 @@ if (file_exists($file) && is_readable($file)) { $SugarAPI = new \Sugarcrm\REST\Client\Sugar7API($server, $credentials); try { - $SugarAPI->login(); - echo "
";
-        print_r($SugarAPI->getAuth()->getToken()->access_token);
-        echo "
"; - $Note = $SugarAPI->module('Notes')->set("name", "Test"); - echo "
 Note:" . print_r($Note->asArray(), true) . "

"; - $Note->save(); - echo "
 Saved Note ID: {$Note['id']}

"; - echo "Attempting to attach $file"; - $Note->attachFile('filename', $file,TRUE,'text/plain','testtest.txt'); - $response = $Note->getResponse()->getBody(); - //echo "
" . print_r($Note->getRequest(), true) . "
"; - echo "File uploaded:
" . print_r($response,true) . "
"; + if ($SugarAPI->login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + $Note = $SugarAPI->module('Notes')->set("name", "Test"); + echo "Creating Note: "; + pre($Note->asArray()); + $Note->save(); + echo "Saved Note ID: {$Note['id']}
"; + echo "Attempting to attach $file..."; + $Note->attachFile('filename', $file,TRUE,'text/plain','testtest.txt'); + $response = $Note->getResponse()->getBody(); + //echo "
" . print_r($Note->getRequest(), true) . "
"; + echo "File uploaded: "; + pre($response); - $Note = $SugarAPI->module('Notes'); - $Note->tempFile('filename',$file); - $response = $Note->getResponse()->getBody(); - echo "
" . print_r($Note->getRequest(), true) . "
"; - echo "File uploaded:
" . print_r($response,true) . "
"; - $Note->set('name','This is a test'); - $Note->save(); - echo "
 Note ID: {$Note['id']}

"; - echo "
" . print_r($Note->getRequest(), true) . "
"; + $Note = $SugarAPI->module('Notes'); + echo "Uploading temp file for new note..."; + $Note->tempFile('filename',$file); + $response = $Note->getResponse()->getBody(); + pre($Note->getRequest()); + echo "File uploaded: "; + pre($response); + $Note->set('name','This is a test'); + $Note->save(); + echo "Note ID: {$Note['id']}
"; + pre($Note->getRequest()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } } catch (Exception $ex) { - echo "
";
-        //print_r($SugarAPI);
-        echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); } } else { if (!file_exists($file)){ diff --git a/examples/FilterAPI.php b/examples/FilterAPI.php index c0c7e8e..70d1f89 100644 --- a/examples/FilterAPI.php +++ b/examples/FilterAPI.php @@ -4,24 +4,50 @@ $SugarAPI = new \Sugarcrm\REST\Client\Sugar7API($server,$credentials); try{ - $SugarAPI->login(); - echo "
";
-    print_r($SugarAPI->getAuth()->getToken()->access_token);
-    echo "
"; - $Accounts = $SugarAPI->list('Accounts'); - $Accounts->filter()->and() - ->or() - ->starts('name','s') - ->contains('name','test') - ->endOr() - ->equals('assigned_user_id','seed_max_id') - ->endAnd(); - echo "
 Filter Accounts that are assigned to User Max, and that either start with an S or contain 'test' in the name: ".print_r($Accounts->filter()->compile(),true)."

"; - $Accounts->filter()->execute(); - echo "
 Response:".print_r($Accounts->getResponse()->getBody(),true)."

"; + if ($SugarAPI->login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + $Accounts = $SugarAPI->list('Accounts'); + $Accounts->filter()->and() + ->or() + ->starts('name','s') + ->contains('name','test') + ->endOr() + ->equals('assigned_user_id','seed_max_id') + ->endAnd(); + echo "Filtering Accounts that are assigned to User Max, and that either start with an S or contain 'test' in the name: "; + pre($Accounts->filter()->compile()); + $Accounts->count(); + echo "Running Count Request: "; + pre($Accounts->getResponse()->getBody()); + echo "Running Filter Request: "; + $Accounts->filter()->execute(); + echo "Request: "; + pre($Accounts->getRequest()); + echo "Accounts: "; + pre($Accounts->asArray()); + $Accounts->clear(); + $Accounts->filter(true); + echo "Filtering Accounts that are created between dates, or in the last 7 days: "; + $Accounts->filter()->or()->date('date_entered') + ->between(array("2019-01-01","2019-02-01")) + ->endDate() + ->date('date_entered') + ->last7Days() + ->endDate() + ->endOr(); + pre($Accounts->filter()->compile()); + $Accounts->filter()->execute(); + echo "Request: "; + pre($Accounts->getRequest()); + echo "Accounts: "; + pre($Accounts->asArray()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); + pre($ex->getTraceAsString()); } \ No newline at end of file diff --git a/examples/FilterRelated.php b/examples/FilterRelated.php index 15b46f7..d72754d 100644 --- a/examples/FilterRelated.php +++ b/examples/FilterRelated.php @@ -1,29 +1,31 @@ login(); - echo "
";
-    print_r($SugarAPI->getAuth()->getToken()->access_token);
-    echo "
"; - $Accounts = $SugarAPI->list('Accounts'); - $Accounts->fetch(); - $Account = $Accounts->at(1); - $Account->getRelated('contacts',true); - echo $Account->getRequest()->getURL()."
"; - echo "
 Response:".print_r($Account->getResponse()->getBody(),true)."

"; - $Filter = $Account->filterRelated('contacts')->contains('first_name','s'); - echo "
 Filter Contacts related to Account {$account['id']} where first_name contains an 's': ".print_r($Filter->compile(),true)."

"; - $Filter->execute(); - echo "
 Response:".print_r($Account->getResponse()->getBody(),true)."

"; + if ($SugarAPI->login()){ + echo "
";
+        print_r($SugarAPI->getAuth()->getToken()->access_token);
+        echo "
"; + $Accounts = $SugarAPI->list('Accounts'); + $Accounts->fetch(); + $Account = $Accounts->at(1); + $Account->getRelated('contacts',true); + echo $Account->getRequest()->getURL()."
"; + echo "
 Response:".print_r($Account->getResponse()->getBody(),true)."

"; + $Filter = $Account->filterRelated('contacts')->contains('first_name','s'); + echo "
 Filter Contacts related to Account {$account['id']} where first_name contains an 's': ".print_r($Filter->compile(),true)."

"; + $Filter->execute(); + echo "
 Response:".print_r($Account->getResponse()->getBody(),true)."

"; + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); } \ No newline at end of file diff --git a/examples/ModuleEnum.php b/examples/ModuleEnum.php index f440c7b..500fdfa 100644 --- a/examples/ModuleEnum.php +++ b/examples/ModuleEnum.php @@ -1,22 +1,25 @@ login(); - echo "Logged In:
";
-    echo $SugarAPI->getAuth()->getToken()->access_token;
-    echo "
"; - $Enum = $SugarAPI->enum('Accounts','account_type'); - $response = $Enum->execute()->getResponse(); - echo "
 Account Type options: 
".print_r($response->getBody(),true)."
"; + if ($SugarAPI->login()){ + echo "Logged In:"; + pre($SugarAPI->getAuth()->getToken()); + $Enum = $SugarAPI->enum('Accounts','account_type'); + $response = $Enum->execute()->getResponse(); + echo "Account Type options: "; + pre($response->getBody()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); + pre($ex->getTraceAsString()); } \ No newline at end of file diff --git a/examples/RelateRecords.php b/examples/RelateRecords.php index 4bd1343..dd27dbb 100644 --- a/examples/RelateRecords.php +++ b/examples/RelateRecords.php @@ -4,23 +4,35 @@ $SugarAPI = new \Sugarcrm\REST\Client\Sugar7API($server,$credentials); try{ - $SugarAPI->login(); - echo "
";
-    print_r($SugarAPI->getAuth()->getToken()->access_token);
-    echo "
"; - $Account = $SugarAPI->module('Accounts')->set("name","Relate Records Test"); - $Account->save(); - echo "
 Saved Account ID: {$Account['id']}

"; - $Opportunity = $SugarAPI->module('Opportunities'); - $Opportunity['name'] = 'Test Opportunity'; - $Opportunity['description'] = 'This opportunity was created via the SugarCRM REST API Client v2 to test creating relationships.'; - $Opportunity->save(); - echo "
 Opportunity Created: 
".print_r($Opportunity->asArray(),true)."
"; - $Account->relate('opportunities',$Opportunity['id']); - echo "
 Relationship Created:".print_r($Account->getResponse(),true)."

"; + if ($SugarAPI->login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + $Account = $SugarAPI->module('Accounts')->set("name","Relate Records Test"); + echo "Creating Account: "; + pre($Account->asArray()); + $Account->save(); + pre("Saved Account ID: {$Account['id']}"); + $Opportunity = $SugarAPI->module('Opportunities'); + $Opportunity['name'] = 'Test Opportunity'; + $Opportunity['description'] = 'This opportunity was created via the SugarCRM REST API Client v2 to test creating relationships.'; + echo "Creating Opportunity: "; + pre($Account->asArray()); + $Opportunity->save(); + pre("Saved Opportunity ID: {$Opportunity['id']}"); + + echo "Relating Opportunity to Account: "; + $Account->relate('opportunities',$Opportunity['id']); + echo "Request: "; + pre($Account->getRequest()->getBody()); + echo "Response: "; + pre($Account->getResponse()->getBody()); + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); + } + }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); + pre($ex->getTraceAsString()); } \ No newline at end of file diff --git a/examples/Sudo.php b/examples/Sudo.php index dda52f7..3e8fef6 100644 --- a/examples/Sudo.php +++ b/examples/Sudo.php @@ -4,16 +4,23 @@ $SugarAPI = new \Sugarcrm\REST\Client\Sugar7API($server,$credentials); try{ - $SugarAPI->login(); - echo "Logged In:
";
-    print_r($SugarAPI->getAuth()->getToken());
-    echo "
"; - if ($SugarAPI->sudo('will')){ - echo "Sudo'd to will:".print_r($SugarAPI->getAuth()->getToken(),true); + if ($SugarAPI->login()){ + echo "Logged In: "; + pre($SugarAPI->getAuth()->getToken()); + if ($SugarAPI->sudo('will')){ + echo "Sudo'd to will:
"; + echo "Token:"; + pre($SugarAPI->getAuth()->getToken()); + echo "User:"; + $Me = $SugarAPI->me(); + pre($Me->asArray()); + } + } else { + echo "Could not login."; + pre($SugarAPI->getAuth()->getActionEndpoint('authenticate')->getResponse()); } }catch (Exception $ex){ - echo "
";
-    //print_r($SugarAPI);
-    echo "
"; - print $ex->getMessage(); + echo "Error Occurred: "; + pre($ex->getMessage()); + pre($ex->getTraceAsString()); } \ No newline at end of file diff --git a/examples/include.php b/examples/include.php index 8456dd2..f75cdc1 100644 --- a/examples/include.php +++ b/examples/include.php @@ -1,9 +1,22 @@ 'admin', 'password' => 'asdf', -); \ No newline at end of file + 'platform' => 'base' +); + +function pre($message) +{ + $msg = $message; + if (!is_string($message)){ + $msg = print_r($message,true); + } + echo "
$msg

"; +} \ No newline at end of file diff --git a/src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php b/src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php index e6308a9..dc5dc14 100644 --- a/src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php +++ b/src/Endpoint/Abstracts/AbstractSugarBeanEndpoint.php @@ -61,6 +61,8 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE const BEAN_ACTION_TEMP_FILE_UPLOAD = 'tempFile'; + const BEAN_ACTION_DUPLICATE_CHECK = 'duplicateCheck'; + const BEAN_ACTION_ARG1_VAR = 'actionArg1'; const BEAN_ACTION_ARG2_VAR = 'actionArg2'; @@ -104,6 +106,7 @@ abstract class AbstractSugarBeanEndpoint extends ModelEndpoint implements SugarE self::BEAN_ACTION_DOWNLOAD_FILE => JSON::HTTP_GET, self::BEAN_ACTION_ATTACH_FILE => JSON::HTTP_POST, self::BEAN_ACTION_TEMP_FILE_UPLOAD => JSON::HTTP_POST, + self::BEAN_ACTION_DUPLICATE_CHECK => JSON::HTTP_POST ); /** @@ -247,6 +250,7 @@ protected function configureURL(array $options) { case self::MODEL_ACTION_UPDATE: case self::MODEL_ACTION_CREATE: case self::MODEL_ACTION_RETRIEVE: + case self::BEAN_ACTION_DUPLICATE_CHECK: $action = NULL; break; } @@ -271,6 +275,9 @@ protected function configureAction($action,array $arguments = array()) { if (isset($options[self::BEAN_ACTION_ARG3_VAR])) unset($options[self::BEAN_ACTION_ARG3_VAR]); if (!empty($arguments)){ switch($action){ + case self::BEAN_ACTION_DUPLICATE_CHECK: + $options[self::MODEL_ID_VAR] = $action; + break; case self::BEAN_ACTION_TEMP_FILE_UPLOAD: case self::BEAN_ACTION_ATTACH_FILE: $this->upload = TRUE; @@ -460,6 +467,23 @@ public function tempFile($fileField,$filePath,$deleteOnFail = false,$mimeType = return $this->execute(); } + /** + * @return $this + * @throws \MRussell\REST\Exception\Endpoint\InvalidRequest + */ + public function duplicateCheck() + { + $action = self::BEAN_ACTION_DUPLICATE_CHECK; + $this->setCurrentAction($action); + $idKey = $this->modelIdKey(); + $id = $this[$idKey]; + $this[$idKey] = $action; + $this->setData($this->asArray()); + $this->execute(); + $this[$idKey] = $id; + return $this; + } + /** * @param bool $deleteOnFail * @throws \MRussell\REST\Exception\Endpoint\InvalidDataType diff --git a/src/Endpoint/Data/Filters/Expression/AbstractExpression.php b/src/Endpoint/Data/Filters/Expression/AbstractExpression.php index 3221c4f..e82dbbf 100644 --- a/src/Endpoint/Data/Filters/Expression/AbstractExpression.php +++ b/src/Endpoint/Data/Filters/Expression/AbstractExpression.php @@ -99,7 +99,7 @@ public function __call($name, $arguments) } if (array_key_exists($name,$this->expressions)){ $Expression = $this->expressions[$name]; - $Exp = new $Expression(); + $Exp = new $Expression($arguments); $Exp->setParentExpression($this); $this->filters[] = $Exp; return $Exp; diff --git a/src/Endpoint/Data/Filters/Expression/DateExpression.php b/src/Endpoint/Data/Filters/Expression/DateExpression.php index e3da1f5..51e423b 100644 --- a/src/Endpoint/Data/Filters/Expression/DateExpression.php +++ b/src/Endpoint/Data/Filters/Expression/DateExpression.php @@ -122,19 +122,27 @@ public function __call($name, $arguments) $range = $this->ranges[$name]; $args[] = $range; $Op = new DateRange($args); - $this->filters[] = $Op; + $this->filters[0] = $Op; return $this; } if (array_key_exists($name,$this->operators)){ $args = array_merge($args,$arguments); $Operator = $this->operators[$name]; $O = new $Operator($args); - $this->filters[] = $O; + $this->filters[0] = $O; return $this; } throw new UnknownFilterOperator(array($name)); } + public function compile() + { + if (isset($this->filters[0])){ + return $this->filters[0]->compile(); + } + return array(); + } + /** * Human Friendly Expression End, allow you to traverse back up the Filter expression * @return AbstractExpression diff --git a/src/Endpoint/Data/Filters/Operator/DateRange.php b/src/Endpoint/Data/Filters/Operator/DateRange.php index 2ca3d8e..3ab17ed 100644 --- a/src/Endpoint/Data/Filters/Operator/DateRange.php +++ b/src/Endpoint/Data/Filters/Operator/DateRange.php @@ -8,7 +8,7 @@ class DateRange extends AbstractOperator { - const OPERATOR = '$dateBetween'; + const OPERATOR = '$dateRange'; protected static $_OPERATOR = self::OPERATOR; diff --git a/src/Endpoint/ModuleFilter.php b/src/Endpoint/ModuleFilter.php index c5d1c07..f656d57 100644 --- a/src/Endpoint/ModuleFilter.php +++ b/src/Endpoint/ModuleFilter.php @@ -109,7 +109,9 @@ public function filter($reset = FALSE) public function count() { $this->setOptions(array($this->getModule(),self::COUNT_OPTION)); - return $this->execute(); + $this->execute(); + $this->setOptions(array($this->getModule())); + return $this; } } \ No newline at end of file From 873ff5e3f1f8a5c0443c979fccfc33f2907f8b29 Mon Sep 17 00:00:00 2001 From: Michael Russell Date: Sat, 13 Jul 2019 16:01:11 -0400 Subject: [PATCH 3/3] Remove SugarFuel Server From Examples --- examples/include.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/include.php b/examples/include.php index f75cdc1..3b1b6a1 100644 --- a/examples/include.php +++ b/examples/include.php @@ -5,7 +5,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; -$server = 'http://ent.900.sugarfuel.vm'; +$server = 'localhost'; $credentials = array( 'username' => 'admin', 'password' => 'asdf',