/**
  * Clone/copy this definition for a new course
  *
  * @param int $ref_id ref-ID of new course
  * @return srCertificateDefinition
  */
 public function copy($ref_id)
 {
     $this->log('Certificate: copy definitions from ' . $this->getRefId() . ' to ' . $ref_id);
     $new_definition = srCertificateDefinition::where(array("ref_id" => $ref_id))->first();
     if (!$new_definition) {
         $this->log('there is no existing definition for ' . $ref_id . ', generating a new one.');
         $new_definition = new srCertificateDefinition();
         $new_definition->setRefId($ref_id);
         $new_definition->setTypeId($this->getTypeId());
         $new_definition->create();
     } else {
         $this->log('used existing definition for ' . $ref_id . '.');
     }
     $this->log('ID of clone: ' . $new_definition->getId());
     $new_definition->setRefId($ref_id);
     $new_definition->setTypeId($this->getTypeId());
     // Clone Signature setting
     if ($this->getSignatureId()) {
         $new_definition->setSignatureId($this->getSignatureId());
     }
     $new_definition->setTypeChanged(false);
     $new_definition->update();
     // Settings and placeholder values now contain default values inherited from type.
     // We overwrite them with the values from this definition
     /** @var $setting srCertificateDefinitionSetting */
     $this->log('copy srCertificateDefinitionSetting');
     foreach ($this->getSettings() as $setting) {
         $s = $new_definition->getSettingByIdentifier($setting->getIdentifier());
         $this->log($setting->getIdentifier());
         if (!$s instanceof srCertificateDefinitionSetting) {
             $this->log('not found, generating new one');
             $s = new srCertificateDefinitionSetting();
             $s->setDefinitionId($new_definition->getId());
             $s->setIdentifier($setting->getIdentifier());
             $s->create();
         }
         $s->setValue($setting->getValue());
         $s->update();
     }
     /** @var $ph_value srCertificatePlaceholderValue */
     $this->log('copy srCertificatePlaceholderValue');
     foreach ($this->getPlaceholderValues() as $ph_value) {
         $ph = $new_definition->getPlaceholderValueByPlaceholderId($ph_value->getPlaceholderId());
         $this->log($ph_value->getPlaceholderId());
         if (!$ph instanceof srCertificatePlaceholderValue) {
             $this->log('not found, generating new one');
             $ph = new srCertificatePlaceholderValue();
             $ph->setDefinitionId($new_definition->getId());
             $ph->setPlaceholderId($ph_value->getPlaceholderId());
             $ph->create();
         }
         $ph->setValue($ph_value->getValue());
         // This does set the values for each language
         $ph->update();
     }
     /** @var $cust_setting srCertificateCustomDefinitionSetting */
     foreach ($this->getCustomSettings() as $cust_setting) {
         $cs = $new_definition->getCustomSettingByIdentifier($cust_setting->getIdentifier());
         $this->log($cust_setting->getIdentifier());
         if (!$cs instanceof srCertificateCustomDefinitionSetting) {
             $this->log('not found, generating new one');
             $cs = new srCertificateCustomDefinitionSetting();
             $cs->setDefinitionId($new_definition->getId());
             $cs->setIdentifier($cust_setting->getIdentifier());
             $cs->create();
         }
         $cs->setValue($cust_setting->getValue());
         // This does set the values for each language
         $cs->update();
         $this->log('old value: ' . $cust_setting->getValue());
         $this->log('cloned value: ' . $cs->getValue());
     }
     $this->log('finished');
     return $new_definition;
 }
 /**
  * Load all the placeholders (standard and custom) with key => value
  * Custom placeholders are loaded in the correct language
  * All placeholders are passed to the hook class to do custom logic.
  * Finally keys are wrapped with the start/end symbols, e.g. [[key]]
  *
  * @param bool $anonymized
  */
 protected function loadPlaceholders($anonymized = false)
 {
     $placeholders = $this->getStandardPlaceholders($anonymized)->getParsedPlaceholders();
     $available_langs = $this->definition->getType()->getLanguages();
     $user_lang = $this->getUser()->getLanguage();
     $default_lang = $this->definition->getSettingByIdentifier(srCertificateTypeSetting::IDENTIFIER_DEFAULT_LANG);
     $lang = in_array($user_lang, $available_langs) ? $user_lang : $default_lang;
     /** @var $ph_value srCertificatePlaceholderValue */
     foreach ($this->definition->getPlaceholderValues() as $ph_value) {
         $placeholders[$ph_value->getPlaceholder()->getIdentifier()] = $ph_value->getValue($lang);
     }
     // Hacky: Add signature placeholders
     if ($this->definition->getSignatureId()) {
         $signature = $this->definition->getSignature();
         $placeholders['SIGNATURE_NAME'] = $signature->getFirstName() . ' ' . $signature->getLastName();
         $placeholders['SIGNATURE_FIRSTNAME'] = $signature->getFirstName();
         $placeholders['SIGNATURE_LASTNAME'] = $signature->getLastName();
         $placeholders['SIGNATURE_IMAGE'] = $signature->getFilePath(true);
         $placeholders['SIGNATURE_IMAGE_SUFFIX'] = $signature->getSuffix();
     }
     $this->placeholders = $this->pl->getHooks()->processPlaceholders($this, $placeholders);
     $this->placeholders = srCertificatePlaceholder::getFormattedPlaceholders($this->placeholders);
 }