Exemplo n.º 1
0
 /** 
  * Returns loan_id for the loan with the specified name, regardless of specified type. If the loan does not already 
  * exist then it will be created with the specified name, type and locale, as well as with any specified values in the $pa_values array.
  * $pa_values keys should be either valid loan fields or attributes.
  *
  * @param string $ps_loan_name Loan label name
  * @param int $pn_type_id The type_id of the loan type to use if the loan needs to be created
  * @param int $pn_locale_id The locale_id to use if the loan needs to be created (will be used for both the loan locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created loan records with. These values are *only* used for newly created loans; they will not be applied if the loan named already exists. The array keys should be names of loan fields or valid loan attributes. Values should be either a scalar (for single-value attributes) or an array of values for (multi-valued attributes)
  * @param array $pa_options An optional array of options, which include:
  *				outputErrors - if true, errors will be printed to console [default=false]
  *				matchOnIdno - try to match on idno if name match fails [default=false]
  *				dontCreate - if true then new loans will not be created [default=false]
  * 				transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *				returnInstance = return ca_loans instance rather than loan_id. Default is false. 
  *				generateIdnoWithTemplate = A template to use when setting the idno. The template is a value with automatically-set SERIAL values replaced with % characters. Eg. 2012.% will set the created row's idno value to 2012.121 (assuming that 121 is the next number in the serial sequence.) The template is NOT used if idno is passed explicitly as a value in $pa_values.
  *				importEvent = if ca_data_import_events instance is passed then the insert/update of the loan will be logged as part of the import
  *				importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *				log = if KLogger instance is passed then actions will be logged
  */
 static function getLoanID($ps_loan_name, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pb_match_on_idno = caGetOption('matchOnIdno', $pa_options, false);
     $t_loan = new ca_loans();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_loan->setTransaction($pa_options['transaction']);
     }
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     $va_find_arr = array();
     if ($pn_type_id) {
         $va_find_arr['type_id'] = $pn_type_id;
     }
     if ($pn_parent_id) {
         $va_find_arr['parent_id'] = $pn_parent_id;
     }
     if (!($vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null)) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_loan->setIdnoTWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     if (!($vn_id = ca_loans::find(array_merge(array('preferred_labels' => array('name' => $ps_loan_name)), $va_find_arr), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction'])))) {
         if ($pb_match_on_idno && $vs_idno) {
             $va_find_arr['idno'] = $vs_idno;
             $vn_id = ca_loans::find($va_find_arr, array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_loans', 'I');
         }
         $t_loan->setMode(ACCESS_WRITE);
         $t_loan->set('locale_id', $pn_locale_id);
         $t_loan->set('type_id', $pn_type_id);
         $t_loan->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_loan->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_loan->set('idno', $vs_idno);
         $t_loan->set('parent_id', isset($pa_values['parent_id']) ? $pa_values['parent_id'] : null);
         $t_loan->insert();
         if ($t_loan->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_loan->addLabel(array('name' => $ps_loan_name), $pn_locale_id, null, true);
         if ($t_loan->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
             }
             $vb_label_errors = true;
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         $vb_attr_errors = false;
         if (is_array($pa_values)) {
             foreach ($pa_values as $vs_element => $va_value) {
                 if (is_array($va_value)) {
                     // array of values (complex multi-valued attribute)
                     $t_loan->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                 } else {
                     // scalar value (simple single value attribute)
                     if ($va_value) {
                         $t_loan->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                     }
                 }
             }
         }
         $t_loan->update();
         if ($t_loan->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set values for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set values for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
             }
             $vb_attr_errors = true;
         }
         $vn_loan_id = $t_loan->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_loan_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_loan->getErrors())));
             } else {
                 $o_event->endItem($vn_loan_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new loan %1", $ps_loan_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_loan;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_loans', 'U');
         }
         $vn_loan_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_loan_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing loan %1 in DataMigrationUtils::getLoanID(); total of %2 loans were found", $ps_loan_name, sizeof($va_loan_ids) + 1));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return new ca_loans($vn_load_id);
         }
     }
     return $vn_loan_id;
 }
