CRM-21052 - Activity.create API - Abide by setting `civicaseActivityRevisions`
[civicrm-core.git] / api / v3 / Cxn.php
index af39133707fafa59531660913a65021790e15247..3c822a8c58dd00d95ab01fd0094733c2a20ac98a 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2015                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -222,13 +222,14 @@ function _civicrm_api3_cxn_getlink_spec(&$spec) {
   $daoFields = CRM_Cxn_DAO_Cxn::fields();
   $spec['app_guid'] = $daoFields['app_guid'];
   $spec['cxn_guid'] = $daoFields['cxn_guid'];
-  $spec['page'] = array(
-    'name' => 'page',
+  $spec['page_name'] = array(
+    'name' => 'page_name',
     'type' => CRM_Utils_Type::T_STRING,
     'title' => ts('Page Type'),
     'description' => 'The type of page (eg "settings")',
     'maxlength' => 63,
     'size' => CRM_Utils_Type::HUGE,
+    'api.aliases' => array('page'),
   );
 }
 
@@ -245,14 +246,14 @@ function civicrm_api3_cxn_getlink($params) {
   $cxnId = _civicrm_api3_cxn_parseCxnId($params);
   $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
 
-  if (empty($params['page']) || !is_string($params['page'])) {
+  if (empty($params['page_name']) || !is_string($params['page_name'])) {
     throw new API_Exception("Invalid page");
   }
 
   /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
   $client = \Civi::service('cxn_reg_client');
   return $client->call($appMeta, 'Cxn', 'getlink', array(
-    'page' => $params['page'],
+    'page' => $params['page_name'],
   ));
 }
 
@@ -270,17 +271,53 @@ function civicrm_api3_cxn_getcfg($params) {
     'siteCallbackUrl' => CRM_Cxn_BAO_Cxn::getSiteCallbackUrl(),
   );
   return civicrm_api3_create_success($result);
+}
 
-  $cxnId = _civicrm_api3_cxn_parseCxnId($params);
-  $appMeta = CRM_Cxn_BAO_Cxn::getAppMeta($cxnId);
+/**
+ * Creates or modifies a Cxn row.
+ *
+ * @param array $params
+ *   Array with keys:
+ *   - id, cxn_guid OR app_guid: string.
+ *   - is_active: boolean.
+ *   - options: JSON
+ * @return page
+ * @throws Exception
+ */
+function civicrm_api3_cxn_create($params) {
+  $result = "";
 
-  if (empty($params['page']) || !is_string($params['page'])) {
-    throw new API_Exception("Invalid page");
-  }
+  try {
+    // get the ID
+    if (!empty($params['id'])) {
+      $cxnId = $params['id'];
+    }
+    else {
+      $cxnId = _civicrm_api3_cxn_parseCxnId($params);
+    }
 
-  /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
-  $client = \Civi\Core\Container::singleton()->get('cxn_reg_client');
-  return $client->call($appMeta, 'Cxn', 'getlink', array(
-    'page' => $params['page'],
-  ));
+    // see if it's sth to update
+    if (isset($params['options']) || isset($params['is_active'])) {
+
+      $dao = new CRM_Cxn_DAO_Cxn();
+      $dao->id = $cxnId;
+
+      if ($dao->find()) {
+        if (isset($params['is_active'])) {
+          $dao->is_active = (int) $params['is_active'];
+        }
+        if (isset($params['options'])) {
+          $dao->options = $params['options'];
+        }
+
+        $result = $dao->save();
+      }
+
+    }
+    return civicrm_api3_create_success($result, $params, 'Cxn', 'create');
+
+  }
+  catch(Exception $ex){
+    throw $ex;
+  }
 }