setTable('department_notification_sms'); } /** * Add a department notification SMS object * @param int $department The department id * @param array $data * @return void * @throws Exception If the object was not created successfully */ public function add(int $department, array $data): void { global /** @var db $db */ $db; // Set the default values $data_default = [ 'label' => '', 'phone_country_code' => 45, 'phone' => 0, 'enabled' => 1, ]; // Merge the default values with the data $data = array_merge($data_default, $data); // Sanitize the input $data['label'] = (string)$db->escape_string($data['label']); $data['phone_country_code'] = (int)$data['phone_country_code']; $data['phone'] = (int)$db->escape_string($data['phone']); // Add the object $tmp_id = self::add_object([ ...$data, 'department' => (int)$department, ]); if (!$tmp_id) { throw new Exception('The object was not created successfully.'); } $this->id = $tmp_id; self::getObjectProperties(); self::objectChanged(); } public function getObjectProperties(): void { $this->department = new object_property($this->table, $this->id, 'department', 'int', false); $this->label = new object_property($this->table, $this->id, 'label', 'string', false); $this->phone_country_code = new object_property($this->table, $this->id, 'phone_country_code', 'int', false); $this->phone = new object_property($this->table, $this->id, 'phone', 'int', false); $this->verified_at = new object_property($this->table, $this->id, 'verified_at', 'datetime', false); $this->enabled = new object_property($this->table, $this->id, 'enabled', 'bool', false); $this->verification_code = new object_property($this->table, $this->id, 'verification_code', 'string', false); $this->created_at = new object_property($this->table, $this->id, 'created_at', 'datetime', false); } public function objectChanged(): void { //TODO: Add cache invalidation } /** * Get the object as an array * @return array The object as an array */ public function asArray(): array { return [ 'id' => $this->id, 'department' => $this->department->value(), 'label' => $this->label->value(), 'phone_country_code' => $this->phone_country_code->value(), 'phone' => $this->phone->value(), 'verified_at' => $this->verified_at->value(), 'enabled' => $this->enabled->value(), 'created_at' => $this->created_at->value(), ]; } /** * Get the department active phone numbers * @param int $id The department id * @return array The department active phone numbers */ public function getDepartmentActivePhoneNumbers(int $id): array { $tmp_phones = []; $result = self::getFieldsWhere([ 'department' => $id, 'enabled' => 1, ], [ 'phone_country_code', 'phone', ]); if ($result) { foreach ( $result as $row ) { $tmp_phones[] = '' . $row['phone_country_code'] . $row['phone']; } } return $tmp_phones; } }