/** * @param $text * @param $patient * * @return mixed * * @throws Exception */ public static function replace($text, $patient) { preg_match_all('/\\[([a-z]{3})\\]/is', $text, $m); foreach ($m[1] as $el) { $count = \PatientShortcode::model()->count('code=?', array(strtolower($el))); if ($count == 1) { if ($code = \PatientShortcode::model()->find('code=?', array(strtolower($el)))) { $text = $code->replaceText($text, $patient, (bool) preg_match('/^[A-Z]/', $el)); } } elseif ($count > 1) { throw new \Exception("Multiple shortcode definitions for {$el}"); } } return $text; }
/** * Register a short code for this event type. * * @param $code * @param $method * @param bool $description * @throws Exception */ public function registerShortCode($code, $method, $description = false) { if (!preg_match('/^[a-zA-Z]{3}$/', $code)) { throw new Exception("Invalid shortcode: {$code}"); } $default_code = $code; if (PatientShortcode::model()->find('code=?', array(strtolower($code)))) { $n = '00'; while (PatientShortcode::model()->find('z' . $n)) { $n = str_pad((int) $n + 1, 2, '0', STR_PAD_LEFT); } $code = "z{$n}"; echo "Warning: attempt to register duplicate shortcode '{$default_code}', replaced with 'z{$n}'\n"; } $ps = new PatientShortcode(); $ps->event_type_id = $this->id; $ps->code = $code; $ps->default_code = $default_code; $ps->method = $method; $ps->description = $description; if (!$ps->save()) { throw new Exception("Unable to save PatientShortcode: " . print_r($ps->getErrors(), true)); } }
<div class="row field-row"> <div class="large-10 large-offset-2 column shortCodeDescription"> </div> </div> <div class="row field-row"> <div class="large-8 large-offset-2 column"> <div class="row field-row"> <div class="large-3 column"> <label for="shortcode"> Add shortcode: </label> </div> <div class="large-6 column end"> <?php echo CHtml::dropDownList('shortcode', '', CHtml::listData(PatientShortcode::model()->findAll(array('order' => 'description asc')), 'code', 'description'), array('empty' => '- Select -')); ?> </div> </div> </div> </div> <div class="row field-row"> <div class="large-10 large-offset-2 column"> <button class="button small primary event-action" name="save" type="submit" id="et_save">Save</button> <button class="warning button small primary cancelEditMacro" name="cancel" type="submit">Cancel</button> </div> </div> <?php $this->endWidget(); ?> </div>