Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 106 |
| AmsCustomFieldsService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 7 |
506.00 | |
0.00% |
0 / 106 |
| __construct | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 3 |
|||
| __clone | |
0.00% |
0 / 1 |
2.00 | |
0.00% |
0 / 1 |
|||
| getInstance | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 5 |
|||
| getAmsCustomFields | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 23 |
|||
| getAmsCustomFieldValue | |
0.00% |
0 / 1 |
42.00 | |
0.00% |
0 / 35 |
|||
| saveAmsCustomFieldValue | |
0.00% |
0 / 1 |
30.00 | |
0.00% |
0 / 29 |
|||
| getAllAmsCustomFieldsStudents | |
0.00% |
0 / 1 |
6.00 | |
0.00% |
0 / 10 |
|||
| <?php | |
| namespace com\linways\core\ams\professional\service; | |
| use com\linways\core\ams\professional\exception\ProfessionalException; | |
| use com\linways\core\ams\professional\constant\AmsCustomFieldsEntities; | |
| use com\linways\core\ams\professional\mapper\AmsCustomFieldsServiceMapper; | |
| use com\linways\core\ams\professional\request\SaveAmsCustomFieldsValueRequest; | |
| use stdClass; | |
| class AmsCustomFieldsService extends BaseService | |
| { | |
| private static $_instance = null; | |
| // /Condition 2 - Locked down the constructor | |
| private function __construct() | |
| { | |
| $this->mapper = AmsCustomFieldsServiceMapper::getInstance()->getMapper(); | |
| } | |
| // Prevent any oustide instantiation of this class | |
| // /Condition 3 - Prevent any object or instance of that class to be cloned | |
| private function __clone() | |
| {} | |
| // Prevent any copy of this object | |
| // /Condition 4 - Have a single globally accessible static method | |
| public static function getInstance() | |
| { | |
| if (! is_object(self::$_instance)) // or if( is_null(self::$_instance) ) or if( self::$_instance == null ) | |
| self::$_instance = new self(); | |
| return self::$_instance; | |
| } | |
| /** | |
| * get ams custom fields by entity | |
| * @param AmsCustomFieldsEntities $entity | |
| * @return Array | |
| */ | |
| public function getAmsCustomFields($entity,$fields = [],$editableFieldsOnly = false) | |
| { | |
| $entity = $this->realEscapeString($entity); | |
| $fields = $this->realEscapeArray($fields); | |
| if(empty($entity)) | |
| { | |
| throw new ProfessionalException(ProfessionalException::INVALID_ENTITY,"Entity cannot be empty!"); | |
| } | |
| $query = "SELECT id,label,code,data_type,validations,entity,created_by,created_date,updated_by,updated_date,html,properties,is_editable FROM ams_custom_fields WHERE is_active=1 AND entity='$entity'"; | |
| if(!empty($fields)) | |
| { | |
| $query .= " AND code in ('".(implode("','",$fields))."')"; | |
| } | |
| if($editableFieldsOnly){ | |
| $query .= " AND is_editable = 1"; | |
| } | |
| try{ | |
| $fields = $this->executeQueryForList($query,$this->mapper[AmsCustomFieldsServiceMapper::GET_AMS_CUSTOM_FIELDS]); | |
| }catch(\Exception $e) | |
| { | |
| throw new ProfessionalException(ProfessionalException::ERROR_FETCHING_AMS_CUSTOM_FIELDS,"Cannot fetch ams fields! Please try again"); | |
| } | |
| return $fields; | |
| } | |
| /** | |
| * get ams custom field value | |
| * @param AmsCustomFieldsEntities $entity | |
| * @param Integer $entityId | |
| * @param Array $fields | |
| * @return Object | |
| */ | |
| public function getAmsCustomFieldValue($entity,$entityId,$fields = []) | |
| { | |
| $entity = $this->realEscapeString($entity); | |
| $entityId = $this->realEscapeString($entityId); | |
| $fields = $this->realEscapeArray($fields); | |
| $entityTable = ""; | |
| $entityCondition = ""; | |
| if($entity === AmsCustomFieldsEntities::STUDENT) { | |
| $entityTable = "ams_custom_fields_student_value"; | |
| $entityCondition = "student_id='".$entityId."'"; | |
| } else if ($entity === AmsCustomFieldsEntities::STAFF) { | |
| $entityTable = "ams_custom_fields_staff_value"; | |
| $entityCondition = "staff_id='".$entityId."'"; | |
| } | |
| $query = "SELECT fields FROM $entityTable WHERE $entityCondition"; | |
| try{ | |
| $entityFields = $this->executeQueryForObject($query); | |
| if(!empty($entityFields)) | |
| { | |
| $entityFields = json_decode($entityFields->fields); | |
| } | |
| else | |
| { | |
| $entityFields = new \StdClass; | |
| } | |
| $amsFields = $this->getAmsCustomFields($entity,$fields); | |
| foreach($amsFields as $field) | |
| { | |
| $field->value = $entityFields->{$field->code}; | |
| } | |
| }catch(\Exception $e) | |
| { | |
| throw new ProfessionalException(ProfessionalException::ERROR_FETCHONG_AMS_CUSTOM_FIELD_VALUE,"Cannot fetch field values! Please try again"); | |
| } | |
| return $amsFields; | |
| } | |
| /** | |
| * save ams custom field value | |
| * @param SaveAmsCustomFieldsValueRequest $request | |
| */ | |
| public function saveAmsCustomFieldValue(SaveAmsCustomFieldsValueRequest $request) | |
| { | |
| $request->entityId = $this->realEscapeString($request->entityId); | |
| $request->entity = $this->realEscapeString($request->entity); | |
| $request->createdBy = $this->realEscapeString($request->createdBy); | |
| $request->updatedBy = $this->realEscapeString($request->updatedBy); | |
| $query = " INSERT INTO "; | |
| $insertValues = ""; | |
| $updateValues = ""; | |
| $entityFieldsObject = new \StdClass; | |
| if($request->entity === AmsCustomFieldsEntities::STUDENT) { | |
| $query .= "ams_custom_fields_student_value (student_id,fields,created_by,created_date,updated_by,updated_date) "; | |
| } else if ($request->entity === AmsCustomFieldsEntities::STAFF) { | |
| $query .= "ams_custom_fields_staff_value (staff_id,fields,created_by,created_date,updated_by,updated_date) "; | |
| } | |
| $fields = array_keys(get_object_vars($request->fieldValues)); | |
| try{ | |
| $amsFields = $this->getAmsCustomFields($request->entity,$fields); | |
| foreach($amsFields as $amsField) | |
| { | |
| //TODO: Implement validation | |
| $entityFieldsObject->{$amsField->code} = $request->fieldValues->{$amsField->code}; | |
| $updateValues .= " fields=JSON_SET(fields,'$.".$amsField->code."','". addslashes($request->fieldValues->{$amsField->code})."'),"; | |
| } | |
| $query .= " VALUES ('$request->entityId','".addslashes(json_encode($entityFieldsObject))."','$request->createdBy',UTC_TIMESTAMP(),'$request->updatedBy',UTC_TIMESTAMP()) ON DUPLICATE KEY UPDATE $updateValues updated_by='$request->updatedBy',updated_date=UTC_TIMESTAMP() "; | |
| $this->executeQuery($query); | |
| }catch(\Exception $e) | |
| { | |
| throw new ProfessionalException(ProfessionalException::ERROR_SAVING_AMS_CUSTOM_FIELD_VALUE,"Error saving ams fields! Please try again"); | |
| } | |
| } | |
| public function getAllAmsCustomFieldsStudents() | |
| { | |
| try{ | |
| $sql = "SELECT * from ams_custom_fields acf WHERE entity = 'STUDENT'"; | |
| return $this->executeQueryForList($sql); | |
| } | |
| catch(\Exception $e) | |
| { | |
| throw new ProfessionalException(ProfessionalException::ERROR_SAVING_AMS_CUSTOM_FIELD_VALUE,"Error saving ams fields! Please try again"); | |
| } | |
| } | |
| } |