Esempio n. 1
0
/**
 * Get all the departments of a company
 *
 * @param  int  the company id
 *
 * @return array  list of departments
 */
function erp_hr_get_departments($args = [])
{
    $defaults = array('number' => 20, 'offset' => 0, 'orderby' => 'id', 'order' => 'asc', 'no_object' => false);
    $args = wp_parse_args($args, $defaults);
    $cache_key = 'erp-get-departments';
    $results = wp_cache_get($cache_key, 'wp-erp');
    $department = new \WeDevs\ERP\HRM\Models\Department();
    if (isset($args['s'])) {
        $results = $department->where('title', 'LIKE', '%' . $_GET['s'] . '%')->get()->toArray();
        $results = erp_array_to_object($results);
    }
    if (false === $results) {
        $results = $department->get()->toArray();
        $results = erp_array_to_object($results);
        wp_cache_set($cache_key, $results, 'wp-erp');
    }
    $results = erp_parent_sort($results);
    $departments = [];
    if ($results) {
        foreach ($results as $key => $row) {
            if (true === $args['no_object']) {
                $departments[] = $row;
            } else {
                $departments[] = new WeDevs\ERP\HRM\Department(intval($row->id));
            }
        }
    }
    return $departments;
}
Esempio n. 2
0
/**
 * sort parents before children
 *
 * @param array   $objects input objects with attributes 'id' and 'parent'
 * @param array   $result  (optional, reference) internal
 * @param integer $parent  (optional) internal
 * @param integer $depth   (optional) internal
 *
 * @return array           output
 */
function erp_parent_sort(array $objects, array &$result = array(), $parent = 0, $depth = 0)
{
    foreach ($objects as $key => $object) {
        if ($object->parent == $parent) {
            $object->depth = $depth;
            array_push($result, $object);
            unset($objects[$key]);
            erp_parent_sort($objects, $result, $object->id, $depth + 1);
        }
    }
    return $result;
}