/**
  * Get published record for this ds ; AJAX data for edit data_source settings primary links
  *
  * @author Liz Woods 
  * @param  [int] 	$data_source_id
  * @param  [string] $key
  * @return [json]   
  */
 public function get_datasource_object()
 {
     $data_source_id = $this->input->post('data_source_id');
     $key = $this->input->post('key');
     //administrative and loading stuffs
     acl_enforce('REGISTRY_USER');
     ds_acl_enforce($data_source_id);
     $jsonData['status'] = "OK";
     $jsonData['message'] = '';
     header('Cache-Control: no-cache, must-revalidate');
     header('Content-type: application/json');
     $this->load->model('data_source/data_sources', 'ds');
     $this->load->model('registry_object/registry_objects', 'ro');
     $data_source = $this->ds->getByID($data_source_id);
     $registry_object = $this->ro->getPublishedByKey($key);
     if ($registry_object == null || $data_source->id != $data_source_id) {
         $jsonData['message'] = "You must provide a published registry object key from within this data source for primary relationship.";
     }
     echo json_encode($jsonData);
 }
 function update($all = false)
 {
     set_exception_handler('json_exception_handler');
     header('Cache-Control: no-cache, must-revalidate');
     header('Content-type: application/json');
     $jsonData = array();
     $jsondata['status'] = 'success';
     $jsondata['error_message'] = '<ul class="error_mesage">';
     $jsondata['success_message'] = '<ul class="success_mesage">';
     $jsondata['success_count'] = 0;
     $jsondata['error_count'] = 0;
     $this->load->model('registry_objects', 'ro');
     $this->load->model('data_source/data_sources', 'ds');
     $data_source_id = $this->input->post('data_source_id');
     $ds = $this->ds->getByID($data_source_id);
     if (!$ds) {
         throw new Exception("Invalid Data Source ID specified");
     }
     ds_acl_enforce($ds->id);
     $attributes = $this->input->post('attributes');
     if (!$all) {
         $affected_ids = $this->input->post('affected_ids');
         $attributes = $this->input->post('attributes');
     } else {
         /* SELECT ALL-style update -- must use the filters to determine what's on-screen */
         $select_all = $this->input->post('select_all');
         $excluded_records = $this->input->post('excluded_records') ?: array();
         $filters = $this->input->post('filters');
         $args = array();
         $args['sort'] = isset($filters['sort']) ? $filters['sort'] : array('updated' => 'desc');
         $args['search'] = isset($filters['search']) ? $filters['search'] : false;
         $args['or_filter'] = isset($filters['or_filter']) ? $filters['or_filter'] : false;
         $args['filter'] = isset($filters['filter']) ? array_merge($filters['filter'], array('status' => $this->input->post('select_all'))) : array('status' => $this->input->post('select_all'));
         $args['data_source_id'] = $data_source_id;
         $registryObjects = $this->ro->filter_by($args, 0, 0, true);
         $affected_ids = array();
         foreach ($registryObjects as $ro) {
             if (!in_array($ro->registry_object_id, $excluded_records)) {
                 array_push($affected_ids, $ro->registry_object_id);
             }
         }
     }
     $sentMail = false;
     foreach ($affected_ids as $id) {
         $ro = $this->ro->getByID($id);
         foreach ($attributes as $a) {
             if ($a['name'] == 'status' && $ro->status == 'DRAFT' && $ro->error_count > 0) {
                 $jsondata['error_count']++;
                 $jsondata['error_message'] .= "<li>Registry Object contains error(s): " . $ro->title . "</li>";
             } elseif ($a['name'] == 'status' && ($a['value'] == 'APPROVED' || $a['value'] == 'PUBLISHED') && $ro->error_count > 0) {
                 $jsondata['error_count']++;
                 $jsondata['error_message'] .= "<li>Registry Object contains error(s): " . $ro->title . "</li>";
             } else {
                 try {
                     $ro->setAttribute($a['name'], $a['value']);
                     if ($a['name'] == 'gold_status_flag' && $a['value'] == 't') {
                         $ro->setAttribute('quality_level', 4);
                     }
                     if ($a['name'] == 'gold_status_flag' && $a['value'] == 'f') {
                         $ro->update_quality_metadata();
                     }
                     if ($a['name'] == 'status') {
                         $ro->flag = 'f';
                     }
                     $old_ro_id = $ro->id;
                     if ($ro->save()) {
                         // ID may have changed if a DRAFT record overwrites a PUBLISHED one
                         if ($ro->id != $old_ro_id) {
                             // This will be used to redirect from the registry view page
                             $jsondata['new_ro_id'] = $ro->id;
                         }
                         if ($a['name'] == 'status') {
                             // Message Code for single-record status updates (from ARO screen)
                             $jsondata['message_code'] = $a['value'];
                             if ($a['value'] == 'SUBMITTED_FOR_ASSESSMENT') {
                                 if ($ds->count_SUBMITTED_FOR_ASSESSMENT == 0 && !$sentMail) {
                                     // If there is a notification email set, send a mail...
                                     if ($ds->assessment_notify_email_addr) {
                                         $this->ro->emailAssessor($ds);
                                         $jsondata['message_code'] = 'SUBMITTED_FOR_ASSESSMENT_EMAIL_SENT';
                                         $jsondata['success_message'] .= '<strong>Note:</strong> An ANDS Quality Assessor has been notified of your submitted record(s).</li>';
                                         $sentMail = true;
                                     } else {
                                         // Otherwise prompt to contact the CLO
                                         $jsondata['success_message'] .= '<strong>Note:</strong> You should contact your ANDS Client Liaison Officer to let them know your records are ready for assessment.</li>';
                                         $sentMail = true;
                                     }
                                 } elseif ($ds->count_SUBMITTED_FOR_ASSESSMENT > 0 && !$sentMail) {
                                     $jsondata['success_message'] .= '<strong>Note:</strong> You should contact your ANDS Client Liaison Officer to let them know your records are ready for assessment.</li>';
                                     $sentMail = true;
                                 }
                             }
                         }
                         $jsondata['success_count']++;
                         //$jsondata['success_message'] .= '<li>Updated '.$ro->title.' set '.$a['name'].' to value:'.$a['value']."</li>";
                     } else {
                         $jsondata['error_count']++;
                         $jsondata['error_message'] .= '<li>Failed to update ' . $ro->title . ' set ' . $a['name'] . ' to value:' . $a['value'] . "</li>";
                         $jsondata['status'] = 'error';
                     }
                 } catch (Exception $e) {
                     $jsondata['status'] = 'error';
                     $jsondata['error_count']++;
                     $jsondata['error_message'] .= "<li>" . $e->getMessage() . "</li>";
                 }
             }
         }
     }
     $ds->updateStats();
     $jsondata['error_message'] .= '</ul>';
     $jsondata['success_message'] .= '</ul>';
     echo json_encode($jsondata);
 }