public function Edit($pa_values = null, $pa_options = null) { $va_values = array(); if ($vn_lot_id = $this->request->getParameter('lot_id', pInteger)) { $t_lot = new ca_object_lots($vn_lot_id); if ($t_lot->getPrimaryKey()) { $va_values['lot_id'] = $vn_lot_id; $va_values['idno'] = $t_lot->get('idno_stub'); } } return parent::Edit($va_values, $pa_options); }
/** * Override set() to do idno_stub lookups on lots * * @param mixed $pm_fields * @param mixed $pm_value * @param array $pa_options Most options are handled by subclasses. Options defined here include: * assumeIdnoStubForLotID = set to force lookup of lot_id values as ca_object_lots.idno_stub values first not matter what, before consideration as a numeric lot_id. The default is false, in which case integer values are considered lot_ids and non-numeric values possible idno_stubs. * * @return int */ public function set($pm_fields, $pm_value = "", $pa_options = null) { if (!is_array($pm_fields)) { $pm_fields = array($pm_fields => $pm_value); } $pb_assume_idno_stub_for_lot_id = caGetOption('assumeIdnoStubForLotID', $pa_options, false); foreach ($pm_fields as $vs_fld => $vs_val) { if ($vs_fld == 'lot_id' && ($pb_assume_idno_stub_for_lot_id || preg_match("![^\\d]+!", $vs_val))) { $t_lot = new ca_object_lots(); if ($t_lot->load(array('idno_stub' => $vs_val))) { $vn_lot_id = (int) $t_lot->getPrimaryKey(); $pm_fields[$vs_fld] = $vn_lot_id; } } } return parent::set($pm_fields, null, $pa_options); }
public function getLotMedia() { //if ((bool)$this->request->config->get('allow_download_of_all_object_media_in_a_lot')) { // DO WE NEED TO REINSTATE THIS OPTIN? set_time_limit(600); // allow a lot of time for this because the sets can be potentially large $t_lot = new ca_object_lots($this->request->getParameter('lot_id', pInteger)); $o_media_metadata_conf = Configuration::load($t_lot->getAppConfig()->get('media_metadata')); if ($t_lot->getPrimaryKey()) { $va_object_ids = $t_lot->get('ca_objects.object_id', array('returnAsArray' => true, 'limit' => 100000)); if (!is_array($va_object_ids) || !sizeof($va_object_ids)) { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey())); return; } $qr_res = ca_objects::createResultSet($va_object_ids); $qr_res->filterNonPrimaryRepresentations(false); $va_paths = array(); while ($qr_res->nextHit()) { $va_original_paths = $qr_res->getMediaPaths('ca_object_representations.media', 'original'); if (sizeof($va_original_paths) > 0) { $va_paths[$qr_res->get('object_id')] = array('idno' => $qr_res->get('idno'), 'type_code' => caGetListItemIdno($qr_res->get('type_id')), 'paths' => $va_original_paths, 'representation_ids' => $qr_res->get('ca_object_representations.representation_id', array('returnAsArray' => true)), 'representation_types' => $qr_res->get('ca_object_representations.type_id', array('returnAsArray' => true))); } } if (sizeof($va_paths) > 0) { $o_zip = new ZipStream(); foreach ($va_paths as $vn_object_id => $va_path_info) { // make sure we don't download representations the user isn't allowed to read if (!caCanRead($this->request->user->getPrimaryKey(), 'ca_objects', $vn_object_id)) { continue; } $vn_c = 1; foreach ($va_path_info['paths'] as $vn_i => $vs_media_path) { if (!file_exists($vs_media_path)) { continue; } if ($o_media_metadata_conf->get('do_metadata_embedding_for_lot_media_download')) { if (!($vs_path = caEmbedMediaMetadataIntoFile($vs_media_path, 'ca_objects', $vn_object_id, $va_path_info['type_code'], $va_path_info['representation_ids'][$vn_i], $va_path_info['representation_types'][$vn_i]))) { $vs_path = $vs_media_path; } } else { $vs_path = $vs_media_path; } $vs_filename = $va_path_info['idno'] ? $va_path_info['idno'] : $vn_object_id; $vs_filename .= "_{$vn_c}"; if ($vs_ext = pathinfo($vs_media_path, PATHINFO_EXTENSION)) { $vs_filename .= ".{$vs_ext}"; } $o_zip->addFile($vs_path, $vs_filename); $vn_c++; } } $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/'); // send files $o_view->setVar('zip_stream', $o_zip); $o_view->setVar('archive_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', ($vs_idno = $t_lot->get('idno_stub')) ? $vs_idno : $t_lot->getPrimaryKey()), 0, 20) . '.zip'); $this->response->addContent($o_view->render('download_file_binary.php')); return; } else { $this->notification->addNotification(_t('No files to download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey())); return; } } //} return $this->Edit(); }
/** * Returns lot_id for the lot with the specified label (and type) or idno (regardless of specified type.) If the lot does not already * exist then it will be created with the specified idno, name, type and locale, as well as with any specified values in the $pa_values array. * $pa_values keys should be either valid lot fields or attributes. * * @param string $ps_idno_stub Lot identifier * @param string $ps_lot_name Lot name * @param int $pn_type_id The type_id of the object type to use if the object needs to be created * @param int $pn_locale_id The locale_id to use if the object needs to be created (will be used for both the object locale as well as the label locale) * @param array $pa_values An optional array of additional values to populate newly created object records with. These values are *only* used for newly created objects; they will not be applied if the object named already exists. The array keys should be names of ca_object_lots fields or valid object 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] * dontCreate - if true then new lots will not be created [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. * transaction - if Transaction object is passed, use it for all Db-related tasks [default=null] * returnInstance = return ca_object_lots instance rather than object_id. Default is false. * importEvent = if ca_data_import_events instance is passed then the insert/update of the object 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 lots. Each label in the array is an array with required lot label values. * log = if KLogger instance is passed then actions will be logged * @return bool|\ca_object_lots|mixed|null */ static function getObjectLotID($ps_idno_stub, $ps_lot_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_lot = new ca_object_lots(); if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) { $t_lot->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; $vn_id = null; if (preg_match('!\\%!', $ps_idno_stub)) { $pa_options['generateIdnoWithTemplate'] = $ps_idno_stub; $ps_idno_stub = null; } if (!$ps_idno_stub) { if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) { $ps_idno_stub = $t_lot->setIdnoWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true)); } } foreach ($pa_match_on as $vs_match_on) { switch (strtolower($vs_match_on)) { case 'label': case 'labels': if (trim($ps_lot_name)) { if ($vn_id = ca_object_lots::find(array('preferred_labels' => array('name' => $ps_lot_name), 'type_id' => $pn_type_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']))) { break 2; } } break; case 'idno': if ($ps_idno_stub == '%') { break; } // don't try to match on an unreplaced idno placeholder if ($vn_id = ca_object_lots::find(array('idno_stub' => $ps_idno_stub ? $ps_idno_stub : $ps_lot_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_object_lots', 'I'); } $t_lot->setMode(ACCESS_WRITE); $t_lot->set('locale_id', $pn_locale_id); $t_lot->set('type_id', $pn_type_id); $t_lot->set('lot_status_id', isset($pa_values['lot_status_id']) ? $pa_values['lot_status_id'] : null); $t_lot->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0); $t_lot->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0); $t_lot->set('idno_stub', $ps_idno_stub); $t_lot->insert(); if ($t_lot->numErrors()) { if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) { print "[Error] " . _t("Could not insert lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors())) . "\n"; } if ($o_log) { $o_log->logError(_t("Could not insert lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors()))); } return null; } $vb_label_errors = false; $t_lot->addLabel(array('name' => $ps_lot_name), $pn_locale_id, null, true); if ($t_lot->numErrors()) { if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) { print "[Error] " . _t("Could not set preferred label for lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors())) . "\n"; } if ($o_log) { $o_log->logError(_t("Could not set preferred label for lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors()))); } $vb_label_errors = true; } /** @var IIDNumbering $o_idno */ if ($o_idno = $t_lot->getIDNoPlugInInstance()) { $va_values = $o_idno->htmlFormValuesAsArray('idno', $ps_idno_stub); 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 != $ps_idno_stub) { $t_lot->set('idno', $vs_proc_idno); $t_lot->update(); if ($t_lot->numErrors()) { if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) { print "[Error] " . _t("Could not update idno for %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors())) . "\n"; } if ($o_log) { $o_log->logError(_t("Could not idno for %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors()))); } return null; } } } unset($pa_values['access']); unset($pa_values['status']); unset($pa_values['idno_stub']); unset($pa_values['lot_status_id']); $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_lot->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element); } else { // scalar value (simple single value attribute) if ($va_value) { $t_lot->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element); } } } } $t_lot->update(); if ($t_lot->numErrors()) { if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) { print "[Error] " . _t("Could not set values for lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors())) . "\n"; } if ($o_log) { $o_log->logError(_t("Could not set values for lot %1: %2", $ps_lot_name, join('; ', $t_lot->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_lot->addLabel($va_label, $pn_locale_id, null, false); if ($t_lot->numErrors()) { if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) { print "[Error] " . _t("Could not set non-preferred label for lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors())) . "\n"; } if ($o_log) { $o_log->logError(_t("Could not set non-preferred label for lot %1: %2", $ps_lot_name, join('; ', $t_lot->getErrors()))); } } } } $vn_lot_id = $t_lot->getPrimaryKey(); if ($o_event) { if ($vb_attr_errors || $vb_label_errors) { $o_event->endItem($vn_lot_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_lot->getErrors()))); } else { $o_event->endItem($vn_lot_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, ''); } } if ($o_log) { $o_log->logInfo(_t("Created new lot %1", $ps_lot_name)); } if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) { return $t_lot; } } else { if ($o_event) { $o_event->beginItem($vs_event_source, 'ca_object_lots', 'U'); } $vn_lot_id = $vn_id; if ($o_event) { $o_event->endItem($vn_lot_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, ''); } if ($o_log) { $o_log->logDebug(_t("Found existing lot %1 in DataMigrationUtils::getObjectLotID()", $ps_lot_name)); } if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) { $t_lot = new ca_object_lots($vn_lot_id); if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) { $t_lot->setTransaction($pa_options['transaction']); } return $t_lot; } } return $vn_lot_id; }
public function getLotMedia() { //if ((bool)$this->request->config->get('allow_download_of_all_object_media_in_a_lot')) { set_time_limit(600); // allow a lot of time for this because the sets can be potentially large $t_lot = new ca_object_lots($this->request->getParameter('lot_id', pInteger)); if ($t_lot->getPrimaryKey()) { $va_object_ids = $t_lot->get('ca_objects.object_id', array('returnAsArray' => true, 'limit' => 100000)); if (!is_array($va_object_ids) || !sizeof($va_object_ids)) { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey())); return; } $qr_res = ca_objects::createResultSet($va_object_ids); $qr_res->filterNonPrimaryRepresentations(false); $va_paths = array(); while ($qr_res->nextHit()) { $va_original_paths = $qr_res->getMediaPaths('ca_object_representations.media', 'original'); if (sizeof($va_original_paths) > 0) { $va_paths[$qr_res->get('object_id')] = array('idno' => $qr_res->get('idno'), 'paths' => $va_original_paths); } } if (sizeof($va_paths) > 0) { $vs_tmp_name = caGetTempFileName('DownloadLotMedia', 'zip'); $o_phar = new PharData($vs_tmp_name, null, null, Phar::ZIP); foreach ($va_paths as $vn_object_id => $va_path_info) { $vn_c = 1; foreach ($va_path_info['paths'] as $vs_path) { if (!file_exists($vs_path)) { continue; } $vs_filename = $va_path_info['idno'] ? $va_path_info['idno'] : $vn_object_id; $vs_filename .= "_{$vn_c}"; if ($vs_ext = pathinfo($vs_path, PATHINFO_EXTENSION)) { $vs_filename .= ".{$vs_ext}"; } $o_phar->addFile($vs_path, $vs_filename); $vn_c++; } } $o_view = new View($this->request, $this->request->getViewsDirectoryPath() . '/bundles/'); // send download $vs_idno = $t_lot->get('idno_stub'); $o_view->setVar('tmp_file', $vs_tmp_name); $o_view->setVar('download_name', 'media_for_' . mb_substr(preg_replace('![^A-Za-z0-9]+!u', '_', $vs_idno ? $vs_idno : $t_lot->getPrimaryKey()), 0, 20) . '.zip'); $this->response->addContent($o_view->render('ca_object_lots_download_media.php')); return; } else { $this->notification->addNotification(_t('No media is available for download'), __NOTIFICATION_TYPE_ERROR__); $this->opo_response->setRedirect(caEditorUrl($this->opo_request, 'ca_object_lots', $t_lot->getPrimaryKey())); return; } } //} return $this->Edit(); }