示例#1
0
	public function add()
	{
    $app = JFactory::getApplication();
    $db = JFactory::getDbo();
    $user = JFactory::getUser();
    $isRoot = $user->authorise('core.admin');

    // JInput object
    $input = $app->input;
    header('Content-Type: application/json');

    //Only Authorised users can add departments.
    if ($user->get('guest') == 1)
    {
      echo json_encode(array("success"=>false, 'message'=>'Вы должны авторизироваться.'));
      JFactory::getApplication()->close();
      return;
    }

    $departmentName = $_POST['departmentName'];
    $departmentUrl = $_POST['departmentUrl'];
    $profileId = $input->getInt('profileId');
    $clinicId = $input->getInt('clinicId', 0);

    if(!( $user->get('id')==$profileId || $isRoot)){
      echo json_encode(array("success"=>false, 'message'=>'У вас нет прав добавлять отделения данному профайлу.'));
      JFactory::getApplication()->close();
      return;
    }

    if($departmentName){
      $db->setQuery("SELECT 1 FROM #__comprofiler_plugin_department
                     WHERE title=".$db->quote($departmentName)."
                       AND profile_id=".$db->quote($profileId)."
                       AND clinic_id=".$db->quote($clinicId));
      if($db->loadResult()=="1"){
        echo json_encode(array("success"=>false, 'message'=>'Отделение с именем '.$departmentName.' уже существует.'));
        JFactory::getApplication()->close();
        return;
      }
    }

    Departments::addDepartment($departmentName, $profileId, $clinicId, $departmentUrl);

    $result = array('success'=>true, 'message'=>'Отделение добавлено', 'departments'=>
      DepartmentsView::renderDepartments(Departments::getDepartments($profileId),$profileId,$user, $isRoot));
    echo json_encode( $result );
    JFactory::getApplication()->close();
	}