Fix: limit to 100 by default to avoid freeze

This commit is contained in:
Regis Houssin 2017-05-26 16:52:27 +02:00
parent f8fead4b90
commit ee2019ae37

View File

@ -1,7 +1,7 @@
<?php <?php
/* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr> /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
* Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or * the Free Software Foundation; either version 3 of the License, or
@ -23,14 +23,14 @@
/** /**
* API class for Agenda Events * API class for Agenda Events
* *
* @access protected * @access protected
* @class DolibarrApiAccess {@requires user,external} * @class DolibarrApiAccess {@requires user,external}
*/ */
class AgendaEvents extends DolibarrApi class AgendaEvents extends DolibarrApi
{ {
/** /**
* @var array $FIELDS Mandatory fields, checked when create and update object * @var array $FIELDS Mandatory fields, checked when create and update object
*/ */
static $FIELDS = array( static $FIELDS = array(
); );
@ -40,7 +40,7 @@ class AgendaEvents extends DolibarrApi
*/ */
public $actioncomm; public $actioncomm;
/** /**
* Constructor * Constructor
*/ */
@ -55,61 +55,61 @@ class AgendaEvents extends DolibarrApi
* Get properties of a Agenda Events object * Get properties of a Agenda Events object
* *
* Return an array with Agenda Events informations * Return an array with Agenda Events informations
* *
* @param int $id ID of Agenda Events * @param int $id ID of Agenda Events
* @return array|mixed Data without useless information * @return array|mixed Data without useless information
* *
* @throws RestException * @throws RestException
*/ */
function get($id) function get($id)
{ {
if(! DolibarrApiAccess::$user->rights->agenda->myactions->read) { if(! DolibarrApiAccess::$user->rights->agenda->myactions->read) {
throw new RestException(401, "Insuffisant rights to read an event"); throw new RestException(401, "Insuffisant rights to read an event");
} }
$result = $this->actioncomm->fetch($id); $result = $this->actioncomm->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Agenda Events not found'); throw new RestException(404, 'Agenda Events not found');
} }
if(! DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->ownerid != DolibarrApiAccess::$user->id) { if(! DolibarrApiAccess::$user->rights->agenda->allactions->read && $this->actioncomm->ownerid != DolibarrApiAccess::$user->id) {
throw new RestException(401, "Insuffisant rights to read event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id); throw new RestException(401, "Insuffisant rights to read event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
} }
if( ! DolibarrApi::_checkAccessToResource('agenda',$this->actioncomm->id)) { if( ! DolibarrApi::_checkAccessToResource('agenda',$this->actioncomm->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
$this->actioncomm->fetchObjectLinked(); $this->actioncomm->fetchObjectLinked();
return $this->_cleanObjectDatas($this->actioncomm); return $this->_cleanObjectDatas($this->actioncomm);
} }
/** /**
* List Agenda Events * List Agenda Events
* *
* Get a list of Agenda Events * Get a list of Agenda Events
* *
* @param string $sortfield Sort field * @param string $sortfield Sort field
* @param string $sortorder Sort order * @param string $sortorder Sort order
* @param int $limit Limit for list * @param int $limit Limit for list
* @param int $page Page number * @param int $page Page number
* @param string $user_ids User ids filter field (owners of event). Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i} * @param string $user_ids User ids filter field (owners of event). Example: '1' or '1,2,3' {@pattern /^[0-9,]*$/i}
* @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'%dol%') and (t.date_creation:<:'20160101')" * @param string $sqlfilters Other criteria to filter answers separated by a comma. Syntax example "(t.label:like:'%dol%') and (t.datec:<:'20160101')"
* @return array Array of Agenda Events objects * @return array Array of Agenda Events objects
*/ */
function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 0, $page = 0, $user_ids = 0, $sqlfilters = '') { function index($sortfield = "t.id", $sortorder = 'ASC', $limit = 100, $page = 0, $user_ids = 0, $sqlfilters = '') {
global $db, $conf; global $db, $conf;
$obj_ret = array(); $obj_ret = array();
// case of external user // case of external user
$socid = 0; $socid = 0;
if (! empty(DolibarrApiAccess::$user->societe_id)) $socid = DolibarrApiAccess::$user->societe_id; if (! empty(DolibarrApiAccess::$user->socid)) $socid = DolibarrApiAccess::$user->socid;
// If the internal user must only see his customers, force searching by him // If the internal user must only see his customers, force searching by him
$search_sale = 0; $search_sale = 0;
if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id;
$sql = "SELECT t.id as rowid"; $sql = "SELECT t.id as rowid";
$sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as t"; $sql.= " FROM ".MAIN_DB_PREFIX."actioncomm as t";
$sql.= ' WHERE t.entity IN ('.getEntity('agenda', 1).')'; $sql.= ' WHERE t.entity IN ('.getEntity('agenda', 1).')';
@ -121,7 +121,7 @@ class AgendaEvents extends DolibarrApi
$sql .= " AND sc.fk_user = ".$search_sale; $sql .= " AND sc.fk_user = ".$search_sale;
} }
// Add sql filters // Add sql filters
if ($sqlfilters) if ($sqlfilters)
{ {
if (! DolibarrApi::_checkFilters($sqlfilters)) if (! DolibarrApi::_checkFilters($sqlfilters))
{ {
@ -130,7 +130,7 @@ class AgendaEvents extends DolibarrApi
$regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)';
$sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")";
} }
$sql.= $db->order($sortfield, $sortorder); $sql.= $db->order($sortfield, $sortorder);
if ($limit) { if ($limit) {
if ($page < 0) if ($page < 0)
@ -143,7 +143,7 @@ class AgendaEvents extends DolibarrApi
} }
$result = $db->query($sql); $result = $db->query($sql);
if ($result) if ($result)
{ {
$num = $db->num_rows($result); $num = $db->num_rows($result);
@ -181,7 +181,7 @@ class AgendaEvents extends DolibarrApi
if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) { if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id); throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
} }
// Check mandatory fields // Check mandatory fields
$result = $this->_validate($request_data); $result = $this->_validate($request_data);
@ -198,18 +198,18 @@ class AgendaEvents extends DolibarrApi
if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) { if ($this->actioncomm->create(DolibarrApiAccess::$user) < 0) {
throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors)); throw new RestException(500, "Error creating event", array_merge(array($this->actioncomm->error), $this->actioncomm->errors));
} }
return $this->actioncomm->id; return $this->actioncomm->id;
} }
/** /**
* Update Agenda Event general fields (won't touch lines of expensereport) * Update Agenda Event general fields (won't touch lines of expensereport)
* *
* @param int $id Id of Agenda Event to update * @param int $id Id of Agenda Event to update
* @param array $request_data Datas * @param array $request_data Datas
* *
* @return int * @return int
*/ */
/* /*
function put($id, $request_data = NULL) { function put($id, $request_data = NULL) {
@ -219,12 +219,12 @@ class AgendaEvents extends DolibarrApi
if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) { if(! DolibarrApiAccess::$user->rights->agenda->allactions->create && DolibarrApiAccess::$user->id != $request_data['userownerid']) {
throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id); throw new RestException(401, "Insuffisant rights to create an Agenda Event for owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
} }
$result = $this->expensereport->fetch($id); $result = $this->expensereport->fetch($id);
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'expensereport not found'); throw new RestException(404, 'expensereport not found');
} }
if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) { if( ! DolibarrApi::_checkAccessToResource('expensereport',$this->expensereport->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
@ -232,19 +232,19 @@ class AgendaEvents extends DolibarrApi
if ($field == 'id') continue; if ($field == 'id') continue;
$this->expensereport->$field = $value; $this->expensereport->$field = $value;
} }
if($this->expensereport->update($id, DolibarrApiAccess::$user,1,'','','update')) if($this->expensereport->update($id, DolibarrApiAccess::$user,1,'','','update'))
return $this->get($id); return $this->get($id);
return false; return false;
} }
*/ */
/** /**
* Delete Agenda Event * Delete Agenda Event
* *
* @param int $id Agenda Event ID * @param int $id Agenda Event ID
* *
* @return array * @return array
*/ */
function delete($id) function delete($id)
@ -252,39 +252,39 @@ class AgendaEvents extends DolibarrApi
if(! DolibarrApiAccess::$user->rights->agenda->myactions->delete) { if(! DolibarrApiAccess::$user->rights->agenda->myactions->delete) {
throw new RestException(401, "Insuffisant rights to delete your Agenda Event"); throw new RestException(401, "Insuffisant rights to delete your Agenda Event");
} }
$result = $this->actioncomm->fetch($id); $result = $this->actioncomm->fetch($id);
if(! DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) { if(! DolibarrApiAccess::$user->rights->agenda->allactions->delete && DolibarrApiAccess::$user->id != $this->actioncomm->userownerid) {
throw new RestException(401, "Insuffisant rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id); throw new RestException(401, "Insuffisant rights to delete an Agenda Event of owner id ".$request_data['userownerid'].' Your id is '.DolibarrApiAccess::$user->id);
} }
if( ! $result ) { if( ! $result ) {
throw new RestException(404, 'Agenda Event not found'); throw new RestException(404, 'Agenda Event not found');
} }
if( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) { if( ! DolibarrApi::_checkAccessToResource('actioncomm',$this->actioncomm->id)) {
throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login); throw new RestException(401, 'Access not allowed for login '.DolibarrApiAccess::$user->login);
} }
if( ! $this->actioncomm->delete(DolibarrApiAccess::$user)) { if( ! $this->actioncomm->delete(DolibarrApiAccess::$user)) {
throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error); throw new RestException(500, 'Error when delete Agenda Event : '.$this->actioncomm->error);
} }
return array( return array(
'success' => array( 'success' => array(
'code' => 200, 'code' => 200,
'message' => 'Agenda Event deleted' 'message' => 'Agenda Event deleted'
) )
); );
} }
/** /**
* Validate fields before create or update object * Validate fields before create or update object
* *
* @param array $data Array with data to verify * @param array $data Array with data to verify
* @return array * @return array
* @throws RestException * @throws RestException
*/ */
function _validate($data) function _validate($data)
@ -294,7 +294,7 @@ class AgendaEvents extends DolibarrApi
if (!isset($data[$field])) if (!isset($data[$field]))
throw new RestException(400, "$field field missing"); throw new RestException(400, "$field field missing");
$event[$field] = $data[$field]; $event[$field] = $data[$field];
} }
return $event; return $event;
} }