/** 
  * Returns entity_id for the entity with the specified name, regardless of specified type. If the entity 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 entity fields or attributes.
  *
  * @param array $pa_entity_name Array with values for entity label
  * @param int $pn_type_id The type_id of the entity type to use if the entity needs to be created
  * @param int $pn_locale_id The locale_id to use if the entity needs to be created (will be used for both the entity locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created entity records with. These values are *only* used for newly created entities; they will not be applied if the entity named already exists. The array keys should be names of ca_entities fields or valid entity 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 entities will not be created [default=false]
  *				matchOnIdno - try to match on idno if name match fails [default=false]
  *				matchOnDisplayName  if true then entities are looked up exclusively using displayname, otherwise forename and surname fields are used [default=false]
  * 				transaction - if Transaction instance is passed, use it for all Db-related tasks [default=null]
  *				returnInstance = return ca_entities instance rather than entity_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 entity 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 getEntityID($pa_entity_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_displayname = caGetOption('matchOnDisplayName', $pa_options, false);
     $pb_match_on_idno = caGetOption('matchOnIdno', $pa_options, false);
     $t_entity = new ca_entities();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_entity->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 (!($vs_idno = isset($pa_values['idno']) ? (string) $pa_values['idno'] : null)) {
         if (isset($pa_options['generateIdnoWithTemplate']) && $pa_options['generateIdnoWithTemplate']) {
             $vs_idno = $t_entity->setIdnoTWithTemplate($pa_options['generateIdnoWithTemplate'], array('dontSetValue' => true));
         }
     }
     if ($pb_match_on_displayname) {
         $vn_id = ca_entities::find(array_merge(array('preferred_labels' => array('displayname' => $pa_entity_name['displayname']), $va_find_arr)), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
     } else {
         $vn_id = ca_entities::find(array_merge(array('preferred_labels' => array('forename' => $pa_entity_name['forename'], 'surname' => $pa_entity_name['surname']), $va_find_arr)), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
     }
     if (!$vn_id && $pb_match_on_idno) {
         $va_find_arr['idno'] = $vs_idno;
         $vn_id = ca_entities::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_entities', 'I');
         }
         $t_entity->setMode(ACCESS_WRITE);
         $t_entity->set('locale_id', $pn_locale_id);
         $t_entity->set('type_id', $pn_type_id);
         $t_entity->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_entity->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_entity->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_entity->set('idno', $vs_idno);
         $t_entity->set('lifespan', isset($pa_values['lifespan']) ? $pa_values['lifespan'] : null);
         $t_entity->set('parent_id', isset($pa_values['parent_id']) ? $pa_values['parent_id'] : null);
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         unset($pa_values['lifespan']);
         unset($pa_values['_interstitial']);
         $t_entity->insert();
         if ($t_entity->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_entity->addLabel($pa_entity_name, $pn_locale_id, null, true);
         if ($t_entity->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())));
             }
             $vb_label_errors = true;
         }
         $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_entity->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                 } else {
                     // scalar value (simple single value attribute)
                     if ($va_value) {
                         $t_entity->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                     }
                 }
             }
             $t_entity->update();
             if ($t_entity->numErrors()) {
                 if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                     print "[Error] " . _t("Could not set values for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())) . "\n";
                 }
                 if ($o_log) {
                     $o_log->logError(_t("Could not set values for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())));
                 }
                 $vb_attr_errors = true;
             }
         }
         $vn_entity_id = $t_entity->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_entity_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_entity->getErrors())));
             } else {
                 $o_event->endItem($vn_entity_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new entity %1", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname']));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_entity;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_entities', 'U');
         }
         $vn_entity_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_entity_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing entity %1 in DataMigrationUtils::getEntityID(); total of %2 entities were found", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], sizeof($va_entity_ids) + 1));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return new ca_entities($vn_entity_id);
         }
     }
     return $vn_entity_id;
 }
 /**
  * Remove all likes from object (for one user)
  * @param array $pa_data
  * @return bool
  */
 protected function unlike($pa_data)
 {
     if (!is_array($pa_data) || !isset($pa_data['object_id']) || !isset($pa_data['entity_id'])) {
         $this->addError("Malformed request body");
         return false;
     }
     $vn_object_id = (int) $pa_data['object_id'];
     $t_object = new ca_objects($vn_object_id);
     if (!$t_object->getPrimaryKey()) {
         $this->addError("Invalid object id");
         return false;
     }
     $vn_entity_id = (int) $pa_data['entity_id'];
     $t_entity = new ca_entities($vn_entity_id);
     if (!$t_entity->getPrimaryKey()) {
         $this->addError("Invalid entity id");
         return false;
     }
     $o_db = new Db();
     $qr_likes = $o_db->query("\n\t\t\tDELETE FROM ca_item_comments WHERE name = ? AND table_num = ? AND row_id = ?\n\t\t", $vn_entity_id, $t_object->tableNum(), $t_object->getPrimaryKey());
     if (!$qr_likes) {
         $this->addError('something may have went wrong');
         return false;
     }
     return array('msg' => 'like(s) successfully removed');
 }
 /**
  * Returns entity_id for the entity with the specified name (and type) or idno (regardless of specified type.) If the entity 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 entity fields or attributes.
  *
  * @param array $pa_entity_name Array with values for entity label
  * @param int $pn_type_id The type_id of the entity type to use if the entity needs to be created
  * @param int $pn_locale_id The locale_id to use if the entity needs to be created (will be used for both the entity locale as well as the label locale)
  * @param array $pa_values An optional array of additional values to populate newly created entity records with. These values are *only* used for newly created entities; they will not be applied if the entity named already exists. The array keys should be names of ca_entities fields or valid entity 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 entities 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.
  *                matchOnDisplayName  if true then entities are looked up exclusively using displayname, otherwise forename and surname fields are used [default=false]
  *                transaction - if Transaction instance is passed, use it for all Db-related tasks [default=null]
  *                returnInstance = return ca_entities instance rather than entity_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 entity 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 entities. Each label in the array is an array with required entity label values.
  *                log = if KLogger instance is passed then actions will be logged
  * @return bool|\ca_entities|mixed|null
  */
 static function getEntityID($pa_entity_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_displayname = caGetOption('matchOnDisplayName', $pa_options, 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_entity = new ca_entities();
     if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
         $t_entity->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_parent_id = isset($pa_values['parent_id']) && $pa_values['parent_id'] ? $pa_values['parent_id'] : 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_entity->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 ($pb_match_on_displayname && strlen(trim($pa_entity_name['displayname'])) > 0) {
                     $vn_id = ca_entities::find(array('preferred_labels' => array('displayname' => $pa_entity_name['displayname']), 'type_id' => $pn_type_id, 'parent_id' => $vn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
                 } else {
                     $vn_id = ca_entities::find(array('preferred_labels' => array('forename' => $pa_entity_name['forename'], 'surname' => $pa_entity_name['surname']), 'type_id' => $pn_type_id, 'parent_id' => $vn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
                 }
                 if ($vn_id) {
                     break 2;
                 }
                 break;
             case 'surname':
                 $vn_id = ca_entities::find(array('preferred_labels' => array('surname' => $pa_entity_name['surname']), 'type_id' => $pn_type_id, 'parent_id' => $vn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
                 if ($vn_id) {
                     break 2;
                 }
                 break;
             case 'forename':
                 $vn_id = ca_entities::find(array('preferred_labels' => array('forename' => $pa_entity_name['forename']), 'type_id' => $pn_type_id, 'parent_id' => $vn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
                 if ($vn_id) {
                     break 2;
                 }
                 break;
             case 'displayname':
                 $vn_id = ca_entities::find(array('preferred_labels' => array('displayname' => $pa_entity_name['displayname']), 'type_id' => $pn_type_id, 'parent_id' => $vn_parent_id), array('returnAs' => 'firstId', 'transaction' => $pa_options['transaction']));
                 if ($vn_id) {
                     break 2;
                 }
                 break;
             case 'idno':
                 if ($vs_idno == '%') {
                     break;
                 }
                 // don't try to match on an unreplaced idno placeholder
                 if (($vs_idno || trim($pa_entity_name['_originalText'])) && ($vn_id = ca_entities::find(array('idno' => $vs_idno ? $vs_idno : $pa_entity_name['_originalText']), 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_entities', 'I');
         }
         $t_entity->setMode(ACCESS_WRITE);
         $t_entity->set('locale_id', $pn_locale_id);
         $t_entity->set('type_id', $pn_type_id);
         $t_entity->set('source_id', isset($pa_values['source_id']) ? $pa_values['source_id'] : null);
         $t_entity->set('access', isset($pa_values['access']) ? $pa_values['access'] : 0);
         $t_entity->set('status', isset($pa_values['status']) ? $pa_values['status'] : 0);
         $t_entity->set('idno', $vs_idno);
         $t_entity->set('lifespan', isset($pa_values['lifespan']) ? $pa_values['lifespan'] : null);
         $t_entity->set('parent_id', isset($pa_values['parent_id']) ? $pa_values['parent_id'] : null);
         unset($pa_values['access']);
         unset($pa_values['status']);
         unset($pa_values['idno']);
         unset($pa_values['source_id']);
         unset($pa_values['lifespan']);
         unset($pa_values['_interstitial']);
         $t_entity->insert();
         if ($t_entity->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not insert entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not insert entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())));
             }
             return null;
         }
         $vb_label_errors = false;
         $t_entity->addLabel($pa_entity_name, $pn_locale_id, null, true);
         if ($t_entity->numErrors()) {
             if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                 print "[Error] " . _t("Could not set preferred label for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())) . "\n";
             }
             if ($o_log) {
                 $o_log->logError(_t("Could not set preferred label for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())));
             }
             $vb_label_errors = true;
         }
         /** @var IIDNumbering $o_idno */
         if ($o_idno = $t_entity->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_entity->set('idno', $vs_proc_idno);
                 $t_entity->update();
                 if ($t_entity->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not update idno for %1: %2", join("/", $pa_entity_name), join('; ', $t_entity->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not update idno for %1: %2", join("/", $pa_entity_name), join('; ', $t_entity->getErrors())));
                     }
                     return null;
                 }
             }
         }
         $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_entity->addAttribute(array_merge($va_value, array('locale_id' => $pn_locale_id)), $vs_element);
                     } else {
                         // scalar value (simple single value attribute)
                         if ($va_value) {
                             $t_entity->addAttribute(array('locale_id' => $pn_locale_id, $vs_element => $va_value), $vs_element);
                         }
                     }
                 }
             }
             $t_entity->update();
             if ($t_entity->numErrors()) {
                 if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                     print "[Error] " . _t("Could not set values for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->getErrors())) . "\n";
                 }
                 if ($o_log) {
                     $o_log->logError(_t("Could not set values for entity %1: %2", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname'], join('; ', $t_entity->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_entity->addLabel($va_label, $pn_locale_id, null, false);
                 if ($t_entity->numErrors()) {
                     if (isset($pa_options['outputErrors']) && $pa_options['outputErrors']) {
                         print "[Error] " . _t("Could not set non-preferred label for entity %1: %2", $va_label['forename'] . "/" . $va_label['surname'], join('; ', $t_entity->getErrors())) . "\n";
                     }
                     if ($o_log) {
                         $o_log->logError(_t("Could not set non-preferred label for entity %1: %2", $va_label['forename'] . "/" . $va_label['surname'], join('; ', $t_entity->getErrors())));
                     }
                 }
             }
         }
         $vn_entity_id = $t_entity->getPrimaryKey();
         if ($o_event) {
             if ($vb_attr_errors || $vb_label_errors) {
                 $o_event->endItem($vn_entity_id, __CA_DATA_IMPORT_ITEM_PARTIAL_SUCCESS__, _t("Errors setting field values: %1", join('; ', $t_entity->getErrors())));
             } else {
                 $o_event->endItem($vn_entity_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
             }
         }
         if ($o_log) {
             $o_log->logInfo(_t("Created new entity %1", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname']));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             return $t_entity;
         }
     } else {
         if ($o_event) {
             $o_event->beginItem($vs_event_source, 'ca_entities', 'U');
         }
         $vn_entity_id = $vn_id;
         if ($o_event) {
             $o_event->endItem($vn_entity_id, __CA_DATA_IMPORT_ITEM_SUCCESS__, '');
         }
         if ($o_log) {
             $o_log->logDebug(_t("Found existing entity %1 in DataMigrationUtils::getEntityID()", $pa_entity_name['forename'] . "/" . $pa_entity_name['surname']));
         }
         if (isset($pa_options['returnInstance']) && $pa_options['returnInstance']) {
             $t_entity = new ca_entities($vn_entity_id);
             if (isset($pa_options['transaction']) && $pa_options['transaction'] instanceof Transaction) {
                 $t_entity->setTransaction($pa_options['transaction']);
             }
             return $t_entity;
         }
     }
     return $vn_entity_id;
 }
 /**
  *
  */
 public function parseValue($ps_value, $pa_element_info, $pa_options = null)
 {
     $vb_require_value = is_null($pa_element_info['settings']['requireValue']) ? true : (bool) $pa_element_info['settings']['requireValue'];
     if (preg_match('![^\\d]+!', $ps_value)) {
         // try to convert idno to entity_id
         if ($vn_id = ca_entities::find(array('idno' => $ps_value), array('returnAs' => 'firstId'))) {
             $ps_value = $vn_id;
         }
     }
     if (!$vb_require_value && !(int) $ps_value) {
         return array('value_longtext1' => null, 'value_integer1' => null);
     }
     if (strlen($ps_value) && !is_numeric($ps_value)) {
         $this->postError(1970, _t('Item_id %2 is not valid for element %1', $pa_element_info["element_code"], $ps_value), 'EntitiesAttributeValue->parseValue()');
         return false;
     }
     $t_item = new ca_entities((int) $ps_value);
     if (!$t_item->getPrimaryKey()) {
         if ($ps_value) {
             $this->postError(1970, _t('%1 is not a valid entity_id for %2 [%3]', $ps_value, $pa_element_info['displayLabel'], $pa_element_info['element_code']), 'EntitiesAttributeValue->parseValue()');
         } else {
             return null;
         }
         return false;
     }
     return array('value_longtext1' => $ps_value, 'value_integer1' => (int) $ps_value);
 }
 /**
  * 
  */
 private function _checkEntity()
 {
     $t_entity = new ca_entities($vn_id = $this->opo_request->getParameter('id', pInteger));
     if (!$t_entity->getPrimaryKey()) {
         return $this->makeResponse(array(), 500, 'Victim ID is invalid');
     }
     // is the entity a victim?
     if ($t_entity->getTypeID() != $this->opn_victim_type_id) {
         return $this->makeResponse(array(), 500, 'Entity is not of type "victim"; expected ' . $this->opn_victim_type_id . '; got ' . $t_entity->getTypeID());
     }
     return $t_entity;
 }