Exemplo n.º 2
0
 /**
  * Return array with list of significant events in object life cycle as configured for 
  * a ca_objects_history editor bundle.
  *
  * @param array $pa_bundle_settings The settings for a ca_objects_history editing BUNDLES
  * @param array $pa_options Array of options. Options include:
  *		noCache = Don't use any cached history data. [Default is false]
  *		currentOnly = Only return history entries dates before or on the current date. [Default is false]
  *		limit = Only return a maximum number of history entries. [Default is null; no limit]
  *
  * @return array A list of life cycle events, indexed by historic timestamp for date of occurrrence. Each list value is an array of history entries.
  *
  * @used-by ca_objects::getObjectHistoryHTMLFormBundle
  */
 public function getObjectHistory($pa_bundle_settings = null, $pa_options = null)
 {
     global $g_ui_locale;
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!is_array($pa_bundle_settings)) {
         $pa_bundle_settings = array();
     }
     $vs_cache_key = caMakeCacheKeyFromOptions(array_merge($pa_bundle_settings, $pa_options, array('object_id' => $this->getPrimaryKey())));
     $pb_no_cache = caGetOption('noCache', $pa_options, false);
     if (!$pb_no_cache && isset(ca_objects::$s_object_use_cache[$vs_cache_key])) {
         return ca_objects::$s_object_use_cache[$vs_cache_key];
     }
     $pb_display_label_only = caGetOption('displayLabelOnly', $pa_options, false);
     $pb_get_current_only = caGetOption('currentOnly', $pa_options, false);
     $pn_limit = caGetOption('limit', $pa_options, null);
     $vs_display_template = caGetOption('display_template', $pa_bundle_settings, _t('No template defined'));
     $vs_history_template = caGetOption('history_template', $pa_bundle_settings, $vs_display_template);
     $vn_current_date = caDateToHistoricTimestamp(_t('now'));
     $o_media_coder = new MediaInfoCoder();
     //
     // Get history
     //
     $va_history = array();
     // Lots
     if (is_array($va_lot_types = caGetOption('ca_object_lots_showTypes', $pa_bundle_settings, null)) && ($vn_lot_id = $this->get('lot_id'))) {
         $t_lot = new ca_object_lots($vn_lot_id);
         if (!$t_lot->get('deleted')) {
             $va_lot_type_info = $t_lot->getTypeList();
             $vn_type_id = $t_lot->get('type_id');
             $vs_color = $va_lot_type_info[$vn_type_id]['color'];
             if (!$vs_color || $vs_color == '000000') {
                 $vs_color = caGetOption("ca_object_lots_{$va_lot_type_info[$vn_type_id]['idno']}_color", $pa_bundle_settings, 'ffffff');
             }
             $va_dates = array();
             $va_date_elements = caGetOption("ca_object_lots_{$va_lot_type_info[$vn_type_id]['idno']}_dateElement", $pa_bundle_settings, null);
             if (!is_array($va_date_elements) && $va_date_elements) {
                 $va_date_elements = array($va_date_elements);
             }
             if (is_array($va_date_elements) && sizeof($va_date_elements)) {
                 foreach ($va_date_elements as $vs_date_element) {
                     $va_dates[] = array('sortable' => $t_lot->get($vs_date_element, array('getDirectDate' => true)), 'display' => $t_lot->get($vs_date_element));
                 }
             }
             if (!sizeof($va_dates)) {
                 $va_dates[] = array('sortable' => $vn_date = caUnixTimestampToHistoricTimestamps($t_lot->getCreationTimestamp(null, array('timestampOnly' => true))), 'display' => caGetLocalizedDate($vn_date));
             }
             foreach ($va_dates as $va_date) {
                 if (!$va_date['sortable']) {
                     continue;
                 }
                 if (!in_array($vn_type_id, $va_lot_types)) {
                     continue;
                 }
                 if ($pb_get_current_only && $va_date['sortable'] > $vn_current_date) {
                     continue;
                 }
                 $vs_default_display_template = '^ca_object_lots.preferred_labels.name (^ca_object_lots.idno_stub)';
                 $vs_display_template = $pb_display_label_only ? "" : caGetOption("ca_object_lots_{$va_lot_type_info[$vn_type_id]['idno']}_displayTemplate", $pa_bundle_settings, $vs_default_display_template);
                 $va_history[$va_date['sortable']][] = array('type' => 'ca_object_lots', 'id' => $vn_lot_id, 'display' => $t_lot->getWithTemplate($vs_display_template), 'color' => $vs_color, 'icon_url' => $vs_icon_url = $o_media_coder->getMediaTag($va_lot_type_info[$vn_type_id]['icon'], 'icon'), 'typename_singular' => $vs_typename = $va_lot_type_info[$vn_type_id]['name_singular'], 'typename_plural' => $va_lot_type_info[$vn_type_id]['name_plural'], 'type_id' => $vn_type_id, 'icon' => '<div class="caUseHistoryIconContainer" style="background-color: #' . $vs_color . '"><div class="caUseHistoryIcon">' . ($vs_icon_url ? $vs_icon_url : '<div class="caUseHistoryIconText">' . $vs_typename . '</div>') . '</div></div>', 'date' => $va_date['display']);
             }
         }
     }
     // Loans
     $va_loans = $this->get('ca_loans.loan_id', array('returnAsArray' => true));
     if (is_array($va_loan_types = caGetOption('ca_loans_showTypes', $pa_bundle_settings, null)) && is_array($va_loans) && sizeof($va_loans)) {
         $qr_loans = caMakeSearchResult('ca_loans', $va_loans);
         $t_loan = new ca_loans();
         $va_loan_type_info = $t_loan->getTypeList();
         $va_date_elements_by_type = array();
         foreach ($va_loan_types as $vn_type_id) {
             if (!is_array($va_date_elements = caGetOption("ca_loans_{$va_loan_type_info[$vn_type_id]['idno']}_dateElement", $pa_bundle_settings, null)) && $va_date_elements) {
                 $va_date_elements = array($va_date_elements);
             }
             if (!$va_date_elements) {
                 continue;
             }
             $va_date_elements_by_type[$vn_type_id] = $va_date_elements;
         }
         while ($qr_loans->nextHit()) {
             $vn_loan_id = $qr_loans->get('loan_id');
             if ((string) $qr_loans->get('ca_loans.deleted') !== '0') {
                 continue;
             }
             // filter out deleted
             $vn_type_id = $qr_loans->get('type_id');
             $va_dates = array();
             if (is_array($va_date_elements_by_type[$vn_type_id]) && sizeof($va_date_elements_by_type[$vn_type_id])) {
                 foreach ($va_date_elements_by_type[$vn_type_id] as $vs_date_element) {
                     $va_dates[] = array('sortable' => $qr_loans->get("ca_loans.{$vs_date_element}", array('getDirectDate' => true)), 'display' => $qr_loans->get("ca_loans.{$vs_date_element}"));
                 }
             }
             if (!sizeof($va_dates)) {
                 $va_dates[] = array('sortable' => $vn_date = caUnixTimestampToHistoricTimestamps($qr_loans->get('lastModified')), 'display' => caGetLocalizedDate($vn_date));
             }
             $vs_default_display_template = '^ca_loans.preferred_labels.name (^ca_loans.idno)';
             $vs_display_template = $pb_display_label_only ? $vs_default_display_template : caGetOption("ca_loans_{$va_loan_type_info[$vn_type_id]['idno']}_displayTemplate", $pa_bundle_settings, $vs_default_display_template);
             foreach ($va_dates as $va_date) {
                 if (!$va_date['sortable']) {
                     continue;
                 }
                 if (!in_array($vn_type_id, $va_loan_types)) {
                     continue;
                 }
                 if ($pb_get_current_only && $va_date['sortable'] > $vn_current_date) {
                     continue;
                 }
                 $vs_color = $va_loan_type_info[$vn_type_id]['color'];
                 if (!$vs_color || $vs_color == '000000') {
                     $vs_color = caGetOption("ca_loans_{$va_loan_type_info[$vn_type_id]['idno']}_color", $pa_bundle_settings, 'ffffff');
                 }
                 $va_history[$va_date['sortable']][] = array('type' => 'ca_loans', 'id' => $vn_loan_id, 'display' => $qr_loans->getWithTemplate($vs_display_template), 'color' => $vs_color, 'icon_url' => $vs_icon_url = $o_media_coder->getMediaTag($va_loan_type_info[$vn_type_id]['icon'], 'icon'), 'typename_singular' => $vs_typename = $va_loan_type_info[$vn_type_id]['name_singular'], 'typename_plural' => $va_loan_type_info[$vn_type_id]['name_plural'], 'type_id' => $vn_type_id, 'icon' => '<div class="caUseHistoryIconContainer" style="background-color: #' . $vs_color . '"><div class="caUseHistoryIcon">' . ($vs_icon_url ? $vs_icon_url : '<div class="caUseHistoryIconText">' . $vs_typename . '</div>') . '</div></div>', 'date' => $va_date['display']);
             }
         }
     }
     // Movements
     $va_movements = $this->get('ca_movements.movement_id', array('returnAsArray' => true));
     if (is_array($va_movement_types = caGetOption('ca_movements_showTypes', $pa_bundle_settings, null)) && is_array($va_movements) && sizeof($va_movements)) {
         $qr_movements = caMakeSearchResult('ca_movements', $va_movements);
         $t_movement = new ca_movements();
         $va_movement_type_info = $t_movement->getTypeList();
         $va_date_elements_by_type = array();
         foreach ($va_movement_types as $vn_type_id) {
             if (!is_array($va_date_elements = caGetOption("ca_movements_{$va_movement_type_info[$vn_type_id]['idno']}_dateElement", $pa_bundle_settings, null)) && $va_date_elements) {
                 $va_date_elements = array($va_date_elements);
             }
             if (!$va_date_elements) {
                 continue;
             }
             $va_date_elements_by_type[$vn_type_id] = $va_date_elements;
         }
         while ($qr_movements->nextHit()) {
             $vn_movement_id = $qr_movements->get('movement_id');
             if ((string) $qr_movements->get('ca_movements.deleted') !== '0') {
                 continue;
             }
             // filter out deleted
             $vn_type_id = $qr_movements->get('type_id');
             $va_dates = array();
             if (is_array($va_date_elements_by_type[$vn_type_id]) && sizeof($va_date_elements_by_type[$vn_type_id])) {
                 foreach ($va_date_elements_by_type[$vn_type_id] as $vs_date_element) {
                     $va_dates[] = array('sortable' => $qr_movements->get("ca_movements.{$vs_date_element}", array('getDirectDate' => true)), 'display' => $qr_movements->get("ca_movements.{$vs_date_element}"));
                 }
             }
             if (!sizeof($va_dates)) {
                 $va_dates[] = array('sortable' => $vn_date = caUnixTimestampToHistoricTimestamps($qr_movements->get('lastModified')), 'display' => caGetLocalizedDate($vn_date));
             }
             $vs_default_display_template = '^ca_movements.preferred_labels.name (^ca_movements.idno)';
             $vs_display_template = $pb_display_label_only ? $vs_default_display_template : caGetOption("ca_movements_{$va_movement_type_info[$vn_type_id]['idno']}_displayTemplate", $pa_bundle_settings, $vs_default_display_template);
             foreach ($va_dates as $va_date) {
                 if (!$va_date['sortable']) {
                     continue;
                 }
                 if (!in_array($vn_type_id, $va_movement_types)) {
                     continue;
                 }
                 if ($pb_get_current_only && $va_date['sortable'] > $vn_current_date) {
                     continue;
                 }
                 $vs_color = $va_movement_type_info[$vn_type_id]['color'];
                 if (!$vs_color || $vs_color == '000000') {
                     $vs_color = caGetOption("ca_movements_{$va_movement_type_info[$vn_type_id]['idno']}_color", $pa_bundle_settings, 'ffffff');
                 }
                 $va_history[$va_date['sortable']][] = array('type' => 'ca_movements', 'id' => $vn_movement_id, 'display' => $qr_movements->getWithTemplate($vs_display_template), 'color' => $vs_color, 'icon_url' => $vs_icon_url = $o_media_coder->getMediaTag($va_movement_type_info[$vn_type_id]['icon'], 'icon'), 'typename_singular' => $vs_typename = $va_movement_type_info[$vn_type_id]['name_singular'], 'typename_plural' => $va_movement_type_info[$vn_type_id]['name_plural'], 'type_id' => $vn_type_id, 'icon' => '<div class="caUseHistoryIconContainer" style="background-color: #' . $vs_color . '"><div class="caUseHistoryIcon">' . ($vs_icon_url ? $vs_icon_url : '<div class="caUseHistoryIconText">' . $vs_typename . '</div>') . '</div></div>', 'date' => $va_date['display']);
             }
         }
     }
     // Occurrences
     $va_occurrences = $this->get('ca_occurrences.occurrence_id', array('returnAsArray' => true));
     if (is_array($va_occurrence_types = caGetOption('ca_occurrences_showTypes', $pa_bundle_settings, null)) && is_array($va_occurrences) && sizeof($va_occurrences)) {
         $qr_occurrences = caMakeSearchResult('ca_occurrences', $va_occurrences);
         $t_occurrence = new ca_occurrences();
         $va_occurrence_type_info = $t_occurrence->getTypeList();
         $va_date_elements_by_type = array();
         foreach ($va_occurrence_types as $vn_type_id) {
             if (!is_array($va_date_elements = caGetOption("ca_occurrences_{$va_occurrence_type_info[$vn_type_id]['idno']}_dateElement", $pa_bundle_settings, null)) && $va_date_elements) {
                 $va_date_elements = array($va_date_elements);
             }
             if (!$va_date_elements) {
                 continue;
             }
             $va_date_elements_by_type[$vn_type_id] = $va_date_elements;
         }
         while ($qr_occurrences->nextHit()) {
             $vn_occurrence_id = $qr_occurrences->get('occurrence_id');
             if ((string) $qr_occurrences->get('ca_occurrences.deleted') !== '0') {
                 continue;
             }
             // filter out deleted
             $vn_type_id = $qr_occurrences->get('type_id');
             $va_dates = array();
             if (is_array($va_date_elements_by_type[$vn_type_id]) && sizeof($va_date_elements_by_type[$vn_type_id])) {
                 foreach ($va_date_elements_by_type[$vn_type_id] as $vs_date_element) {
                     $va_dates[] = array('sortable' => $qr_occurrences->get("ca_occurrences.{$vs_date_element}", array('getDirectDate' => true)), 'display' => $qr_occurrences->get("ca_occurrences.{$vs_date_element}"));
                 }
             }
             if (!sizeof($va_dates)) {
                 $va_dates[] = array('sortable' => $vn_date = caUnixTimestampToHistoricTimestamps($qr_occurrences->get('lastModified')), 'display' => caGetLocalizedDate($vn_date));
             }
             $vs_default_display_template = '^ca_occurrences.preferred_labels.name (^ca_occurrences.idno)';
             $vs_display_template = $pb_display_label_only ? $vs_default_display_template : caGetOption("ca_occurrences_{$va_occurrence_type_info[$vn_type_id]['idno']}_displayTemplate", $pa_bundle_settings, $vs_default_display_template);
             foreach ($va_dates as $va_date) {
                 if (!$va_date['sortable']) {
                     continue;
                 }
                 if (!in_array($vn_type_id, $va_occurrence_types)) {
                     continue;
                 }
                 if ($pb_get_current_only && $va_date['sortable'] > $vn_current_date) {
                     continue;
                 }
                 $vs_color = $va_occurrence_type_info[$vn_type_id]['color'];
                 if (!$vs_color || $vs_color == '000000') {
                     $vs_color = caGetOption("ca_occurrences_{$va_occurrence_type_info[$vn_type_id]['idno']}_color", $pa_bundle_settings, 'ffffff');
                 }
                 $va_history[$va_date['sortable']][] = array('type' => 'ca_occurrences', 'id' => $vn_occurrence_id, 'display' => $qr_occurrences->getWithTemplate($vs_display_template), 'color' => $vs_color, 'icon_url' => $vs_icon_url = $o_media_coder->getMediaTag($va_occurrence_type_info[$vn_type_id]['icon'], 'icon'), 'typename_singular' => $vs_typename = $va_occurrence_type_info[$vn_type_id]['name_singular'], 'typename_plural' => $va_occurrence_type_info[$vn_type_id]['name_plural'], 'type_id' => $vn_type_id, 'icon' => '<div class="caUseHistoryIconContainer" style="background-color: #' . $vs_color . '"><div class="caUseHistoryIcon">' . ($vs_icon_url ? $vs_icon_url : '<div class="caUseHistoryIconText">' . $vs_typename . '</div>') . '</div></div>', 'date' => $va_date['display']);
             }
         }
     }
     // Storage locations
     $va_locations = $this->get('ca_objects_x_storage_locations.relation_id', array('returnAsArray' => true));
     if (is_array($va_location_types = caGetOption('ca_storage_locations_showRelationshipTypes', $pa_bundle_settings, null)) && is_array($va_locations) && sizeof($va_locations)) {
         $t_location = new ca_storage_locations();
         $va_location_type_info = $t_location->getTypeList();
         $vs_name_singular = $t_location->getProperty('NAME_SINGULAR');
         $vs_name_plural = $t_location->getProperty('NAME_PLURAL');
         $qr_locations = caMakeSearchResult('ca_objects_x_storage_locations', $va_locations);
         $vs_default_display_template = '^ca_storage_locations.parent.preferred_labels.name ➜ ^ca_storage_locations.preferred_labels.name (^ca_storage_locations.idno)';
         $vs_display_template = $pb_display_label_only ? $vs_default_display_template : caGetOption('ca_storage_locations_displayTemplate', $pa_bundle_settings, $vs_default_display_template);
         Debug::msg($qr_locations->numHits());
         while ($qr_locations->nextHit()) {
             $vn_location_id = $qr_locations->get('ca_objects_x_storage_locations.location_id');
             if ((string) $qr_locations->get('ca_storage_locations.deleted') !== '0') {
                 continue;
             }
             // filter out deleted
             $va_date = array('sortable' => $qr_locations->get("ca_objects_x_storage_locations.effective_date", array('getDirectDate' => true)), 'display' => $qr_locations->get("ca_objects_x_storage_locations.effective_date"));
             if (!$va_date['sortable']) {
                 continue;
             }
             if (!in_array($vn_rel_type_id = $qr_locations->get('ca_objects_x_storage_locations.type_id'), $va_location_types)) {
                 continue;
             }
             $vn_type_id = $qr_locations->get('ca_storage_locations.type_id');
             if ($pb_get_current_only && $va_date['sortable'] > $vn_current_date) {
                 continue;
             }
             $vs_color = $va_location_type_info[$vn_type_id]['color'];
             if (!$vs_color || $vs_color == '000000') {
                 $vs_color = caGetOption("ca_storage_locations_color", $pa_bundle_settings, 'ffffff');
             }
             $va_history[$va_date['sortable']][] = array('type' => 'ca_storage_locations', 'id' => $vn_location_id, 'relation_id' => $qr_locations->get('relation_id'), 'display' => $qr_locations->getWithTemplate("<unit relativeTo='ca_storage_locations'>{$vs_display_template}</unit>"), 'color' => $vs_color, 'icon_url' => $vs_icon_url = $o_media_coder->getMediaTag($va_location_type_info[$vn_type_id]['icon'], 'icon'), 'typename_singular' => $vs_name_singular, 'typename_plural' => $vs_name_plural, 'type_id' => $vn_type_id, 'rel_type_id' => $vn_rel_type_id, 'icon' => '<div class="caUseHistoryIconContainer" style="background-color: #' . $vs_color . '"><div class="caUseHistoryIcon">' . ($vs_icon_url ? $vs_icon_url : '<div class="caUseHistoryIconText">' . $vs_name_singular . '</div>') . '</div></div>', 'date' => $va_date['display']);
         }
     }
     // Deaccession
     if ($this->get('is_deaccessioned') && caGetOption('showDeaccessionInformation', $pa_bundle_settings, false)) {
         $vs_color = caGetOption('deaccession_color', $pa_bundle_settings, 'cccccc');
         $vn_date = $this->get('deaccession_date', array('getDirectDate' => true));
         $vs_default_display_template = '^ca_objects.deaccession_notes';
         $vs_display_template = $pb_display_label_only ? $vs_default_display_template : caGetOption('deaccession_displayTemplate', $pa_bundle_settings, $vs_default_display_template);
         if (!($pb_get_current_only && $vn_date > $vn_current_date)) {
             $va_history[$vn_date][] = array('type' => 'ca_objects_deaccession', 'id' => $this->getPrimaryKey(), 'display' => $this->getWithTemplate("<unit>{$vs_display_template}</unit>"), 'color' => $vs_color, 'icon_url' => '', 'typename_singular' => $vs_name_singular = _t('deaccession'), 'typename_plural' => $vs_name_plural = _t('deaccessions'), 'type_id' => null, 'icon' => '<div class="caUseHistoryIconContainer" style="background-color: #' . $vs_color . '"><div class="caUseHistoryIcon"><div class="caUseHistoryIconText">' . $vs_name_singular . '</div>' . '</div></div>', 'date' => $this->get('deaccession_date'));
         }
     }
     ksort($va_history);
     $va_history = array_reverse($va_history);
     if ($pn_limit > 0) {
         $va_history = array_slice($va_history, 0, $pn_limit);
     }
     if (sizeof(ca_objects::$s_object_use_cache[$vs_cache_key]) > 100) {
         ca_objects::$s_object_use_cache[$vs_cache_key] = array_slice(ca_objects::$s_object_use_cache[$vs_cache_key], 0, 50);
     }
     return ca_objects::$s_object_use_cache[$vs_cache_key] = $va_history;
 }
Exemplo n.º 3
0
 /**
  * Returns loan_id for the loan with the specified name (and type) or idno (regardless of specified type.) If the loan does not already
  * exist then it will be created with the specified name, type and locale, as well as with any specified values in the $pa_values array.
  * $pa_values keys should be either valid loan fields or attributes.
  *
  * @param string $ps_loan_name Loan label name
  * @param int $pn_type_id The type_id of the loan type to use if the loan needs to be created
  * @param int $pn_locale_id The locale_id to use if the loan needs to be created (will be used for both the loan locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created loan records with. These values are *only* used for newly created loans; they will not be applied if the loan named already exists. The array keys should be names of loan fields or valid loan attributes. Values should be either a scalar (for single-value attributes) or an array of values for (multi-valued attributes)
  * @param array $pa_options An optional array of options, which include:
  *                outputErrors - if true, errors will be printed to console [default=false]
  *                matchOn = optional list indicating sequence of checks for an existing record; values of array can be "label" and "idno". Ex. array("idno", "label") will first try to match on idno and then label if the first match fails.
  *                dontCreate - if true then new loans will not be created [default=false]
  *                transaction - if Transaction object is passed, use it for all Db-related tasks [default=null]
  *                returnInstance = return ca_loans instance rather than loan_id. Default is false.
  *                generateIdnoWithTemplate = A template to use when setting the idno. The template is a value with automatically-set SERIAL values replaced with % characters. Eg. 2012.% will set the created row's idno value to 2012.121 (assuming that 121 is the next number in the serial sequence.) The template is NOT used if idno is passed explicitly as a value in $pa_values.
  *                importEvent = if ca_data_import_events instance is passed then the insert/update of the loan will be logged as part of the import
  *                importEventSource = if importEvent is passed, then the value set for importEventSource is used in the import event log as the data source. If omitted a default value of "?" is used
  *                nonPreferredLabels = an optional array of nonpreferred labels to add to any newly created loans. Each label in the array is an array with required loan label values.
  *                log = if KLogger instance is passed then actions will be logged
  * @return bool|\ca_loans|mixed|null
  */
 static function getLoanID($ps_loan_name, $pn_type_id, $pn_locale_id, $pa_values = null, $pa_options = null)
 {
     if (!is_array($pa_options)) {
         $pa_options = array();
     }
     if (!isset($pa_options['outputErrors'])) {
         $pa_options['outputErrors'] = false;
     }
     $pa_match_on = caGetOption('matchOn', $pa_options, array('label', 'idno'), array('castTo' => "array"));
     /** @var ca_data_import_events $o_event */
     $o_event = isset($pa_options['importEvent']) && $pa_options['importEvent'] instanceof ca_data_import_events ? $pa_options['importEvent'] : null;
     $t_loan = new ca_loans();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_loan->setTransaction($pa_options['transaction']);
         if ($o_event) {
             $o_event->setTransaction($pa_options['transaction']);
         }
     }
     $vs_event_source = isset($pa_options['importEventSource']) && $pa_options['importEventSource'] ? $pa_options['importEventSource'] : "?";
     /** @var KLogger $o_log */
     $o_log = isset($pa_options['log']) && $pa_options['log'] instanceof KLogger ? $pa_options['log'] : null;
     $vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null;
     if (preg_match('!\\%!', $vs_idno)) {
         $pa_options['generateIdnoWithTemplate'] = $vs_idno;
         $vs_idno = null;
     }
     if (!$vs_idno) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_loan->setIdnoWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     $vn_id = null;
     foreach ($pa_match_on as $vs_match_on) {
         switch (strtolower($vs_match_on)) {
             case 'label':
             case 'labels':
                 if (trim($ps_loan_name)) {
                     if ($vn_id = ca_loans::find(array('preferred_labels' => array('name' => $ps_loan_name), 'type_id' => $pn_type_id, 'parent_id' => caGetOption('parent_id', $pa_values, null)), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                         break 2;
                     }
                     break;
                 }
             case 'idno':
                 if ($vs_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 if ($vn_id = ca_loans::find(array('idno' => $vs_idno ? $vs_idno : $ps_loan_name), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) {
                     break 2;
                 }
                 break;
         }
     }
     if (!$vn_id) {
         if (isset($pa_options['dontCreate']) && $pa_options['dontCreate']) {
             return false;
         }
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_loans', 'I');
         }
         $t_loan->setMode(ACCESS_WRITE);
         $t_loan->set('locale_id', $pn_locale_id);
         $t_loan->set('type_id', $pn_type_id);
         $t_loan->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_loan->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_loan->set('idno', $vs_idno);
         $t_loan->set('parent_id', isset($pa_values['parent_id']) ? $pa_values['parent_id'] : null);
         $t_loan->insert();
         if ($t_loan->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_loan->addLabel(array('name' => $ps_loan_name), $pn_locale_id, null, true);
         if ($t_loan->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
             }
             $vb_label_errors = true;
         }
         /** @var IIDNumbering $o_idno */
         if ($o_idno = $t_loan->getIDNoPlugInInstance()) {
             $va_values = $o_idno->htmlFormValuesAsArray('idno', $vs_idno);
             if (!is_array($va_values)) {
                 $va_values = array($va_values);
             }
             if (!($vs_sep = $o_idno->getSeparator())) {
                 $vs_sep = '';
             }
             if (($vs_proc_idno = join($vs_sep, $va_values)) && $vs_proc_idno != $vs_idno) {
                 $t_loan->set('idno', $vs_proc_idno);
                 $t_loan->update();
                 if ($t_loan->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not update idno for %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not update idno for %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
                     }
                     return null;
                 }
             }
         }
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         $vb_attr_errors = false;
         if (is_array($pa_values)) {
             foreach ($pa_values as $vs_element => $va_values) {
                 if (!caIsIndexedArray($va_values)) {
                     $va_values = array($va_values);
                 }
                 foreach ($va_values as $va_value) {
                     if (is_array($va_value)) {
                         // array of values (complex multi-valued attribute)
                         $t_loan->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                     } else {
                         // scalar value (simple single value attribute)
                         if ($va_value) {
                             $t_loan->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                         }
                     }
                 }
             }
             $t_loan->update();
             if ($t_loan->numErrors()) {
                 if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                     print "[Error] " . _t("Could not set values for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
                 }
                 if ($o_log) {
                     $o_log->logError(_t("Could not set values for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
                 }
                 $vb_attr_errors = true;
             }
         }
         if (is_array($va_nonpreferred_labels = caGetOption("nonPreferredLabels", $pa_options, null))) {
             if (caIsAssociativeArray($va_nonpreferred_labels)) {
                 // single non-preferred label
                 $va_labels = array($va_nonpreferred_labels);
             } else {
                 // list of non-preferred labels
                 $va_labels = $va_nonpreferred_labels;
             }
             foreach ($va_labels as $va_label) {
                 $t_loan->addLabel($va_label, $pn_locale_id, null, false);
                 if ($t_loan->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not set non-preferred label for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not set non-preferred label for loan %1: %2", $ps_loan_name, join('; ', $t_loan->getErrors())));
                     }
                 }
             }
         }
         $vn_loan_id = $t_loan->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_loan_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_loan->getErrors())));
             } else {
                 $o_event->endItem($vn_loan_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new loan %1", $ps_loan_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_loan;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_loans', 'U');
         }
         $vn_loan_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_loan_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing loan %1 in DataMigrationUtils::getLoanID()", $ps_loan_name));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_loan = new ca_loans($vn_loan_id);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_loan->setTransaction($pa_options['transaction']);
             }
             return $t_loan;
         }
     }
     return $vn_loan_id;
 }